Non linear regression with groups by nlsList in R

772 views Asked by At

Take the example of mtcars in R: I want to make a non linear regression for the relationship between mpg and disp for each cylinder and each group of vs. In my case I have a dataframe much bigger than mtcars. I know there is not enough data. But to simplify the question, I will always use mtcars but suppose that we have sufficiant data to make multiple group non-linear regression and the formula is tanh type. I want to make a tanh type regression for each cyl and each group of vs, so I tried:

mtcars2 <-list()
reg2 <-list()
attach(mtcars)
for (i in levels(cyl))
{
    mtcars2[[i]] <-
        groupedData( mpg ~ disp | vs,
             data = subset(mtcars,cyl==i))
    reg2[[i]] <- nlsList(mpg ~ A + B * tanh(disp/C), data=mtcars2[[i]],
                   start = list(A=1, B=1, C=0.5), na.action = na.omit)
}

I want to do scatter plot by doing plot(mycars2[[i]]) with the tanh regression. Unfortunately I have always errors like:

dim(x) must have positive length

Could you help me? Can I use directly

nlme() or other functions to do regressions and xyplot results?

Thank you very much!

0

There are 0 answers