0
votes

I used lm() function to get an exponential curve and it works well (the formula is y~exp(x)). But i don't understand how to use manually the coefficients ?

I do lm(y~exp(x)), extract the coefficients : b = intercept a = coef

Then, if i try to do the prediction "manually" with : a * exp(x) + b The result is wrong.

But with predict() it works totally fine. So I guess i didn't understand how lm() do the model ?

EDIT : Just mixed everything haha, it works well.

1
Hello, can you send some reproductible code, with a dataset (you can use dput) ?MrSmithGoesToWashington
Can you share an example? It should work the way you describe iterocoar
could you have mixed the coefficiencts when you tried it manually?Sirius
Yes ! My bad, i just mixed up the coef between my models...user15282107

1 Answers

0
votes

This code demonstrates that your approach should work:

    set.seed( 100 )

    x <- rnorm(10)
    y <- runif(10)

    m <- lm( y~exp(x) )

    cf <- coef(m)

    yp1 <- predict( m, newdata=data.frame(x=x) )
    yf <- fitted.values(m)

    stopifnot( max( abs(yp1 - yf) ) < 1e-10 )

    yp2 <- cf[1] + cf[2] * exp(x)

    stopifnot( max( abs(yp1 - yp2) ) < 1e-10 )

    cat( "Hi-Ho Silver - Away!\n" )