1

Take the example of mtcars in R: I want to make a non linear regression for the relationship between mpg and disp with:

nls(data=mtcars, mpg ~ A * disp^2 + B * disp + C, start=list(A=1,B=1,C=1))

But, I need to analyse the relationship for every combination of cyl, gear and carb.

I tried to do this :

 nlsList(data=mtcars, mpg ~ A * disp^2 + B * disp + C | cyl * gear * carb, 
                                              start=list(A=1,B=1,C=1)) 

but I have always errors. The final objective is to plot datas with regression with facets of cyl, gear and carb. Could you help me?

user20650
  • 24,654
  • 5
  • 56
  • 91
Yang
  • 21
  • 1
  • 1 of the facet will be the data with regression for subset(cyl==6 & gear==4 & carb==4). Another facet will be data and regression for subset(cyl==4 & gear==4 & carb==4) etc. – Yang Dec 16 '16 at 16:23
  • 1
    Why nls? your regression is linear in the coefficients – user20650 Dec 16 '16 at 16:24
  • I would drop the nls and do linear with I(disp^2) – Derek Corcoran Dec 16 '16 at 16:27
  • Also as for running the regression for each level, `with(mtcars, table(interaction(cyl, gear, carb, drop=TRUE)))` would suggest there are not enough points to estimate every grouping. [but choosing groups with enough data, this should work `mtcars$f = with(mtcars, interaction(am,vs)) ; nlsList(data=mtcars, mpg ~ A * disp^2 + B * disp + C | f, start=list(A=1,B=1,C=1))` ] – user20650 Dec 16 '16 at 16:28
  • That's a bad choice of polynomial given the relationship of the variables you're considering. – Señor O Dec 16 '16 at 16:56

0 Answers0