【问题标题】:Error while using the weights option in nlme in r在 r 的 nlme 中使用权重选项时出错
【发布时间】:2022-11-12 10:04:39
【问题描述】:

抱歉,这是来自https://stats.stackexchange.com/questions/593717/nlme-regression-with-weights-syntax-in-r 的交叉发布,但我认为将其发布在此处可能更合适。

我正在尝试拟合功率曲线来模拟nlme 中的一些观察结果。但是,我知道一些观察结果不如其他观察结果可靠(每个OBSID 的可靠性反映在虚拟数据中的WEIV 中),相对独立于方差,我事先对此进行了量化,并希望将其作为权重包含在我的模型。此外,我知道我的一部分方差与我的自变量相关,所以我不能直接使用方差作为权重。

这是我的模型:

coeffs_start = lm(log(DEPV)~log(INDV), filter(testdummy10,DEPV!=0))$coefficients

nlme_fit <- nlme(DEPV ~ a*INDV^b, 
                      data = testdummy10,
                      fixed=a+b~ 1, 
                      random = a~ 1, 
                      groups = ~ PARTID, 
                      start = c(a=exp(coeffs_start[1]), b=coeffs_start[2]),
                      verbose = F,
                      method="REML",
                      weights=varFixed(~WEIV))

这是一些示例虚拟数据(我知道它不是很合适,但无论如何它都是假数据):https://github.com/FlorianLeprevost/dummydata/blob/main/testdummy10.csv

这在没有“权重”参数的情况下运行良好,但是当我添加它时,我得到了这个错误,我不知道为什么,因为我相信它是正确的语法:

Error in recalc.varFunc(object[[i]], conLin) : 
dims [product 52] do not match the length of object [220]
In addition: Warning message:
In conLin$Xy * varWeights(object) :
longer object length is not a multiple of shorter object length

提前致谢!

【问题讨论】:

    标签: r mixed-models weighted nlme


    【解决方案1】:

    这看起来像 nlme 中的 very long-standing bug。我在 Github 上有一个补丁版本,您可以通过remotes::install_github() 安装它,如下所示...

    remotes::install_github("bbolker/nlme")
    testdummy10 <- read.csv("testdummy10.csv") |> subset(DEPV>0 & INDV>0)
    coeffs_start <- coef(lm(log(DEPV)~log(INDV), testdummy10))
    library(nlme)
    nlme_fit <- nlme(DEPV ~ a*INDV^b, 
                          data = testdummy10,
                          fixed=a+b~ 1, 
                          random = a~ 1, 
                          groups = ~ PARTID, 
                          start = c(a=exp(coeffs_start[1]),
                                    b=coeffs_start[2]),
                          verbose = FALSE,
                          method="REML",
                          weights=varFixed(~WEIV))
    packageVersion("nlme") ## 3.1.160.9000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 1970-01-01
      相关资源
      最近更新 更多