简体   繁体   English

for循环中的多个glm

[英]Multiple glm in for loop

I have an R dataframe, strongly simplified as: 我有一个R数据帧,强烈简化为:

id <- rep(1:2, c(6,8))
correct <- sample(0:1,14,TRUE)
phase <- c(rep("discr",3),rep("rev",3), rep("discr",4),rep("rev",4))
dat <- data.frame(id,correct,phase)

with id as my subjects (in reality I have a lot more than 2), correct = responses coded as incorrect (0) or correct (1), and the phases Discrimination and Reversal (within-subjects factor). id作为我的主题(实际上我有2个以上), correct =响应编码为不正确(0)或正确(1),以及phases歧视和逆转(主体内因素)。

I want to perform a logistic regression in the form of 我想以形式执行逻辑回归

glm(correct~phase, dat, family="binomial")

later possibly adding additional predictors. 以后可能会添加其他预测变量。 However, since I have a varying amount of data for each subject, I would like to perform glm() seperately for each subject and later compare the coefficients with ANOVA for group effects. 但是,由于每个主题都有不同数量的数据,我想为每个主题分别执行glm() ,然后将系数与ANOVA进行比较以获得组效果。 I would like to do this in a for loop in the form of 我想以for循环的形式执行此操作

for(i in seq_along(dat$id)){
   my_glm[i] <- glm(correct~list,dat[dat$id==i,],family="binomial")
}

but keep receiving the error message 但一直收到错误信息

>Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
 contrasts can be applied only to factors with 2 or more levels.

I have checked my data and there is no factor which contains only one level. 我检查了我的数据,没有任何因素只包含一个级别。 All subjects gave at least one incorrect and one correct response, and all took part in Discrimination and Reversal. 所有受试者都给出了至少一个不正确和一个正确的回答,并且都参与了歧视和逆转。 The function works outside the loop when I specify a particular subject. 当我指定特定主题时,该函数在循环外部工作。

you currently trying to do a glm for each row in of id : 你目前正试图为id每一行做一个glm

I think you want a glm for each id seperately. 我认为你需要一个glm为每个id分开。 Personally , I would go with something like: 就个人而言 ,我会选择以下内容:

library(plyr)

ddply(dat, .(id), function (x){
intercept <- coef(summary(glm(correct~phase,family="binomial",data=x)))[1]
slope     <- coef(summary(glm(correct~phase,family="binomial",data=x)))[2]
c(intercept,slope)                 
                              })

# id         V1            V2
#1  1 -0.6931472  1.386294e+00
#2  2  1.0986123 -6.345448e-16

# here V1 is intercept and V2 is the estimate

Here's an R Base solution: 这是一个R Base解决方案:

> lapply(split(dat, dat$id), function(x) coef(summary(glm(correct~phase,family="binomial",data=x))))
$`1`
                 Estimate Std. Error       z value  Pr(>|z|)
(Intercept) -6.931472e-01   1.224745 -5.659524e-01 0.5714261
phaserev    -3.845925e-16   1.732050 -2.220446e-16 1.0000000

$`2`
                Estimate Std. Error      z value Pr(>|z|)
(Intercept) 3.356998e-16   1.000000 3.356998e-16 1.000000
phaserev    1.098612e+00   1.527524 7.192109e-01 0.472011

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM