object 在 R 中导出模型摘要 table 时未找到

object not found when export model summary table in R

我正在尝试循环将模型摘要数据导出到 excel。我需要导出 2 个变量的系数(变量 gainloss)并且我已经成功写入了截距和变量 1 的系数,但是 R 告诉我第三个变量的 object未找到。

我的代码:运行参加人数PID的模型,PIDsPID的名单。

for (i in 1: length(PIDs)) {
   subject<-df[df$PID == PIDs[i],]
   myModel <- glm(gamble~Gain + Loss, data = subject, family=binomial)
   summ <- summary(myModel)

   #save results
   ID[i] <- subject$PID
   intercept_coef[i]<-summ$coefficients[1,1]
   gain_coef[i]<-summ$coefficients[2,1]
   loss_coef[i]<-summ$coefficients[3,1]

}

系数摘要 table 如下所示,我注意到 table 已关闭,因为 headers 不对应于每一列。可能是这个问题?

 Estimate   Std. Error     z value  Pr(>|z|)
 (Intercept) 13.4214135 3353.1375049 0.004002643 0.9968064
 Gain         0.2929938    0.1635471 1.791494960 0.0732139
 Loss         8.3144005 1619.8731372 0.005132748 0.9959047

错误:

occurrednumber of items to replace is not a multiple of replacement length 

Error in loss_coef[i] <- summ$coefficients[3, 1] : 
 object 'loss_coef' not found

这里有什么问题?我可以得到 InterceptGain 都很好。

谢谢!

有没有可能你没有初始化 losscoef(或者 loss_coef,也许检查输入)?

我们在循环中存储值之前需要初始化变量。

待初始化:

ID <- 0
intercept_coef <- 0
gain_coef <- 0
loss_coef <- 0

#loop