Yi
2010-Jul-21 21:29 UTC
[R] Variance of the prediction in the linear regression model (Theory and programming)
Hi, folks, Here are the codes: ############## y=1:10 x=c(1:9,1) lin=lm(log(y)~x) ### log(y) is following Normal distribution x=5:14 prediction=predict(lin,newdata=x) ##prediction=predict(lin) ############### 1. The codes do not work, and give the error message: Error in eval(predvars, data, env) : numeric 'envir' arg not of length one. But if I use the code after the pound sign, it works. I mean the name of the newdata is x, why it does not work though? 2. Because the prediction is conducted for log(y). I need to get the expected value of y, which is LN distribution, for the new data sets. I need to know the expectation of log(y) and variance of log(y). ##### mean=mean(prediction) sd=sd(prediction) mean_y=exp(mean+0.5*sd^2) ### formula from Normal to LN ###### Is sd(prediction) the correct why to calculate the sigma of the prediction? Or should I just use the value of Residual standard error from summary(lin)? Answer to either question will be appreciated! Thanks Yi [[alternative HTML version deleted]]
Yi
2010-Jul-21 21:49 UTC
[R] Variance of the prediction in the linear regression model (Theory and programming)
Sorry, for the second question. I stated in a wrong way. My aim is the mean and sd of each new observation. ##### mean=fitted(prediction) ###### But I do not know how to get sd for each new observation. Any tips? Thanks Yi On Wed, Jul 21, 2010 at 2:29 PM, Yi <liuyi.feier@gmail.com> wrote:> Hi, folks, > > Here are the codes: > > ############## > y=1:10 > x=c(1:9,1) > lin=lm(log(y)~x) ### log(y) is following Normal distribution > x=5:14 > prediction=predict(lin,newdata=x) ##prediction=predict(lin) > ############### > > 1. The codes do not work, and give the error message: Error in > eval(predvars, data, env) : > numeric 'envir' arg not of length one. But if I use the code after the > pound sign, it works. I mean the name of the newdata is x, why it does not > work though? > > > 2. Because the prediction is conducted for log(y). I need to get the > expected value of y, which is LN distribution, for the new data sets. I need > to know the expectation of log(y) and variance of log(y). > > ##### > mean=mean(prediction) > sd=sd(prediction) > mean_y=exp(mean+0.5*sd^2) ### formula from Normal to LN > ###### > > Is sd(prediction) the correct why to calculate the sigma of the prediction? > Or should I just use the value of Residual standard error from summary(lin)? > > Answer to either question will be appreciated! > > Thanks > > Yi > >[[alternative HTML version deleted]]
Dennis Murphy
2010-Jul-21 22:44 UTC
[R] Variance of the prediction in the linear regression model (Theory and programming)
Hi: On Wed, Jul 21, 2010 at 2:29 PM, Yi <liuyi.feier@gmail.com> wrote:> Hi, folks, > > Here are the codes: > > ############## > y=1:10 > x=c(1:9,1) > lin=lm(log(y)~x) ### log(y) is following Normal distribution > x=5:14 > prediction=predict(lin,newdata=x) ##prediction=predict(lin) > ############### >predict() needs a *data.frame* as the argument of newdata:> d <- data.frame(x) > prediction=predict(lin,newdata=d) > prediction1 2 3 4 5 6 7 8 1.574308 1.733976 1.893643 2.053311 2.212979 2.372646 2.532314 2.691981 9 10 2.851649 3.011316 1. The codes do not work, and give the error message: Error in> eval(predvars, data, env) : > numeric 'envir' arg not of length one. But if I use the code after the > pound sign, it works. I mean the name of the newdata is x, why it does not > work though? > > > 2. Because the prediction is conducted for log(y). I need to get the > expected value of y, which is LN distribution, for the new data sets. I > need > to know the expectation of log(y) and variance of log(y). > > ##### > mean=mean(prediction) > sd=sd(prediction) > mean_y=exp(mean+0.5*sd^2) ### formula from Normal to LN > ###### > > Is sd(prediction) the correct why to calculate the sigma of the prediction? > Or should I just use the value of Residual standard error from > summary(lin)? >The prediction variance is not the same as the residual variance; consult your regression text. Which expected value do you want to transform: the response, or the predicted response? Since the lognormal mean depends on both the normal mean and variance, this is not an idle question. HTH, Dennis> > Answer to either question will be appreciated! > > Thanks > > Yi > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Reasonably Related Threads
- Verify the linear regression model used in R ( fundamental theory)
- count data with a specific range
- How to predict the mean and variance of the dependent variable after regression
- Help on calculating spearman rank correlation for a data frame with conditions
- How to calculate the robust standard error of the dependent variable