Skip to content

Least Squares Fit

3 messages · Fred J., Jonathan Baron, Martin Maechler

#
Hello

I need to plot the least squares fit and get the slope
of the line that best fit the data. after reading lm
and lsfit, since not being able to understand the use
of the second argument "y" for the lsfit, I am giving
the lm a go, but know not why the code below does not
draw the line on the plot. e.g. well.. why -50
Intercept?
thanks

x <- 1:10; y <- x+50
plot(x,y)
z <- lm(as.data.frame(cbind(x,y))))
abline(z)
(Intercept)           y 
        -50           1
#
On 05/02/04 03:26, Fred J. wrote:
The lm() is predicting x from y, and plot() is plotting y as a
function of x.

Try
plot(x,y)
abline(lm(y~x))
#

        
Jon> On 05/02/04 03:26, Fred J. wrote:
>> Hello
    >> 
    >> I need to plot the least squares fit and get the slope of
    >> the line that best fit the data. after reading lm and
    >> lsfit, since not being able to understand the use of the
    >> second argument "y" for the lsfit, I am giving the lm a
    >> go, but know not why the code below does not draw the
    >> line on the plot. e.g. well.. why -50 Intercept?  thanks
    >> 
    >> x <- 1:10; y <- x+50
    >> plot(x,y)
    >> z <- lm(as.data.frame(cbind(x,y))))
    >> abline(z)
    >> 
    >>> z$coefficients
    >> (Intercept)           y
    >> -50           1

Did you, "Fred J.",  really ever take time to read the
"Introduction to R", maybe with a pencil in your hand to mark a
few things?


    Jon> The lm() is predicting x from y, and plot() is plotting
    Jon> y as a function of x.

    Jon> Try 
    Jon> plot(x,y) 
    Jon> abline(lm(y~x))

or in this case, the more intuitive

     plot(y ~ x)
abline(lm(y ~ x))

y ~ x  : `` y is modeled by (predictor) x ''

--
Martin