1

I have successfully fitted a non-linear mixed model with the nlme() function of the nlme package, but in trying to improve said model by including a weights argument, I get nothing but errors I could not interpret, and I have therefore been unable to pinpoint the exact source of my problem.

I used the following code for the non-weighted model:

fctmod<-function(x,y,z){
  resultat<-(1+x*((z-mean(z))/mean(z))+y*((z-mean(z))/mean(z))^2)
  return(resultat)
}

mod<- nlme(INC ~ (a*(CUMU)^(b) - c*CUMU)*
                              fctmod(d,e,tpt)*
                              fctmod(f,g,rdan),
                            fixed = a + b + c + d + e + f + g ~ 1,
                            random = a ~ 1,
                            start = list(fixed = c(a = 1, b = 0.1, c = 0.1, d=0, e=0, f=0, g=0)),
                            groups = ~ ID,
                            data = dataEg)

An this is the weighted model:

mod<- nlme(INC ~ (a*(CUMU)^(b) - c*CUMU)*
                                  fctmod(d,e,tpt)*
                                  fctmod(f,g,rdan),
                                fixed = a + b + c + d + e + f + g ~ 1,
                                random = a ~ 1,
                                start = list(fixed = c(a = 1, b = 0.1, c = 0.1, d=0, e=0, f=0, g=0)),
                                groups = ~ ID,
                                data = dataEg,
                                weights=varFixed(~CUMU))

I get the following error:

Error in conLin$Xy * varWeights(object) : non-conformable arrays

I understand this has to do with a product of a matrix and a vector, one of them not being the same dimension so that the operation can be successfully carried out. However I cannot figure out how to check the dimensions of those objects, or even access them and try to solve the problem.

If I change the varFixed() argument to a varIdent one, the message changes but is basically telling me the same thing, as far as I can gather on my own:

[...]
weights=varIdent(~CUMU))

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

Any help on the matter would be greatly appreciated!

Here is a sample of my data:

ID           INC        CUMU           tpt        rdan
1801    50.87048    5.09E+01    0.37017968  8.003372945
1801    250.7865    3.02E+02    -0.93841232 -0.284327055
1801    216.115     5.18E+02    -1.97075332 -11.63592706
1801    17.49655    5.35E+02    -0.57010332 -13.39202706
1801    64.32044    6.00E+02    0.48817968  -14.29292706
1801    93.44014    6.93E+02    0.74130468  -11.03592706
1801    323.8984    1.02E+03    0.04017168  -6.111227055
1801    340.4834    1.36E+03    -0.71888732 12.76487295
1801    538.6622    1.90E+03    -0.78026232 -4.722627055
1801    836.1861    2.73E+03    -2.75356232 12.19917295
1801    993.915     3.73E+03    0.84063768  7.208372945
1801    1350.227    5.08E+03    -0.16196232 2.234672945
1801    1704.331    6.78E+03    2.06117168  13.33377295
1801    1497.009    8.28E+03    2.43130468  -7.332427055
1801    2343.468    1.06E+04    0.26309668  8.642372945
1801    2694.576    1.33E+04    -2.19830332 8.492172945
1801    1929.02     1.52E+04    -2.44949532 -2.072127055
1801    2224.844    1.75E+04    -3.29004532 7.834272945
1801    2481.327    2.00E+04    -1.31985332 0.178472945
1801    4105.118    2.41E+04    -2.53618732 15.21767295
1801    2861.757    2.69E+04    -2.98210332 -6.081427055
1801    3757.36     3.07E+04    -1.94142032 7.024772945
1801    4883.06     3.56E+04    -3.02842032 7.071672945
1801    4492.688    4.01E+04    -2.61557032 13.03677295
1801    4421.439    4.45E+04    -4.64508732 15.32817295
1801    4291.048    4.88E+04    0.08944668  -23.36592706
1801    5872.25     5.46E+04    -2.48509532 -11.84332706
1801    4726.314    5.94E+04    -1.97220332 -2.063327055
1801    2258.114    6.16E+04    -2.40127032 0.703472945
1801    3064.254    6.47E+04    0.06097168  2.285672945
1801    5209.305    6.99E+04    -3.75766232 3.212772945
1801    5789.695    7.57E+04    -1.10404532 -2.296027055
1801    5278.369    8.10E+04    -1.44937832 -10.85002706
1801    5776.588    8.67E+04    -0.70907832 0.248972945
1801    5965.722    9.27E+04    -2.72012832 -7.313327055
1801    5381.872    9.81E+04    -0.98598732 -20.67842706
1801    3894.213    1.02E+05    -0.57961232 -8.999627055
1801    5239.56     1.07E+05    -2.10707032 6.151372945
1801    7147.558    1.14E+05    -2.53260332 -8.591527055
1801    8615.51     1.23E+05    -0.24122032 -15.93492706
1801    6256.669    1.29E+05    0.37691268  -23.57042706
1801    6037.065    1.35E+05    -2.67912032 -8.294527055
1801    6629.371    1.42E+05    -1.11789532 -11.14572706
1801    6476.749    1.48E+05    -0.51492832 -0.753727055
1801    6581.073    1.55E+05    -2.93513732 7.555172945
1801    6969.042    1.62E+05    -2.23327032 -9.324827055
1801    4403.415    1.66E+05    -1.18624532 -9.837027055
1801    5096.871    1.71E+05    -2.07377032 -6.567227055
1801    5027.219    1.76E+05    -1.05444532 5.552872945
1801    4732.223    1.81E+05    -1.79232832 7.504872945
1801    6492.61     1.88E+05    0.73975468  -14.91762706
1801    7186.891    1.95E+05    0.44052968  0.669772945
1801    5676.406    2.01E+05    0.10896268  -19.10652706
1808    2.653267    2.65E+00    -2.06587032 -13.18752706
1808    12.81145    1.55E+01    -0.75933732 -13.96612706
1808    25.68672    4.12E+01    0.27944668  -21.30192706
1808    37.65382    7.88E+01    0.61759668  -17.10762706
1808    71.66865    1.50E+02    -0.18621232 -9.912727055
1808    158.0589    3.09E+02    -0.86542032 8.144972945
1808    156.2768    4.65E+02    -0.86728732 -7.570827055
1808    237.5628    7.02E+02    -2.82411232 6.361572945
1808    184.6583    8.87E+02    0.67944668  3.986372945
1808    219.4781    1.11E+03    -0.26756232 -2.530227055
1808    386.6515    1.49E+03    2.29544668  15.52257295
1808    376.0046    1.87E+03    1.92659668  -9.689227055
1808    455.2105    2.32E+03    -0.47513732 6.732472945
1808    505.2185    2.83E+03    -2.13908732 6.763472945
1808    578.1172    3.41E+03    -2.33707032 -6.411127055
1808    570.791     3.98E+03    -3.25053732 5.383972945
1808    645.5343    4.62E+03    -1.38504532 -1.277927055
1808    835.1992    5.46E+03    -2.60448732 14.52987295
1808    719.335     6.18E+03    -3.06864532 -6.788227055
1808    803.2229    6.98E+03    -2.12332832 2.891972945
1808    1017.481    8.00E+03    -2.99362832 7.325372945
1808    1039.884    9.04E+03    -2.54442032 7.693072945
1808    1103.892    1.01E+04    -4.66533732 12.84257295
1808    1128.305    1.13E+04    -0.08787832 -27.45782706
1808    1203.774    1.25E+04    -2.51752032 -12.42242706
1808    1416.5      1.39E+04    -1.78362832 -3.775227055
1808    1149.501    1.50E+04    -2.73605332 -1.345627055
1808    1342.186    1.64E+04    0.30392968  1.708072945
1808    1692.817    1.81E+04    -3.53136232 -0.090827055
1808    1683.827    1.98E+04    -0.48927832 -2.944427055
1808    1754.922    2.15E+04    -0.58983732 -17.19532706
1808    1701.573    2.32E+04    -0.64008732 -6.623727055
1808    2075.097    2.53E+04    -2.37770332 -7.776127055
1808    1962.803    2.73E+04    -0.45017832 -21.55882706
1808    2167.067    2.94E+04    -0.30337032 -16.21162706
1808    2191.808    3.16E+04    -1.36269532 0.789772945
1808    2540.392    3.42E+04    -2.69541232 -11.72912706
1808    2449.676    3.66E+04    -0.22992832 -26.32262706
1808    2176.656    3.88E+04    0.88247168  -25.90452706
1808    1559.047    4.03E+04    -2.49282032 -10.71292706
1808    2051.73     4.24E+04    -0.94173732 -13.36192706
1808    2353.799    4.47E+04    -0.06031232 -4.518227055
1808    2339.927    4.71E+04    -2.61190332 -3.615227055
1808    2542.576    4.96E+04    -1.90476232 -17.21042706
1808    1851.814    5.15E+04    -1.00652032 -14.89022706
1808    1799.821    5.33E+04    -1.66852032 -12.93212706
1808    1751.032    5.50E+04    -0.76849532 -2.244227055
1808    2068.087    5.71E+04    -1.28739532 -13.79412706
1808    2520.929    5.96E+04    1.74033768  -40.11402706
1808    2524.768    6.21E+04    0.31034668  -15.37072706
1808    2245.268    6.44E+04    -0.14590332 -26.91642706
1822    243.2774    2.43E+02    0.71213768  0.361472945
1822    295.3962    5.39E+02    1.24134668  9.947772945
1822    422.28      9.61E+02    -0.99325332 9.723072945
1822    635.918     1.60E+03    -0.84666232 4.894672945
1822    684.5842    2.28E+03    -0.15511232 2.767772945
1822    740.4322    3.02E+03    0.01338768  13.02837295
1822    877.1983    3.90E+03    -0.76973732 11.24047295
1822    1102.142    5.00E+03    -0.06317832 3.155572945
1822    1114.751    6.12E+03    -0.01521232 10.34347295
1822    1498.066    7.61E+03    0.93285468  -4.146727055
1822    1274.27     8.89E+03    -0.99130332 11.08257295
1822    1295.652    1.02E+04    -0.74763732 13.15277295
1822    1456.543    1.16E+04    0.45261268  13.63757295
1822    1378.481    1.30E+04    -2.81537832 11.68637295
1822    1427.113    1.44E+04    0.84238768  -5.599227055
1822    1501.924    1.59E+04    -2.36837832 1.537272945
1822    2055.408    1.80E+04    -1.26663732 6.664072945
1822    2243.367    2.02E+04    -1.17715332 -5.906827055
1822    2117.086    2.24E+04    -1.52857832 -1.450027055
1822    2319.644    2.47E+04    -1.05047032 11.79147295
1822    1801.73     2.65E+04    -0.03891232 4.133472945
1822    2361.298    2.88E+04    -2.83277832 16.39077295
1822    2195.976    3.10E+04    -7.93014532 38.54507295
1822    2076.264    3.31E+04    -4.72564532 13.35337295
1822    1933.169    3.51E+04    -6.25872032 11.54057295
1822    1865.861    3.69E+04    -7.17471232 26.36887295
1822    1950.644    3.89E+04    -4.29537832 0.003172945
1822    1923.462    4.08E+04    -3.98904532 3.892672945
1822    1626.137    4.24E+04    -3.51962032 15.16987295
1822    2067.186    4.45E+04    -3.14743732 1.357472945
1822    2397.914    4.69E+04    -1.64246232 -13.69462706
1822    2238.5      4.91E+04    -6.21394532 3.198272945
1822    2409.343    5.15E+04    -6.55646232 27.01797295
1822    2037.574    5.36E+04    -5.08392832 0.779972945
1822    2410.954    5.60E+04    -6.51442832 -3.198127055
1822    1831.59     5.78E+04    -6.70302032 6.626672945
1822    2113.152    5.99E+04    -3.89295332 16.36777295
1822    1767.248    6.17E+04    -5.83889532 -10.21052706
1822    2225.575    6.39E+04    -4.79626232 -2.438027055
1822    2007.595    6.59E+04    -2.00391232 -4.103427055
1822    2460.827    6.84E+04    -0.25860332 -5.483427055
1822    2169.772    7.06E+04    -0.82212032 -0.431927055
1822    1939.393    7.25E+04    -4.11975332 11.14317295
1822    1839.968    7.43E+04    -1.11702832 -10.03882706
1822    1911.487    7.62E+04    -0.57976232 -15.99062706
1822    2103.234    7.83E+04    -2.62102032 -5.665827055
1822    1894.019    8.02E+04    -2.52459532 8.237672945
1822    1900.976    8.21E+04    -1.05139532 3.891872945
1822    1923.652    8.41E+04    -0.54103732 -3.840127055
1822    2189.481    8.63E+04    -3.15074532 7.494872945
1822    3133.193    8.94E+04    -1.32373732 -3.874627055
1822    2840.912    9.22E+04    -0.15873732 -6.964527055
1822    2453.992    9.47E+04    -0.17384532 -4.425127055
1822    2116.707    9.68E+04    0.15636268  -12.93612706
1822    2572.732    9.94E+04    -0.95784532 -8.085427055
1822    1791.071    1.01E+05    -1.23557832 -6.776027055
1822    2640.578    1.04E+05    -2.54494532 -3.026927055
1822    3905.958    1.08E+05    0.08290468  -10.25792706
1822    3030.905    1.11E+05    -1.18086232 6.724372945
1822    2180.843    1.13E+05    -0.41478732 9.966672945
1822    2949.961    1.16E+05    -0.36606232 -12.79502706
1822    2123.581    1.18E+05    -3.25817832 1.413172945
1822    2190.637    1.20E+05    -1.54525332 -5.029327055
1822    2568.083    1.23E+05    -2.23234532 -4.825927055
1822    2310.152    1.25E+05    -3.18713732 7.569272945
1822    2455.807    1.28E+05    -1.74045332 -6.766027055
1822    2393.918    1.30E+05    -3.97016232 10.25977295
1822    2752.022    1.33E+05    -4.17411232 -5.978727055
1822    3097.308    1.36E+05    -3.83764532 -0.768227055
1822    2130.477    1.38E+05    -2.44955332 -2.951827055
1822    2233.559    1.40E+05    -2.00573732 9.120172945
1822    2395.441    1.43E+05    -4.33248732 8.190272945
1822    2584.954    1.45E+05    -0.70032032 -12.86992706
1822    1680.365    1.47E+05    -2.97705332 -0.922627055
1822    1928.463    1.49E+05    -3.69305332 6.553472945
1822    975.4539    1.50E+05    -4.79113732 11.17317295
1822    1595.284    1.51E+05    -1.38713732 20.42707295
1822    2313.053    1.54E+05    -4.95961232 10.84617295
1822    2299.346    1.56E+05    -1.97567832 1.962672945
1822    2084.111    1.58E+05    -1.78690332 -13.97662706
1822    1762.053    1.60E+05    -2.08870332 -5.256527055
1822    1498.806    1.61E+05    -3.47517032 -6.847927055
1822    1441.977    1.63E+05    -0.93724532 -6.746327055
1822    1250.551    1.64E+05    -1.41612832 -13.15312706
1822    1797.025    1.66E+05    -1.69650332 -2.344927055
1822    2195.586    1.68E+05    -3.24574532 -11.43942706
1822    1946.757    1.70E+05    -1.57494532 -17.32502706
1822    1573.032    1.71E+05    -0.61749532 -18.26172706
1822    1296.121    1.73E+05    -3.86612832 -1.943527055
1822    1514.312    1.74E+05    -2.15214532 -3.785027055
1822    1970.279    1.76E+05    -1.37000332 0.107872945
1822    1792.646    1.78E+05    -3.88349532 2.305472945
1822    2198.977    1.80E+05    -3.14851232 -17.17662706
1822    1545.574    1.82E+05    -1.78811232 -9.729927055
1822    1535.827    1.83E+05    -3.13195332 -7.044927055
1822    1328.625    1.85E+05    -2.12005332 -5.122827055
1822    1402.548    1.86E+05    -2.12696232 -5.865827055
1822    1505.996    1.88E+05    0.31811268  -34.11432706
1822    1535.659    1.89E+05    -0.31787832 -31.05032706
1822    1391.383    1.90E+05    -1.57599532 -34.63242706

