Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
399 views
in Technique[技术] by (71.8m points)

lm - retrieve formula used by predict function in exponential equation in R

I can't figure out how to reconstruct the results nor the formula from the predict function of a linear model. I get the same results also when using this data in ggplot geom_smooth(method='lm',formula,y ~ exp(x)).

Here's some sample data

x=c(1,10,100,1000,10000,100000,1000000,3000000)
y=c(1,1,10,15,20,30,40,60)

I would like to use an exponential function so (ignore for the moment that I log the x value, because exp() fails for very large values):

model = lm( y ~ exp(log10(x)))
mypred = predict(model)
plot(log(x),mypred)

I have tried

lm_coef <- coef(model)
plot(log10(x),lm_coef[1]*exp(-lm_coef[2]*x))

However this is giving me a decreasing exponential instead of the increasing. My goal is to extract the equation of the exponential function so I can reuse the coefficients in another context.. What equation is predict() using and is there a way to see it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I did something along the lines of:

Df<-data.frame(x=c(1,10,100,1000,10000,100000,1000000,3000000),
               y=c(1,1,10,15,20,30,40,60))


model<-lm(data = Df, formula = y~log(x))
predict(model)
plot(log(Df$x),predict(model))

summary(model)

The relevant output you get is:

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  -6.0700     4.7262  -1.284 0.246386    
log(x)        3.5651     0.5035   7.081 0.000398 ***
---

Your equation therefore is 3.5651*log(x)-6.0700


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...