- Thread starter Mikkelsoeren
- Start date

Well there is a package called "contrast" in R but I typically just do them by hand.

Code:

```
test.contrast = function(lm.out, C, d = 0){
# Provides a test of Ho: C*b = d vs. Ha: C*b != d
# lm.out: - is the linear model used
# C: ----- is a matrix with the desired set of contrasts
# which may contain more than one row
# d: ----- a vector of values to test against
b <- coef(lm.out)
V <- vcov(lm.out)
df.numerator <- nrow(C)
df.denominator <- lm.out$df
Cb.d <- (C %*% b) - d
Fstat <- drop(t(Cb.d) %*% solve(C %*% V %*% t(C)) %*% Cb.d/df.numerator)
pvalue <- 1 - pf(Fstat, df.numerator, df.denominator)
ans <- list(Fstat = Fstat, pvalue = pvalue)
return(ans)
}
# Generate some fake data
n <- 20
sigma <- 1
betas <- c(7, 2, -2)
x1 <- 1:n
x2 <- runif(n)
X <- matrix(c(rep(1,n),x1, x2), ncol = 3)
y <- X %*% betas + rnorm(n, 0, sigma)
# Fit the linear model
o <- lm(y ~ x1 + x2)
# Test the idea that b1 = -b2 ie b1 + b2 = 0
C <- matrix(c(0, 1, 1), nrow = 1)
test.contrast(o, C)
```

Last edited:

Okay friends, this is my exam. I did not think it was necessary to read up on the material because I thought I knew everything. I thought I got all knowledge from heaven

dudes, im not much cleaver here. Please introduce me to the rapidhole

Are you just asking for quick answers? You could try googling "linear contrast".

I've always thought that was only for certain types of contrasts though. I don't know how to specify a generic contrast through that parameter.

A list, whose entries are values (numeric matrices or character strings naming functions) to be used as replacement values for the contrasts replacement function and whose names are the names of columns of data containing factors.