# [R] fitting with lm

Scott, Uriel uriel.scott at mirant.com
Tue Mar 19 20:13:56 CET 2002

```You can linearize the equation y = a*x^b by applying log's to both sides:
log(y) = log(a) + b*log(x).  Then get log(a) and b as the coefficients of
the linear model lm( log(y) ~ log(x) ).

> -----Original Message-----
> From:	Arne Mueller [SMTP:a.mueller at icrf.icnet.uk]
> Sent:	Tuesday, March 19, 2002 11:04 AM
> To:	R-list
> Subject:	[R] fitting with lm
>
> Dear All,
>
> I'm getting confused with the concept R uses to do regression using lm.
> I'm afmiliar with gnuplot and the build-in fit command, but couldn't get
> R's lm to work on my data.
>
> I know that my data follows a powerlaw or maybe an exponential function,
> and I'd like to determine the best fitting factors for the formula:
> a*x^b where b < 0.
>
> I've tried thge follwoing:
>
> s <- lm(y ~ x)
>
> > summary(s)
>
> Call:
> lm(formula = y ~ x)
>
> Residuals:
>     Min      1Q  Median      3Q     Max
> -18.454  -7.577  -2.861   3.909  60.988
>
> Coefficients:
>              Estimate Std. Error t value Pr(>|t|)
> (Intercept) 21.209171   1.431472  14.816  < 2e-16 ***
> x           -0.065609   0.008799  -7.456 7.45e-12 ***
> ---
> Signif. codes:  0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1
>
> Residual standard error: 11.87 on 145 degrees of freedom
> Multiple R-Squared: 0.2772,     Adjusted R-squared: 0.2722
> F-statistic:  55.6 on 1 and 145 DF,  p-value: 7.454e-12
>
> What has R done? I assume the formula is just a+b*x and I can get a and
> b via
>
> > coef(s)
> (Intercept)           x
> 21.20917074 -0.06560878
>
> But:
>
> > s <- lm(y ~ a*x^b)
> Error in terms.formula(formula, data = data) :
>         invalid power in formula
>
> I went through the formula section of the R-manual, but I realy don't
> get it.
>
> Finally, I'd like to have the raw data-points together with a line
> representing the function used to fit the data in a plot? How can I plot
> function, e.g. sin(x) ?
>
> I hope I just need a primer on this to get going.
>
> 	thanks very much for any help,
>
> 	Arne
>
>
> --
> Arne Mueller
> Biomolecular Modelling Laboratory
> Imperial Cancer Research Fund
> 44 Lincoln's Inn Fields
> London WC2A 3PX, U.K.
> phone : +44-(0)207 2693405      | fax :+44-(0)207-269-3534
> email : a.mueller at icrf.icnet.uk | http://www.bmm.icnet.uk
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-
> r-help mailing list -- Read
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```