R错误:“类型'闭包'的对象不是子集”

时间:2017-04-27 14:16:48

标签: r object closures subset

运行代码时收到以下消息。我正在尝试进行调解分析。

“新的$ meddepression错误:'闭包'类型的对象不是子集”

我是编程和R的新手。任何帮助或建议都将不胜感激!以下是我的代码。

doEffectDecomp = function(d)
{
  d$actlim2Temp=d$actlim2
  mdepression = glm(depression ~ actlim2Temp, family=binomial(), data=d)

  d1 = d2 = d
  d1$meddepression = d1$actlim2
  d2$meddepression = !d2$actlim2
  newd = rbind(d1,d2)

  newd$actlim2Temp = newd$actlim2
  w = predict(mdepression, newdata = newd, type='response')
  direct = ifelse(newd$depression, w, 1-w)

  newd$actlim2Temp <-  new$meddepression
  w = predict(mdepression, newdata = newd, type='response')
  indirect = ifelse(newd$depression, w, 1-w)
  newd$Wdepression = indirect/direct

  newd$W = newd$Wdepression
  cox=coxph(Surv(days_in_study, censor) ~ meddepression + actlim2, weight=W, 
 data=newd)

  TE=exp(sum(coef(cox)[c('actlim2', 'meddepression')]))
  DE=exp(unname(coef(cox)['actlim2']))
  IE=exp(sum(coef(cox)[c('meddepression')]))

  PM = log(IE)/log(TE)

  return(c(exp(coef(cox)), TE=TE, DE=DE, IE=IE, PM=PM))
}

doEffectDecomp(d)

先谢谢你的帮助,

SIMRAN

1 个答案:

答案 0 :(得分:1)

代码中有一个拼写错误,我错过了&#34; d&#34;。 new$meddepression应该已阅读newd$meddepression

非常感谢您的所有投入,非常感谢。