【问题标题】:R Error: "object of type 'closure' is not subsettable"R错误:“'closure'类型的对象不是子集”
【发布时间】:2017-04-27 14:16:47
【问题描述】:

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

“new$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)

提前感谢您的帮助,

西姆兰

【问题讨论】:

  • 您应该考虑记录/评论您的代码。这有点难以阅读/理解。
  • 我怀疑缺少“d”以及未能提供“sessionInfo()”的错字。错误消息说解释器试图获取它认为是函数的 S4 对象。
  • 您正在尝试子集new 函数。
  • 非常感谢您的所有意见。事实证明我错过了一个“d”,就像你怀疑的那样 42-。这是救命稻草,再次感谢!

标签: r object closures subset


【解决方案1】:

代码中有错字,我缺少一个“d”。 new$meddepression 应该读过newd$meddepression

非常感谢您的所有意见,非常感谢。

【讨论】:

  • 错字也是我的问题。
猜你喜欢
  • 1970-01-01
  • 2015-06-18
  • 2017-06-01
  • 1970-01-01
  • 2016-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-01
相关资源
最近更新 更多