Re: [R] Least Squares Fit

About this list Date view Thread view Subject view Author view Attachment view

From: Jonathan Baron (baron@psych.upenn.edu)
Date: Sun 02 May 2004 - 21:10:10 EST


Message-id: <20040502111010.GA20854@psych>

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

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))

-- 
Jonathan Baron, Professor of Psychology, University of Pennsylvania
Home page:            http://www.sas.upenn.edu/~baron
R page:               http://finzi.psych.upenn.edu/

______________________________________________ R-help@stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


About this list Date view Thread view Subject view Author view Attachment view

This archive was generated by hypermail 2.1.3 : Mon 31 May 2004 - 23:05:07 EST