Search code examples
rlmpredictexp

How to interpret lm() coefficients when formula=y~exp(x)?


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.


Solution

  • 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" )