Skip to content

lme, nlsList, nlsList.selfStart

9 messages · Patrick Giraudoux, Spencer Graves, Brian Ripley

#
Dear listers,

I am trying to fit a model using nlsList() using alternately a SSfol() 
selfstart function or its developped equivalent formulae.

This preliminary trial works well

mydata<-groupedData(Conc~Tps|Organ,data=mydata)
mymod1<-nls(Conc~SSfol(Dose,Tps,lKe,lKa,lCl),data=mydata)

as well as a developped form:

mymod2<-nls(Conc~Dose * exp(lKe+lKa-lCl) * 
(exp(-exp(lKe)*Tps)-exp(-exp(lKa)*Tps)) /(exp(lKa)-exp(lKe)),
    data=mydata,
    start= c(lKe=-2.77, lKa=-1.41, lCl=-1.13)
    )

However when trying to fit the model with nlsList, I get:

mymod3<-nlsList(Conc~Dose * exp(lKe+lKa-lCl) * 
(exp(-exp(lKe)*Tps)-exp(-exp(lKa)*Tps)) /(exp(lKa)-exp(lKe)),
     data=mydata,
     start= c(lKe=-2.77, lKa=-1.41, lCl=-1.13)
     )
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ

Or specifying  the grouping factor explicitely:

mymod3<-nlsList(Conc~Dose * exp(lKe+lKa-lCl) * 
(exp(-exp(lKe)*Tps)-exp(-exp(lKa)*Tps)) /(exp(lKa)-exp(lKe))|Organ,
     data=mydata,
     start= c(lKe=-2.77, lKa=-1.41, lCl=-1.13)
     )

Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ


I cannot find out why the grouping factor cannot be used (it has the 
same length as the other variables...)

Another strange thing occurs: in the example given in the help of 
nlsList.selfstart, the following command works  well:

 fm1 <- nlsList(SSasympOff, CO2)

However its seemingly equivalent applied to the case above fails:

mymod4<-nlsList(SSfol,data=mydata)
Error in eval(expr, envir, enclos) : object "input" not found
Error in eval(expr, envir, enclos) : object "input" not found
Error in eval(expr, envir, enclos) : object "input" not found


Any hint/suggestion appreciated.

Kind regards,

Patrick Giraudoux
4 days later
#
Regarding the following:  	  	

 > mymod3<-nlsList(Conc~Dose * exp(lKe+lKa-lCl) *
 > (exp(-exp(lKe)*Tps)-exp(-exp(lKa)*Tps)) /(exp(lKa)-exp(lKe)),
 >      data=mydata,
 >      start= c(lKe=-2.77, lKa=-1.41, lCl=-1.13)
 > Error in model.frame(formula, rownames, variables, varnames, extras,
 > extranames,  :
 >         variable lengths differ

	  This example is NOT self contained and is entirely too complicated 
for me to try to replicate it myself in a reasonable period of time.  I 
will therefore ask one short question:  Are all the variable names in 
the "nlsList" call either columns of "mydata" or parameters to be 
estimated and therefore spelled out in "start"?  If I were you, I'd 
check this all very carefully, being especially careful about the 
distinction between "lCl" and "lC1", in particular.

	  If you'd like further help with this, I suggest you try to find the 
simplest possible example that generates problem you don't understand, 
then try to recast that example into one that is completely self 
contained, either a data set in the standard R or nlme distribution  or 
numbers that one can generate with a very few lines of code.  If you use 
random numbers, please "set.seed", to increase your confidence that 
someone else will see what you see.  (And please review the posting 
guide! "www.R-project.org/posting-guide.html".  Doing so may increase 
your chances of getting more useful information more quickly.)

	  spencer graves
Patrick Giraudoux wrote:

            
#
Spencer,

Thanks for the hint. I did not dare to bore people with the full data 
set and though that this kind of error may have been trivial and often 
encountered (so leading to a short answer), even though I did not see 
related messages on the r-help list. I already did the checks suggested 
before posting, and was aware of the possible confusion between 1 and l 
(actually the variable names were not given by myself).

It seems that the trouble comes when the grouping variable "Organ" is 
used. The best (?) I can do  is to dump the data.frame here below.

With this example, one can get exactly the same errors:

mymod2<-nls(Conc~Dose * exp(lKe+lKa-lCl) * 
(exp(-exp(lKe)*Tps)-exp(-exp(lKa)*Tps)) /(exp(lKa)-exp(lKe)),
    data=mydata2,
    start= c(lKe=-2.77, lKa=-1.41, lCl=-1.13)
    )
   
... works well!!!

but we get then:

mymod3<-nlsList(Conc~Dose * exp(lKe+lKa-lCl) * 
(exp(-exp(lKe)*Tps)-exp(-exp(lKa)*Tps)) /(exp(lKa)-exp(lKe)) | Organ,
    data=mydata2,
    start= c(lKe=-2.77, lKa=-1.41, lCl=-1.13)
    )
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ
Error in model.frame(formula, rownames, variables, varnames, extras, 
extranames,  :
        variable lengths differ


 > mymod4<-nlsList(SSfol,data=mydata2)
Error in eval(expr, envir, enclos) : object "input" not found
Error in eval(expr, envir, enclos) : object "input" not found
Error in eval(expr, envir, enclos) : object "input" not found


Sorry and apologise for the inconvenience met,

Kind regards,

Patrick



# data.frame just to copy and past into R

"mydata2" <-
structure(list(Tps = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3,
3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6,
6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9,
9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 14,
14, 14, 14, 14, 17, 17, 17, 17, 17, 20, 20, 20, 20, 25, 28, 29,
50, 50, 50, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136,
1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4,
4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10,
10, 10, 11, 11, 11, 11, 11, 11, 14, 14, 14, 14, 14, 17, 17, 17,
17, 17, 20, 20, 20, 20, 25, 28, 29, 50, 50, 50, 136, 136, 136,
136, 136, 136, 136, 136, 136, 136, 1, 1, 1, 1, 1, 1, 2, 2, 2,
2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8,
8, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11,
11, 11, 14, 14, 14, 14, 14, 17, 17, 17, 17, 17, 20, 20, 20, 20,
25, 28, 29, 50, 50, 50, 136, 136, 136, 136, 136, 136, 136, 136,
136, 136), Conc = c(0, 0, 0, 0, 0, 0, 0, 0.807852503274902, 
0.204371494701886,
0.579222129993907, 0.989062273721619, 0, 1.11728297897571, 
1.41057428121324,
0.888883702851307, 1.259907624008, 1.45753269675829, 1.07077516747401,
0.843279379, 0, 0.763110069196737, 1.11297791434297, 1.10087763997637,
0.946929594501016, 1.33112168, 0.654041755, 0.694167499, 1.289548703,
1.117139864, 0.807196192, 0.720221376552025, 0.560082823, 0.476583438,
2.590855204, 0.51510972, 1.072946887, 0.537999938614396, 0.886684225905255,
0.630178116793598, 1.31534758842196, 1.33333958571746, 0.922032210748255,
0.429930193046174, 1.35881467717335, 0.790045927902363, 1.22484702570724,
0.808104508207897, 1.31185966817903, 1.51837686425553, 1.74105163638734,
1.80365598487402, 1.13240352674377, 1.50086243061644, 2.06355364280445,
0.439350890906039, 1.54692793444949, 1.78758216051046, 1.09043400023239,
0.811328376840514, 0.459192443530981, 0.695333473157298, 0.387995007681174,
0.784627063444921, 1.02282256375842, 0.382687104107726, 0.554290634950242,
0.130420456296453, 0.324194753224919, 0.31106140274139, 0.513473505828888,
0.878620320248701, 1.18404358659996, 0.136926837896477, 0, 0,
0.835588760032974, 0.558617235576616, 1.21002805866839, 0.769381068031404,
1.04514254228094, 0.373251847173678, 0.389005898972802, 0.183141006154896,
0.223596336820146, 0.315526423315647, 0.0930349732768131, 
0.169959185212759,
0.161878841748425, 0, 0.0483041009105874, 0, 0, 0, 0.0777005553478052,
0, 0.153175826795441, 0.0428171049833677, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 26.564295705327, 5.5893744508169, 7.22612934071834,
36.6563989567777, 0, 28.8967184437329, 28.4030370337251, 28.0886043901352,
26.1230935842208, 28.8895673910072, 42.6814210131968, 32.3555695551062,
0.76883326657205, 34.6159136622156, 38.329242204291, 56.4476583636484,
26.6249506083603, 31.3001451026823, 23.7339071829084, 23.3702284599355,
36.669903715038, 44.7377244306005, 31.2079335923023, 32.8613384312272,
29.4259634309146, 45.6112405959009, 48.1231689836687, 55.0037961570027,
32.9822316456421, 20.0382768189682, 26.0986380308655, 28.8915584506145,
28.7949023823068, 30.0278417498425, 58.8089779973569, 20.3602570111197,
29.6269605259023, 28.4404986724604, 30.2165182590977, 19.9204461889074,
31.1019196559556, 30.3847467747055, 36.8726911479995, 51.0618036275519,
23.5408013442579, 36.6948355347593, 27.4753860809429, 24.1341667099646,
27.5411488989643, 35.9021799354022, 19.7417897046158, 31.1403887303244,
46.1743622734049, 34.8235854891765, 22.1714704189293, 33.6805966894274,
35.2814908686112, 42.9767437212852, 38.1264997164547, 5.3651357974451,
42.8990434918385, 25.4908883698364, 25.99649502, 36.4958105490917,
40.8004126550705, 5.36867162116895, 0.00898476265269363, 0, 
27.6810997945798,
28.7918300045713, 45.7577183830352, 35.9276318604787, 34.9717618087238,
29.620354272564, 24.6537513599869, 13.5363982464958, 25.8289073574818,
12.0090406245759, 4.753436805, 11.849214652228, 8.41410147611612,
0, 1.80855352862552, 1.1987530031681, 1.01148025243171, 0.495675369574172,
1.62701127228732, 0, 16.6288242287241, 1.23656061354912, 0.323708776035328,
0, 0.566916625204436, 0, 0, 0, 0, 0, 0, 0, 2.53578781871283,
3.50083667130797, 0, 0.98049572179098, 0, 3.57129673217304, 
2.77298867949388,
2.12302645642669, 4.11923869203499, 4.69069462193674, 2.8698666062651,
2.05079837323067, 0.0602771574448942, 5.96454350250626, 2.26267114439802,
3.06911285674854, 2.04233129537404, 2.62181873844029, 1.51813653072598,
1.46193772981073, 2.69864635755833, 3.44016493913122, 2.50834832469627,
3.48170744166168, 1.00637581555435, 1.67065398473081, 4.18855363095027,
3.39649762611015, 1.72804613460423, 1.40053679329531, 2.37032387724109,
3.19332545080983, 2.49474373894248, 2.17800931288708, 2.7601484443213,
0.91266104095844, 1.93485048639199, 1.19692593420788, 1.79537330666258,
2.14020930767983, 3.0122526724942, 2.81112226980754, 3.54890724398174,
3.01022926452999, 2.38263226710738, 3.53569238341869, 3.47869329713911,
0.679333339820719, 2.4764260756438, 3.82615100065366, 2.20449890383871,
1.371303113329, 1.2427787019995, 1.73319133880954, 0.391268883238408,
1.73610193837913, 2.68494324646718, 1.77065393606844, 1.45079980147062,
0.763775702906329, 0.98566725668627, 0.37838763208699, 0.841811919286804,
1.46436462204795, 1.98409602726, 0.507005887891038, 0.465515668274195,
0, 1.873365675227, 1.69023864630648, 2.65530855919137, 2.34392199908302,
1.61917643594837, 1.05165934333345, 0.564642823436471, 0.121621029620328,
0.515007625737071, 0.524345809084086, 0.130898614090571, 0.332427740242623,
0.110214989555118, 0, 0.128642193589, 0.119407067173878, 0.128926224027295,
0.0622331866694357, 0.215645168287442, 0, 0.859343941945178,
0.0500810300696456, 0, 0, 0.0628746592609754), Organ = 
structure(as.integer(c(1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2)), .Label = c("Carc", "TD", "Foie"), class = c("ordered",
"factor"))), .Names = c("Tps", "Conc", "Organ"), row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35",
"36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46",
"47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57",
"58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68",
"69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90",
"91", "92", "93", "94", "95", "96", "97", "98", "99", "100",
"101", "102", "103", "104", "105", "106", "107", "108", "109",
"110", "111", "112", "113", "114", "115", "116", "117", "118",
"119", "120", "121", "122", "123", "124", "125", "126", "127",
"128", "129", "130", "131", "132", "133", "134", "135", "136",
"137", "138", "139", "140", "141", "142", "143", "144", "145",
"146", "147", "148", "149", "150", "151", "152", "153", "154",
"155", "156", "157", "158", "159", "160", "161", "162", "163",
"164", "165", "166", "167", "168", "169", "170", "171", "172",
"173", "174", "175", "176", "177", "178", "179", "180", "181",
"182", "183", "184", "185", "186", "187", "188", "189", "190",
"191", "192", "193", "194", "195", "196", "197", "198", "199",
"200", "201", "202", "203", "204", "205", "206", "207", "208",
"209", "210", "211", "212", "213", "214", "215", "216", "217",
"218", "219", "220", "221", "222", "223", "224", "225", "226",
"227", "228", "229", "230", "231", "232", "233", "234", "235",
"236", "237", "238", "239", "240", "241", "242", "243", "244",
"245", "246", "247", "248", "249", "250", "251", "252", "253",
"254", "255", "256", "257", "258", "259", "260", "261", "262",
"263", "264", "265", "266", "267", "268", "269", "270", "271",
"272", "273", "274", "275", "276", "277", "278", "279", "280",
"281", "282", "283", "284", "285", "286", "287", "288", "289",
"290", "291", "292", "293", "294", "295", "296", "297", "298",
"299", "300"), class = c("nfnGroupedData", "nfGroupedData", "groupedData",
"data.frame"), formula = quote(Conc ~ Tps | Organ), FUN = function (x)
max(x, na.rm = TRUE), order.groups = TRUE)




Spencer Graves a ??crit :
#
We don't have Dose, and I think that is where the error lies.  If Dose 
were part of mydata2, this is likely to work, but otherwise it is Dose 
which has the wrong length.

?nlsList says

     data: a data frame in which to interpret the variables named in
           'model'.

and it means it: you must get all the variables from there.
On Fri, 17 Feb 2006, Patrick Giraudoux wrote:

            

  
    
#
Well, right, Dose was indeed in the global environment and not in the 
data.frame. Changing it with

mydata2$Dose<-100 # the real dose at the beginning of the experiment

improves the thing in a sense... but I face a new error:

mymod3<-nlsList(Conc+1 ~ Dose * exp(lKe+lKa-lCl) * 
(exp(-exp(lKe)*Tps)-exp(-exp(lKa)*Tps)) /(exp(lKa)-exp(lKe)) | Organ,
data=mydata2,
start= c(lKe=-2.77, lKa=-1.41, lCl=-1.13)
)

Error in exp(-exp(lKa) * Conc + 1 ~ Tps) :
        Non-numeric argument to mathematical function
Error in exp(-exp(lKa) * Conc + 1 ~ Tps) :
        Non-numeric argument to mathematical function
Error in exp(-exp(lKa) * Conc + 1 ~ Tps) :
        Non-numeric argument to mathematical function

I have checked the variables in the data.frame with:

 > sapply(mydata2,is.factor)
  Tps  Conc Organ  Dose
FALSE FALSE  TRUE FALSE
 > sapply(mydata2,is.character)
  Tps  Conc Organ  Dose
FALSE FALSE FALSE FALSE

So everything looks OK on this side...

Furthermore, I have still this "old" error intact:

 > mymod4<-nlsList(SSfol,data=mydata2)
Error in eval(expr, envir, enclos) : object "input" not found
Error in eval(expr, envir, enclos) : object "input" not found
Error in eval(expr, envir, enclos) : object "input" not found


I am really sorry for calling help and boring everybody on this likely 
trivial issue (this looks like asking a community to participate to 
debogging line by line, a shame!!!), but I must admit that I am really 
lost...

Thanks for your concern,

Kind regards,

Patrick


Prof Brian Ripley a ??crit :
#
Have you tried making mydata2$Dose the same length as everything else?

	  spencer graves
Patrick Giraudoux wrote:

            
#
On Fri, 17 Feb 2006, Spencer Graves wrote:

            
[<- does that for you: all columns in a data frame must be the same 
length.  Read on in your inbox for the solution.

  
    
#
Dear Prof. Ripley:

	  1.  That's great.  I seem to recall having problems with something 
like that when using a commercial *-Plus product not unlike R.  Version 
6.2 of this other software just produced the following for me:

 > tstDF <- data.frame(a = 1:3)
 > tstDF$b <- 2
 > sapply(tstDF, length)
  a b
  3 1

	  From R2.2.1, I get "3 3" for this example, as you noted.  Once more, 
I am doubly in your debt, sir, both for helping to make R what it is 
today AND for helping to educate me on its capabilities.

	  2.  Alas, my cerebral cortex produced "Nil pointer reference" in 
trying to parse your other comment, "Read on in your inbox for the 
solution."  Perhaps in the fullness of time, I shall be enlightened.

	  Thanks again,
	  spencer graves
Prof Brian Ripley wrote:

            
#
This is a bug in nlsList.formula, where the author forgot that deparsing 
can spread over multiple lines.  Here is a patch:

--- nlsList.R   (revision 2298)
+++ nlsList.R   (working copy)
@@ -60,8 +60,9 @@
      } else if (length(level) > 1) {
        stop("Multiple levels not allowed")
      }
-    model <- eval(parse(text = paste(deparse(model[[2]]),
-                        deparse(getCovariateFormula(model)[[2]]), sep = "~")))
+    model <- eval(parse(text = paste(paste(deparse(model[[2]]), collapse=" "),
+                        paste(deparse(getCovariateFormula(model)[[2]]), collapse=" "),
+                       sep = "~")))
      groups <- getGroups(data, form = grpForm, level = level)[drop = TRUE]
    }
    if (is.null(start) && is.null(attr(data, "parameters"))) {

which will probably not survive emailing but should show you what the 
change is.
On Fri, 17 Feb 2006, Patrick Giraudoux wrote: