r - Shuffle data before doing glm, then repeating x times -
i have data contains group variable (0/1), , score pr individual, 2000 individuals. data set looks this:
id group score a1 1 3.5 a2 1 3.2 a3 0 2.8 a4 0 2.5
i want test if group variable can predicted through score, , have used following in r:
glm(group~score,family=binomial)
now test p-value shuffling group variable, doing glm again. @ least 10,000 times, , possibly more, each time printing p-value score in file, there 1 row per permutation. i've looked @ sample(), struggle combine glm() , how output p-value only. in script/formula change number of permutations, , change glm formula if choose add covariates.
thank help!
you're on right track.
example (i added 1 more value suppress warnings "fitted probabilities numerically 0 or 1")
ex <- read.table(textconnection( "id group score a1 1 3.5 a2 1 3.2 a3 0 2.8 a4 0 2.5 a5 1 2.4"),header=true) g0 <- glm(group~score,data=ex,family=binomial)
now need function compute summary p-value (you can on fly in replicate
, way cleaner).
pvalfun <- function() { g <- update(g0,data=transform(ex,group=sample(group))) coef(summary(g))["score","pr(>|z|)"] } res <- replicate(1000,pvalfun())
or
library(plyr) res <- raply(1000,pvalfun(),.progress="text")
or
library(glmperm) ptest2 <- prr.test(group~score,"score",data=ex,family=binomial) summary(ptest2)
Comments
Post a Comment