# [R] fitting with lm

Arne Mueller a.mueller at cancer.org.uk
Tue Mar 19 22:19:08 CET 2002

```Thanks for pointing out nls, it seems to provide what I'm looking for.
Unfortunately, my daya distribution doesn't seem to be approxumated by
either a power-law nor by an exponential fucntion :-( but I may have to
play a little bit more with the params of the nls function.

regards,

Arne

Kaspar Pflugshaupt wrote:
> On 19.3.2002 17:03 Uhr, Arne Mueller wrote:
>
>
>>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)
>>
>
> [...]
>
>
>>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.
>>
>
> Generally, you want to look at the nlm library to fit complicated functions
> to your data. lm() does just linear models. In your case, however, you could
> try a log-transformation to linearize (fitting log(y) ~ a + log(x) * b),
> then re-transform the coefficients to the original scale.
>
>
>>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) ?
>>
>
> Look at help(curve). To add the results of any fit to an existing data
> scatterplot, you can also use
>
> lines(x.values, predict(your.model))
>
> - if your x values are sorted by size. If not, use something like this:
> x.order <- order(x.values)
> lines(x.values[x.order], predict(your.model)[x.order])
>
>
>
>>I hope I just need a primer on this to get going.
>>
>
> Hope that helps.
>
>
>
> Kapsar Pflugshaupt
>
>
>

--
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```