Hi I am using R 2.2.0 under SuSE 10 I want to use lm() to get the slope and intercept for several daatasets and store them in a database. So far so good - but how do I extract the slope and the intercept from the result from lm()? my code looks like this: lmNNDens <- lm(log(DensNN$MeanNN) ~ log(DensNN$MeanDensity)) anovaLM <- anova(lmNNDens) Results$slope[No] <- ???lmNNDens??? Results$intercept[No] <- ???lmNNDens??? any help welcome, Rainer -- -- Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) Department of Conservation Ecology University of Stellenbosch Matieland 7602 South Africa Tel: +27 - (0)72 808 2975 (w) Fax: +27 - (0)21 808 3304 Cell: +27 - (0)83 9479 042 email: RKrug at sun.ac.za Rainer at krugs.de
Am 10 Mar 2006 um 12:17 hat Rainer M Krug geschrieben:> Hi > > I am using R 2.2.0 under SuSE 10 > > I want to use lm() to get the slope and intercept for several > daatasets and store them in a database. So far so good - but how do I > extract the slope and the intercept from the result from lm()? > > my code looks like this: > > lmNNDens <- lm(log(DensNN$MeanNN) ~ log(DensNN$MeanDensity)) > anovaLM <- anova(lmNNDens) > > Results$slope[No] <- ???lmNNDens??? > Results$intercept[No] <- ???lmNNDens??? > > any help welcome, >> x<-rnorm(100) > y<-rnorm(100) > summary(lmResult <- lm(y~x))Call: lm(formula = y ~ x) Residuals: Min 1Q Median 3Q Max -2.53308 -0.66265 0.02258 0.64282 3.38839 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.13784 0.11211 1.230 0.222 x -0.02748 0.12184 -0.226 0.822 Residual standard error: 1.114 on 98 degrees of freedom Multiple R-Squared: 0.0005188, Adjusted R-squared: -0.00968 F-statistic: 0.05087 on 1 and 98 DF, p-value: 0.822> coef(lmResult)(Intercept) x 0.13783934 -0.02748145> coef(lmResult)["(Intercept)"](Intercept) 0.1378393> coef(lmResult)["x"]x -0.02748145 That's the way I would do it. HTH, Bernd
Hi, I hope the following code helps. Best, Roland ## creating example data xx <- 1:30 yy <- 3 + 2*xx + rnorm(length(xx), mean=0, sd=2) plot(xx,yy) # looks reasonable ## the 'lm' mymodel <- lm(yy~xx) summary(mymodel) # just looking at the results ## extracting intercept and slope: coef(mymodel) intcp <- coef(mymodel)[1] slp <- coef(mymodel)[2] intcp slp> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Rainer M Krug > Sent: Friday, March 10, 2006 11:17 AM > To: r-help at stat.math.ethz.ch > Subject: [R] How to get the intercept from lm? > > Hi > > I am using R 2.2.0 under SuSE 10 > > I want to use lm() to get the slope and intercept for several > daatasets > and store them in a database. So far so good - but how do I > extract the > slope and the intercept from the result from lm()? > > my code looks like this: > > lmNNDens <- lm(log(DensNN$MeanNN) ~ log(DensNN$MeanDensity)) > anovaLM <- anova(lmNNDens) > > Results$slope[No] <- ???lmNNDens??? > Results$intercept[No] <- ???lmNNDens??? > > any help welcome, > > > Rainer > > > -- > -- > Rainer M. Krug, Dipl. Phys. (Germany), MSc Conservation Biology (UCT) > > Department of Conservation Ecology > University of Stellenbosch > Matieland 7602 > South Africa > > Tel: +27 - (0)72 808 2975 (w) > Fax: +27 - (0)21 808 3304 > Cell: +27 - (0)83 9479 042 > > email: RKrug at sun.ac.za > Rainer at krugs.de > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >+++++ This mail has been sent through the MPI for Demographic Rese...{{dropped}}
coef(lmNNDens)[1] ?coef Rainer M Krug wrote:> Hi > > I am using R 2.2.0 under SuSE 10 > > I want to use lm() to get the slope and intercept for several daatasets > and store them in a database. So far so good - but how do I extract the > slope and the intercept from the result from lm()? > > my code looks like this: > > lmNNDens <- lm(log(DensNN$MeanNN) ~ log(DensNN$MeanDensity)) > anovaLM <- anova(lmNNDens) > > Results$slope[No] <- ???lmNNDens??? > Results$intercept[No] <- ???lmNNDens??? > > any help welcome, > > > Rainer > >-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 452-1424 (M, W, F) fax: (917) 438-0894
Rainer M Krug wrote:> I want to use lm() to get the slope and intercept for several daatasets > and store them in a database. So far so good - but how do I extract the > slope and the intercept from the result from lm()?Read the help for lm - it talks about coefficients rather than slope and intercept, because a linear model can have more (or less) than a slope and an intercept. But basically you want the coef() or coefficients() function on the model: > x=1:10;y=x*1.4+2.5;m=lm(y~x) > m Call: lm(formula = y ~ x) Coefficients: (Intercept) x 2.5 1.4 > coef(m) (Intercept) x 2.5 1.4 > coef(m)[1] (Intercept) 2.5 > coef(m)[2] x 1.4 In general the help for any function should tell you what you can do with its returned object in the 'VALUE' section of the help, but there may also be functions like 'coef()' that give back useful info. For lm() objects there's coef(), residuals(), fitted() etc. Barry