Skip to content

Get lm formula

2 messages · Thomas Szegvary, ONKELINX, Thierry

#
I have several plots where I'd like to add the formula from the linear model
automatically, i.e. in form of y=ax+b, to the plot. I used lm() for a simple
scatter plot.
I was able to add the r-sqare to the plot, using summary(lm$r.square) to
extract the value for r^2. but for the coefficients a and b I couldn't
extract the values like for r-square. I can check the values using coef(lm),
but to be able to write it as text to the plot I should extract the values,
am I right? Or is there a better solution? Does anyone has an idea? I
couldn't find any yet...

Thanks a lot,Thomas



__

Thomas Szegvary
Institute of Environmental Geosciences
Department of Geosciences
University of Basel
Bernoullistrasse 30
CH - 4056 Basel

Tel.  41-61-267 04 82
Fax. 41-61-267 04 79
Email: t.szegvary at unibas.ch
www.radon.unibas.ch
www.unibas.ch/environment
#
You'll need something like

x <- rnorm(100)
y <- 0.5 * x + rnorm(100) + 1
fit <- lm(y ~x)
paste("y =", round(fit$coef[2], 2), "x +", round(fit$coef[1], 2))

Please note that this is R-sig-geo, the R mailing list dedicated to
spatial issues. For more general questions (like this one) you'll get
more respons from R-help (r-help at stat.math.ethz.ch).

Cheers,

Thierry

------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry.Onkelinx at inbo.be
www.inbo.be 

Do not put your faith in what statistics say until you have carefully
considered what they do not say.  ~William W. Watt
A statistical analysis, properly conducted, is a delicate dissection of
uncertainties, a surgery of suppositions. ~M.J.Moroney