Thanks in advance.

Chris. Z
  • 365
  • 1
  • 7
  • 17
  • 1
    Please include sample data to make this problem [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It doesn't have to be a complete data set, just enough so we can run the code and get the same error as you. That will help people debug your problem. Right now we have no idea what data you are feeding into the function – MrFlick Jul 21 '14 at 16:06
  • I've added the sample, sorry about this! – Chris. Z Jul 21 '14 at 17:16
  • Maybe I'm misreading the help page, but I think you want `weights='~CUMU'` and because the argument is a formula it's passed to `varFixed` . – Carl Witthoft Jul 21 '14 at 18:34
  • It does not improve the outcome, as you said it does pass it to a varClass function, so I get the same error message... – Chris. Z Jul 21 '14 at 18:38
  • I am having the very same problem with the weights in gls. Did you ever figure it out? var_model <- gls(Y ~ fmean+fd10, weights=varPower(form = ~fmean)) Error in conLin$Xy * varWeights(object) : non-conformable arrays – J Kelly Feb 24 '15 at 14:18
  • Sorry but no, could not find a solution, and I moved on from the gls models to another class... Hope you'll have some luck elsewhere! – Chris. Z Mar 01 '15 at 02:59
  • There is a potential solution at https://stackoverflow.com/questions/74235304/error-while-using-the-weights-option-in-nlme-in-r, although with the data sample given here I get a "step halving factor reduced below minimum" error instead ... – Ben Bolker Nov 12 '22 at 01:57

2 Answers2

1

I had this same problem while using the lme function, what I found was that the weight variable I was using was not a vector.

When it was changed to a vector using as.vector() or c() the problem went away.

enderland
  • 13,825
  • 17
  • 98
  • 152
0

There appears to be a bug with the nlme::nlme function and the varFixed weights.

One solution is to combined the fixed weights with another set of weights equal to 1:

weights=varComb(varExp(fixed=0.000000001), varFixed(~CUMU)))

this works because:

all.equal(varWeights(Initialize(varExp(fixed=0.000000001), dataEg)), rep(1, nrow(dataEg)))

or

mod2 <- nlme(INC ~ (a*(CUMU)^(b) - c*CUMU)*fctmod(d,e,tpt)*fctmod(f,g,rdan),
             fixed = a + b + c + d + e + f + g ~ 1,
             random = a ~ 1,
             start = list(fixed = c(a = 1, b = 0.1, c = 0.1, d=0, e=0, f=0, g=0)),
             groups = ~ ID,
             data = dataEg,
             weights=varExp(fixed=0.000000001))

all.equal(varWeights(mod2$modelStruct$varStruct), rep(1, nrow(dataEg)))
bensentropy
  • 1,293
  • 13
  • 17