Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

loops - looping through multiple models to test multiple contrast based hypothesis test r

I am analyzing data with the lme4 package. My dependent variable (group_time) is a 9 level factor and I am therefore using contrast matrices to test certain hypotheses. I have 4 outcomes of interest, so I want to iterate the model 4 times which I have successfully done (see model 1). However, how do I take this nested model object (4 models nested into 1 list) and run my 4 contrast matrice hypothesis test on each model and then export the p values?

my data looks likes

```
unqid -> c(100,100,100,101,101,101,102,102,102,103,103,103,104,104,104,105,105,105)
group_time -> c(control_0, control_1, control_2, control_0, control_1, control_2,surgery_0, surgery_1, surgery_2, nonsurgery_0, nonsurgery_1, nonsurgery_2, control_0, control_1, control_2)
dv1 -> sample(1:100, 15)
dv2 -> sample(1:100, 15)
dv3 -> sample(1:100, 15)
dv4 -> sample(1:100, 15)
clean_data -> data.frame (unqid = unqid, group_time= group_time, dv1=dv1, dv2=dv2, dv3=dv3,dv4=dv4) 
```

My iterated model is as follows:

```
model1 <- lapply(clean_data[,3:6], function(x) lmer(x ~ -1 + group_time + (1|unqid) +(1|group_time:unqid),
                                                   REML=F, data=clean_data))
```

Then I want to iterate through these four hypotheses (should have 16 p values exported-- 4 answered hypothesis for each of the 4 models):

```
#HP1
C=matrix(c(0,-1,0,0,0,0,0,1,0)) 
chi2= t(t(C)%*%
          fixef(model1))%*%
  solve(t(C)%*%
          vcov(model1)%*%
          C,t(C)%*%
          fixef(model1))
chi2 <- as.vector(chi2)
Df=1 # i.e. the number of linearly independent columns in C.
pval1<- 1 - pchisq(chi2,Df)

#HP2
C=matrix(c(-1,0,0,0,0,0,1,0,0)) 
chi2= t(t(C)%*%
          fixef(model1))%*%
  solve(t(C)%*%
          vcov(model1)%*%
          C,t(C)%*%
          fixef(model1))
chi2 <- as.vector(chi2)
Df=1 # i.e. the number of linearly independent columns in C.
pval2<- 1 - pchisq(chi2,Df)

#HP3
C=matrix(c(0,0,-1,0,0,0,0,0,1)) 
chi2= t(t(C)%*%
          fixef(model1))%*%
  solve(t(C)%*%
          vcov(model1)%*%
          C,t(C)%*%
          fixef(model1))
chi2 <- as.vector(chi2)
Df=1 # i.e. the number of linearly independent columns in C.
pval3<- 1 - pchisq(chi2,Df)

#HP4
C=matrix(c(0,0,0,0,0,0,-1,1,0)) 
chi2= t(t(C)%*%
          fixef(model1))%*%
  solve(t(C)%*%
          vcov(model1)%*%
          C,t(C)%*%
          fixef(model1))
chi2 <- as.vector(chi2)
Df=1 # i.e. the number of linearly independent columns in C.
pval4<- 1 - pchisq(chi2,Df)
```
question from:https://stackoverflow.com/questions/65906732/looping-through-multiple-models-to-test-multiple-contrast-based-hypothesis-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...