This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' Function requires a PubMed API key. They are free and easily obtained | |
#' Instructions get an API key: | |
#' https://ncbiinsights.ncbi.nlm.nih.gov/2017/11/02/new-api-keys-for-the-e-utilities/ | |
#' Register here: https://account.ncbi.nlm.nih.gov/ | |
#' This function runs a PubMed query and returns a data frame of metadata for the results | |
#' Where the query returns >10,000 (which normally triggers an error from the API), | |
#' the function automatically breaks down the search into chunks by date and returns a combined and de-duped data set |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Main function to find the 'optimal' combination of rows and columns to drop in order to | |
# maximise the number of remaining data points in a data set | |
# Search space rapidly becomes enormous as data size grows, so this random search solution will | |
# only ever be a rough approximation of the optimal data set. | |
optimiseDataset <- function(data, max_iterations = 10000) { | |
best_solution <- data | |
dims=dim(data[complete.cases(data),]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install.packages("pubmedR") | |
# install.packages("rcrossref") | |
# devtools::install_github("ropensci/rAltmetric") | |
library(pubmedR) | |
library(rcrossref) | |
library(rAltmetric) | |
pacman::p_load(curl,readr, tidyverse,purrr) # load required packages | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
install.packages("pubmedR") | |
install.packages("rcrossref") | |
devtools::install_github("ropensci/rAltmetric") | |
install.packages("tidyverse") | |
install.packages("janitor") | |
library(pubmedR) | |
library(rcrossref) | |
library(tidyverse) | |
library(janitor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
altmetrics_new <- | |
function(doi = NULL, | |
apikey = NULL, | |
...) { | |
base_url <- "https://api.altmetric.com/v1/" | |
args <- list(key = apikey) | |
request <- | |
httr::GET(paste0(base_url, "doi/",doi)) | |
if(httr::status_code(request) == 404) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
library(dplyr) | |
library(tidyr) | |
library(faux) | |
library(randomForest) | |
# Set parameters ---------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# xgboost permutation function | |
#' This function takes an XGBoost model and some X and y data | |
#' (ideally this would be unseen holdout data but you could also use the training data) | |
#' and returns a data frame with an estimation of the contribution that each variable makes to the overall AUC | |
#' can take a long time to run with a large data set – nperm can be reduced to reduce compute time | |
PermuteImportXBG <- function(model, X, y, nperm = 100){ | |
predictors=model$feature_names |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' This short function takes some input from whatever R script you are working in and creates an | |
#' rmarkdown document and corresponding knitted HTML file on the fly | |
#' Basis for code is: https://stackoverflow.com/questions/60110904/how-to-generate-html-report-directly-from-r-script | |
my_markdown_rederer <- function(text, myList=myList) { | |
# The file name for the rmd doc | |
rmd_file_name <- "temp.Rmd" | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### 1. Cross tab function | |
# Simple cross-tab function, with %s | |
# Creates a cross tab of categorical variables, with %s in brackets after the number | |
# You can specify the names of the levels within the variables if you want, or leave blank to use the existing names | |
# (Be sure to enter the level names in the right order if you do specify them) | |
crossTab <- function(dat = dfRes, rowvar, colvar, rowvar_levels = NULL,colvar_levels = NULL){ | |
tab <- addmargins(table(pull(dat,rowvar), pull(dat,colvar))) %>% as.data.frame.matrix() | |
tab.prop <- round(100*prop.table(table(pull(dat,rowvar), pull(dat,colvar)),1),1) %>% as.data.frame.matrix() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### slightly adapted from here to save png or pdf and specify resolution etc: https://stackoverflow.com/questions/43051525/how-to-draw-pheatmap-plot-to-screen-and-also-save-to-file | |
# function detects '.png' or '.pdf' in the declared filename and assigns that file type | |
### pheatmap save function | |
save_pheatmap <- function(x, filename, width=12, height=12){ | |
stopifnot(!missing(x)) | |
stopifnot(!missing(filename)) | |
if(grepl(".png",filename)){ | |
png(filename, width=width, height=height, units = "in", res=300) |
NewerOlder