1
y<-c(0.0100,2.3984,11.0256,4.0272,0.2408,0.0200);
x<-c(1,3,5,7,9,11);
d<-data.frame(x,y)
myLm<-lm(x~y**2,data=d)
plot(d)
lines(x,lm(y ~ I(log(x)) + x,data=d)$fitted.values)
lines(x,lm(y ~ I(x**2) + x,data=d)$fitted.values) % not quite right, smooth plz

It should be smooth plot, something wrong.

enter image description here

Helper questions

  1. What algorithm is used in linear regression?
  2. Explain least squares plotting with Ones -matrix
2
  • What does "nevertheless of the points or compliance" mean?
    – Ben Bolker
    Dec 3, 2011 at 13:31
  • 2
    He edited the posts numerous times, changing his word etc. AndresT and I tried to help last night but, well, the terms of the question kept shifting. Dec 3, 2011 at 13:37

4 Answers 4

9

You need predict in order to interpolate the predictions between the fitted points.

d <- data.frame(x=seq(1,11,by=2),
                y=c(0.0100,2.3984,11.0256,4.0272,0.2408,0.0200))
lm1 <-lm(y ~ log(x)+x, data=d)
lm2 <-lm(y ~ I(x^2)+x, data=d)
xvec <- seq(0,12,length=101)
plot(d)
lines(xvec,predict(lm1,data.frame(x=xvec)))
lines(xvec,predict(lm2,data.frame(x=xvec)))

enter image description here

3
  • Yes, after he finally edited in that all he wants is a parabola, the predict() over a sequence of x-axis point is the correct answer. Note how his first fit still models y as a function of y. Oh well. I'm sure he'll update that now that I mention. Dec 3, 2011 at 13:57
  • what could i do if i have multiple variables, rather than only one independent variable.
    – Frank Wang
    Jul 13, 2012 at 9:34
  • @FrankWANG: you should probably post a new question, referencing this one, and giving appropriate detail (e.g. a reproducible question: tinyurl.com/reproducible-000 ). Short answer, you have to decide between (1) producing a surface plot; (2) drawing one or more predicted lines (or subplots) with all but one of the predictors held at a specified value.
    – Ben Bolker
    Jul 13, 2012 at 12:02
6

The mandatory ggplot2 method:

library(ggplot2)
qplot(x,y)+stat_smooth(method="lm", formula="y~poly(x,2)", se=FALSE)

enter image description here

3
  • is it possible to have many plots in the same picture? This is apparently some different environment so I cannot use the style plot(1:10,1:10); lines(1:10, 1:10*2) etc.
    – hhh
    Dec 4, 2011 at 11:37
  • 1
    Yes, just add extra layers or aesthetics, eg: dfr <- data.frame(x=rep(1:10,2),y=c(1:10,(1:10)^2),type=c(rep("linear",10),rep("squared",10))); qplot(x,y,data=dfr,geom="line",colour=type)
    – James
    Dec 5, 2011 at 10:29
  • 1
    Also ggplot2 uses the grid graphics system rather than the base one so the commands and paradigms are different, see had.co.nz/ggplot2
    – James
    Dec 5, 2011 at 10:31
3

something like:

 plot(d)    
 abline(lm(x~y**2,data=d), col="black")

will make it (if linear, as was implied by the way the question was asked first)

For what you are looking for I think:

  lines(smooth.spline(x, y))

May work as hinted by Dirk.

1
  • Maybe not quite as he is fitting a linear model where his regressor is squared. Dec 3, 2011 at 5:27
2

You should spend some time with the 'Appendix A: A sample session' of the 'An Introduction R' manual that came with your program. But here is a start

R> y<-c(0.0100,2.3984,11.0256,4.0272,0.2408,0.0200);
R> x<-c(1,3,5,7,9,11);
R> d<-data.frame(x,y)
R> myLm<-lm(x~y**2,data=d)
R> myLm

Call:
lm(formula = x ~ y^2, data = d)

Coefficients:
(Intercept)            y  
      6.434       -0.147  

and we can plot this as (where I now corrected for your unusual inversion of the roles of x and y):

R> plot(d)
R> lines(d$y,fitted(myLm))

enter image description here

4
  • ...err quadratic regression, this is linear.
    – hhh
    Dec 3, 2011 at 5:44
  • 2
    That's just it: you are fitting a linear model over a nonlinear transformation of your variables. You could create x2 <- x^2 and then regress y ~ x2 and plot that in (y,x2) space. If you want a nonlinear regression you need a different function such as nls(). Or splines. Or additive models. Or, or, or ... Dec 3, 2011 at 5:47
  • look now the question, is it now clear. No fuzzy sentences, only one clear picture -- got what I am trying to achieve?
    – hhh
    Dec 3, 2011 at 5:52
  • 7
    Can you be a little more rude to people who you give free lessons? Dec 3, 2011 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.