I am wanting to use lm()
to estimate predicted group means and then perform linear hypothesis tests across the different 'levels' of another stratifying variable.
Here is my data:
library(tidyverse)
library(car)
set.seed(42)
n <- 1000
dat <- data.frame(id=1:n,
before=runif(n, min=45, max=85),
after=runif(n, min=24, max=160),
treat = factor(sample(c('Treat','Control'), n, rep=TRUE, prob=c(.5, .5))),
meds = factor(sample(c('A','B', "C", "D"), n, rep=TRUE, prob=c(.18, .32, .27, 23))),
age=sample(18:80, n, replace=TRUE),
sex = factor(sample(c('Male','Female'), n, rep=TRUE, prob=c(.6, .4))),
smoke=factor(sample(c("Never", 'Former', 'Current'), n, rep=TRUE, prob=c(.25, .6, .15))))
dat$change <- dat$after-dat$before
dat$treat <- as.factor(dat$treat)
m1 <- lm(change~treat*meds+sex+age+smoke, dat)
pred.dat = expand.grid(dat$treat, meds=unique(dat$meds))
predict(m1, newdata=pred.dat)
linearHypothesis(m1, treat("Treat"), meds('A','B', "C", "D"))
Essentially, I would like to compare the predicted group means for change
in both Treat
and Control
across the different levels of group
so I end up with a 2x4 matrix of these data.
I understand my syntax is incomplete owing to my lack of understanding of how the syntax works for some of these calls.
Any help, as always, is immensely appreciated.
Many thanks,
Sandro