0

I have been learning how to plot graphs and curves in R, and have used the plot() and curve() functions with a set of data to plot both the data and the curve. However, I don't believe I am using the most efficient methods.

What I have done is set up 2 vectors from a data.frame that and then I assigned an object (call it o) to the nls() function like o<-nls(y~I(a*x^3)+I(b*x^2)+I(c*x)+d). I could get all of the coefficient values for o (a,b,c,d) by calling o. I can then plug approximations for those values into the curve() function after plotting the points. That works, but I was wondering if there is any way to plug o directly into the curve function so I don't have to retype each coefficient?

d.b
  • 32,245
  • 6
  • 36
  • 77
DeCodened
  • 59
  • 2
  • 8
  • 1
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input so that we can test possible solutions. Chances are good this can be solved with the `predict()` function. – MrFlick Jul 10 '17 at 20:17
  • So, if I am following, you would prefer to see something like my response to Roland below. I probably could have organized these out step-wise a little better here, so I apologize for that. – DeCodened Jul 11 '17 at 16:19

1 Answers1

1

Note that your function is a polynomial and therefore linear in its coefficients. Anyway, it works the same with nls as with lm. Specify the data.frame for the fit:

o <- lm(y ~ poly(x, 3, raw = TRUE), data = DF)
plot(y ~ x, data = DF)
curve(predict(o, newdata = data.frame(x = x)), add = TRUE)
Roland
  • 127,288
  • 10
  • 191
  • 288
  • Roland, doesn't `curve` need an expression (in this case a polynomial in x with the coefficients from `o`)? Whereas you could use `lines()` to add a curve using the `predict` function to get the `y` values explicitly. – eipi10 Jul 11 '17 at 00:11
  • @eipi10 Yes, curve needs a function or an expression containing x. In this example I pass it the latter. Create some dummy data and test it. It works (there might be a typo in my code since it's untested). I use this construction regularly. – Roland Jul 11 '17 at 08:12
  • Thanks for this Roland. So I tried to reproduce my results using this methodology and I received a different curve: Originally: I have DF<-read.csv(.....) x<-DF[,1] y<-DF[,2] o<-nls(y~I(c1*x^3)+I(c2*x^2)+I(c3*x)+c4) which had coeff: -3.712e-05 7.559e-03 -7.784e-01 2.719e+01 Now, I used: o<-lm(y~poly(x,3,raw=TRUE)) curve(predict(o),add=TRUE)) I **did get the same coefficients** but the lines plotted on the graph aren't the same. Quite puzzled. I assume you can leave out the optional data in lm() if you have the data already fited with the lm() function? – DeCodened Jul 11 '17 at 16:14
  • You **must** follow my example to the details. Pass a data.frame to the model function and newdata to predict as shown. Otherwise this does not work correctly. – Roland Jul 11 '17 at 16:26
  • Scratch that Roland, I figured it out. I forgot that my data went through a slight modification, and when I re-plotted, the new curve using the lm() fit perfectly. Thank you for the help! Really quickly, I noticed in the documentation that the newdata parameter is optional, but it seems in this case it is necessary to specify x as input. Is that to specify the range over which the prediction is to occur? I received an error when omitting that value. – DeCodened Jul 11 '17 at 16:29
  • In a comment above I wrote that curve needs an expression containing x. – Roland Jul 11 '17 at 16:38