Created
June 17, 2012 00:10
-
-
Save anonymous/2942913 to your computer and use it in GitHub Desktop.
treatment and sum contrasts in R
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
#create dummy data | |
minex <- c() | |
minex$hand <- c(rep(1,20),rep(0,20)) | |
minex$speed <- c(rep(c(rep(20,10),rep(50,10)),2)) | |
minex$angle <- c(rep(c(rep(45,5),rep(75,5)),4)) | |
minex <- as.data.frame(minex) | |
beta0 <- 18 | |
beta1 <- -16 | |
beta2 <- -6 | |
beta3 <- 30 | |
beta4 <- 13 | |
sigma2 <- 0.3 | |
minex$distance <- beta0 + beta1*minex$hand + beta2*minex$angle + beta3*minex$speed + beta4*minex$hand*minex$angle + rnorm(dim(minex),0,sqrt(sigma2)) | |
minex$hand <- as.factor(minex$hand) | |
minex$speed <- as.factor(minex$speed) | |
minex$angle <- as.factor(minex$angle) | |
# treatment contrasts | |
options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) | |
m1 <- lm(distance ~ hand + angle + speed + hand:angle + hand:speed + angle:speed, data=minex, x=T) | |
#m1 <- lm(distance ~ hand + angle + speed, data=minex, x=T) | |
m1$x -> X | |
X1 <- X; | |
d <- dim(X1) | |
v <- solve(t(X1) %*% X1) | |
sd <- diag(1/sqrt(diag(v))) | |
c <- sd %*% v %*% t(sd) | |
X1.cor.betahat <- round(c[2:d[2], 2:d[2]], 3) | |
X1.Sigma <- solve(t(X1) %*% X1) | |
X1.contrast <- solve(t(X1) %*% X1) %*% t(X1) | |
cbind(X1.contrast[2,],minex$hand,X1[,2]) | |
#repeat with sum contrasts | |
options(contrasts = c(unordered = "contr.sum", ordered = "contr.poly")) | |
m2 <- lm(distance ~ hand + angle + speed + hand:angle + hand:speed + angle:speed, data=minex, x=T) | |
#m2 <- lm(distance ~ hand + angle + speed, data=minex, x=T) | |
m2$x -> X | |
X2 <- X; | |
d <- dim(X2) | |
v <- solve(t(X2) %*% X2) | |
sd <- diag(1/sqrt(diag(v))) | |
c <- sd %*% v %*% t(sd) | |
X2.cor.betahat <- round(c[2:d[2], 2:d[2]], 3) | |
X2.Sigma <- solve(t(X2) %*% X2) | |
X2.contrast <- solve(t(X2) %*% X2) %*% t(X2) | |
cbind(X2.contrast[2,],minex$hand,X2[,2]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment