Skip to content

Instantly share code, notes, and snippets.

@dfeng
Created November 14, 2012 21:11
Show Gist options
  • Save dfeng/4074855 to your computer and use it in GitHub Desktop.
Save dfeng/4074855 to your computer and use it in GitHub Desktop.
regression for horse problem
set.seed(10)
lm.power <- c()
spd.power <- c()
for (n in range) {
spd.pvalue <- c()
lm.pvalue <- c()
for (j in 1:1E2) {
mat <- c()
samplesize <- 1E2
for (i in 1:samplesize) {
mat <- rbind(mat,sample(1:n, prob=c(rep(1.2,floor(n / 2)), rep(1, n - floor(n / 2)))))
}
distances <- apply(mat, 1, permdist)
logd <- apply(mat, 1, tolodds)
# creating log-odds of start
n1 <- (1:n) / (n+1)
n2 <- log(n1 / (1 - n1))
logn <- rep(n2,samplesize)
# THIS LINE IS THE REGRESSION THING. THE OTHER LINES ARE NOT THAT IMPORTANT
# calculate the p-value of the linear model test
lm.pvalue <- c(lm.pvalue,summary(lm(as.vector(logd)~logn))$coef[2,4])
spd <- (distances - means[n-first+1])/sds[n-first+1]
average <- mean(spd)
# calculate the p-value of the permutation test (CLT)
spd.pvalue <- c(spd.pvalue, pnorm(average*sqrt(length(distances))))
}
# calculate the simulated power
spd.power <- c(spd.power, sum(spd.pvalue < 0.05 | spd.pvalue > 0.95)/length(spd.pvalue))
lm.power <- c(lm.power, sum(lm.pvalue < 0.05 | lm.pvalue > 0.95)/length(lm.pvalue))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment