Grokbase Groups R r-help March 2002
FAQ
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Search Discussions

  • Kaspar Pflugshaupt at Mar 19, 2002 at 5:05 pm

    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


    --

    Kaspar Pflugshaupt
    Geobotanisches Institut
    Zuerichbergstr. 38
    CH-8044 Zuerich

    Tel. ++41 1 632 43 19
    Fax ++41 1 632 12 15

    mailto:pflugshaupt at geobot.umnw.ethz.ch
    privat:pflugshaupt at mails.ch
    http://www.geobot.umnw.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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Arne Mueller at Mar 19, 2002 at 9:19 pm
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Ggrothendieck at Mar 20, 2002 at 12:26 am
    This is just going to you since its not really about R.
    If you are running Windows, check out
    the CurveExpert software. Use google to find
    out where to download it from. You can paste
    your data into its spreadsheet and then one click
    will run your data against numerous models, order
    them by fit and display the data and the fit on a graph
    for each one. I have no connection with this package.
    On 19 Mar 2002 at 21:19, Arne Mueller wrote:

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

    -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
  • Scott, Uriel at Mar 19, 2002 at 7:13 pm
    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
    _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupr-help @
categoriesr
postedMar 19, '02 at 4:03p
activeMar 20, '02 at 12:26a
posts5
users5
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase