FloLe
FloLe

Reputation: 41

Error while using the weights option in nlme in r

Sorry this is crossposting from https://stats.stackexchange.com/questions/593717/nlme-regression-with-weights-syntax-in-r, but I thought it might be more appropriate to post it here.

I am trying to fit a power curve to model some observations in an nlme. However, I know some observations to be less reliable than others (reliability of each OBSID reflected in the WEIV in the dummy data), relatively independent of variance, and I quantified this beforehand and wish to include it as weights in my model. Moreover, I know a part of my variance is correlated with my independent variable so I cannot use directly the variance as weights.

This is my model:

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))

This is some sample dummy data (I know it is not a great fit but it's fake data anyway) : https://github.com/FlorianLeprevost/dummydata/blob/main/testdummy10.csv

This runs well without the "weights" argument, but when I add it I get this error and I am not sure why because I believe it is the correct syntax:

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

Thanks in advance!

Upvotes: 2

Views: 465

Answers (1)

Ben Bolker
Ben Bolker

Reputation: 226881

This looks like a very long-standing bug in nlme. I have a patched version on Github, which you can install via remotes::install_github() as below ...

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

Upvotes: 4

Related Questions