Skip to content

Instantly share code, notes, and snippets.

View AWKruijt's full-sized avatar

Anne-Wil AWKruijt

  • Netherlands Comprehensive Cancer Organisation (IKNL)
  • Groningen
  • X @t_awkr
View GitHub Profile
```{r}
library(tictoc)
library(splithalf)
library(splithalfr)
```
```{r}
tic()
data("ds_vpt", package = "splithalfr")
@AWKruijt
AWKruijt / diffScoreVarDecomp
Last active April 6, 2019 12:24
variance decomposition for difference scores - inspired by Hedge 2018
diffScoreVarDecomp <- function(cA_m1, cB_m1, cA_m2, cB_m2, plot = "both", measurename = "x") {
# calculations are heavily based on the irr's package icc() code:
# bind the four vectors first for row-wise deletion:
alldat <- as.matrix(na.omit(cbind(cA_m1, cB_m1, cA_m2, cB_m2)))
alldat <- cbind(alldat, "ds1" = alldat[,"cA_m1"] - alldat[,"cB_m1"], "ds2" = alldat[,"cA_m2"] - alldat[,"cB_m2"])
s1 <- round(rnorm(20, 600, 150))
s2a <- s1 + round(rnorm(20, 0, 80))
s2c <- s1 + 250
id <- seq(1:20)
iccdf <- as.data.frame(cbind(id, s1, s2a, s2c))
require(reshape2)
pldf <- melt(iccdf, id.vars = "id" )
---
title: "search pubmed and download pdfs"
output: html_notebook
---
store this rmd file in a folder
in the same folder also create a subfolder called 'pdfs'
```{r search pubmed and store search query in D}
library(RISmed)
@AWKruijt
AWKruijt / fetchCitingRecords
Last active March 21, 2019 21:57
quickly whipped up something to fetch citing articles from pubmed
# essentially same as before but now restructured into a function: getCitingPMIDs()
getCitingPMIDs <- function(PMID) {
lines <- readLines(paste0("https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi?dbfrom=pubmed&linkname=pubmed_pubmed_citedin&id=", PMID)) # construct quering URL using the PMID parameter in resTargetRecord, and fetch its contents into object lines
citingPMIDs <- sub("^.*(<Id>)(.*)(</Id>)", "\\2", lines[grep("<Id>", lines)]) # filter out lines in lines that contain <Id> and then filter out the content between <Id> and </Id>.Store in object citingPMIDs
citingPMIDs <- citingPMIDs[-1] # remove first entry in citingPMIDs, which is the target record
return(citingPMIDs)
}
#Project for if I ever get bored... a function that can be used at the top of a complex rmd file to list al chunks and their starting lines.
# I called out about this on twitter and someone referred me to the namer package, from which I initially stole a lot of code (specifically, most of this file: https://github.com/lockedata/namer/blob/master/R/utils.R). Twitter thread here: https://twitter.com/t_awkr/status/1082320989717958657
# a few months later I dramatically simplified the code, assessing only the name and startline for each chunk:
get_chunk_info <- function(){
lines <- readLines(rstudioapi::getActiveDocumentContext()$path) # read the current active document - notice that this reads the document as saved
@AWKruijt
AWKruijt / correlation table simple.r
Last active January 7, 2019 17:44
AWs corrtable function: i wanted a function that presents correlation tables in a reasonably nice but plain format that is sufficiently reminiscent of SPSS & APA style tables to use as the default correlation presentation style when sharing things
# AWs corrtable function: i wanted a function that presents correlation tables in a reasonably nice but plain format that is sufficiently reminiscent of SPSS & APA style tables to use as the default correlation presentation style when sharing things in collaborations with colleagues who might not be used to R much. The kind of graphical correlation thingies that many R packages provide are awesome but also a tad intimidating :)
# x -> dataframe to correlate
# adjust -> adjust parameter for the corr.test function. From corr.test documentation: "What adjustment for multiple tests should be used? ("holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none"). See p.adjust for details")."
# footert -> text for the footer. If not defined the default footer text is: "note: * p <.05, ** p <.01, *** p <.001" .
# captiont -> text for the caption. If not defined the default caption text is: "correlation table" .
@AWKruijt
AWKruijt / app.R
Last active December 3, 2018 17:52
MyFirstR2D3
#
# This is a Shiny web application incorporating a D3 graph, called with R2D3().
# You can run the application by clicking the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
# Find out more about R2D3 here:
#
# just code - for function version, see below
i_max <- 10000
l_seq <- 5
v_min <- 0
v_max <- 480
d_min <- 75
r <- rep(NA, l_seq)
@AWKruijt
AWKruijt / JT_RCC.R
Last active February 25, 2019 11:19
A function to calculate and plot Jacobson-Truax based reliable clinical change. Here as a gist rather than a package because it has not been fully checked yet (especially for crit B and C). Input & comments welcome!
JT_RCC <- function(x.pre, x.post, norm.M = NA, norm.SD = NA, RC.crit ="auto", reliability, ppid, plot = T, plottype = "JT", higherIsBetter = F) {
SEmeasurement <- sd(x.pre) * sqrt(1-reliability) # standard error of measurement based on the sd of the pre measurement/baseline
Sdiff<- sqrt(2*SEmeasurement^2) # the SD of the SEm for a difference score
# determine the criterion to determine recovery:
### C is preferred, followed by B, then A..