varin sacha
2014-Feb-25 22:57 UTC
[R] plot out the predicted values and the upper and lower limits
Hi, I have realized a multiple linear regression. To know how well my model does in terms of prediction, I can compute prediction intervals bands and decide if they are narrow enough to be of use. If they are too wide, then they probably are not useful. So what I am trying to do is : Theoretically I know that I can use the "predict" command in R to generate the prediction interval for a set of points. The idea is to find the linear regression using the lm command. Then I can use the predict command to get the prediction interval for a set of points in the domain. Then I plot out the predicted values as well as the upper and lower limits of the prediction intervals for those values. My problem is to practice what I theoretically know, especially using R. My linear model is the following : LinearModel.1 <- lm(GDP.per.head ~ Competitivness.score + Quality.score, data=Dataset) summary(LinearModel.1) predict(LinearModel.1, se.fit = FALSE, scale = NULL, df = Inf,interval = c("none", "confidence", "prediction"),level = 0.95, type = c("response", "terms"),terms = NULL) Could you please help me with my R codes ? Thanks for your precious help, [[alternative HTML version deleted]]
Rolf Turner
2014-Feb-26 01:22 UTC
[R] plot out the predicted values and the upper and lower limits
On 26/02/14 11:57, varin sacha wrote:> Hi, > I have realized a multiple linear regression. > To know how well my model does in terms of prediction, I can compute prediction intervals bands and decide if they are narrow enough to be of use. If they are too wide, then they probably are not useful. > > So what I am trying to do is : > Theoretically I know that I can use the "predict" command in R to generate the prediction interval for a set of points. The idea is to find the linear regression using the lm command. Then I can use the predict command to get the prediction interval for a set of points in the domain. Then I plot out the predicted values as well as the upper and lower limits of the prediction intervals for those values. > My problem is to practice what I theoretically know, especially using R. > > My linear model is the following : > LinearModel.1 <- lm(GDP.per.head ~ Competitivness.score + Quality.score, data=Dataset) > summary(LinearModel.1) > predict(LinearModel.1, se.fit = FALSE, scale = NULL, df = Inf,interval = c("none", "confidence", "prediction"),level = 0.95, type = c("response", "terms"),terms = NULL) > > Could you please help me with my R codes ?If you want prediction intervals, ask for them!!! There is no point at all in simply repeating the default for the argument "interval". If you actually want the default, then don't say anything at all. I.e. omit the "interval" argument from your call and don't clutter things up with irrelevancies. But here you *don't* want the default (which is "none"). You want "prediction". So set interval="prediction" in your call. Actually you can just set interval="p" because of partial argument matching. cheers, Rolf Turner
varin sacha
2014-Feb-26 16:43 UTC
[R] plot out the predicted values and the upper and lower limits
Many thanks Rolf, These codes below are ok : LinearModel.1 <- lm(GDP.per.head ~ Competitivness.score + Quality.score, data=Dataset) summary(LinearModel.1) predict(LinearModel.1, se.fit = FALSE, scale = NULL, df = Inf,interval = c("prediction"),level = 0.95, type = c("response")) One problem remains : If I want to plot out (in a same graph) the predicted values as well as the upper and lower limits of the prediction intervals for those values. How can I do, many thanks once more. Best, Le Mercredi 26 février 2014 4h59, varin sacha <varinsacha@yahoo.fr> a écrit : Hi, I have realized a multiple linear regression. To know how well my model does in terms of prediction, I can compute prediction intervals bands and decide if they are narrow enough to be of use. If they are too wide, then they probably are not useful. So what I am trying to do is : Theoretically I know that I can use the "predict" command in R to generate the prediction interval for a set of points. The idea is to find the linear regression using the lm command. Then I can use the predict command to get the prediction interval for a set of points in the domain. Then I plot out the predicted values as well as the upper and lower limits of the prediction intervals for those values. My problem is to practice what I theoretically know, especially using R. My linear model is the following : LinearModel.1 <- lm(GDP.per.head ~ Competitivness.score + Quality.score, data=Dataset) summary(LinearModel.1) predict(LinearModel.1, se.fit = FALSE, scale = NULL, df = Inf,interval = c("none", "confidence", "prediction"),level = 0.95, type = c("response", "terms"),terms = NULL) Could you please help me with my R codes ? Thanks for your precious help, [[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]]
Rolf Turner
2014-Feb-26 21:32 UTC
[R] plot out the predicted values and the upper and lower limits
On 27/02/14 05:43, varin sacha wrote:> Many thanks Rolf, > > These codes below are ok : > LinearModel.1 <- lm(GDP.per.head ~ Competitivness.score + > Quality.score, data=Dataset) > summary(LinearModel.1) > predict(LinearModel.1, se.fit = FALSE, scale = NULL, df = Inf,interval > c("prediction"),level = 0.95, type = c("response")) > > One problem remains : If I want to plot out (in a same > graph) the predicted values as well as the upper and lower limits of the > prediction intervals for those values. How can I do, many thanks once more.This is very a very basic R plotting problem. If you are going to use R, you should learn how to use R. Your emails indicate that you haven't really got a clue. Read some of the excellent introductory material available from the R web page. Start with a thorough read of "An Introduction to R". Then have a look at some of the material available under "contributed documentation". It will take a while but will be worth the effort if you wish to become anything like competent at using R. I could tell you how to do the plot you want, in a few lines, but I am not going to. You need to do some learning. cheers, Rolf P. S. An indication that you haven't a clue is the fact that you use interval = c("prediction") Just what on earth do you think that "c()" is doing for you here? Learn what the c() function means and what its purpose is. Don't just slap down bits of code in a higgledy-piggledy manner and hope that something will work. Wrapping "prediction" in c() is harmless in this instance, but is unnecessary, redundant and confusing. In general using inappropriate code that you don't understand will result in errors. R.
MacQueen, Don
2014-Feb-26 22:54 UTC
[R] plot out the predicted values and the upper and lower limits
When in doubt, first consult the included online help. ?predict.lm Offers this example, which seems to meet the request x <- rnorm(15) y <- x + rnorm(15) predict(lm(y ~ x)) new <- data.frame(x = seq(-3, 3, 0.5)) predict(lm(y ~ x), new, se.fit = TRUE) pred.w.plim <- predict(lm(y ~ x), new, interval = "prediction") pred.w.clim <- predict(lm(y ~ x), new, interval = "confidence") matplot(new$x, cbind(pred.w.clim, pred.w.plim[,-1]), lty = c(1,2,2,3,3), type = "l", ylab = "predicted y") (note that all the functions come with R and are loaded by default) And predict.lm can be found in the "See also" section of ?predict. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 2/25/14 2:57 PM, "varin sacha" <varinsacha at yahoo.fr> wrote:>Hi, >I have realized a multiple linear regression. >To know how well my model does in terms of prediction, I can compute >prediction intervals bands and decide if they are narrow enough to be of >use. If they are too wide, then they probably are not useful. > >So what I am trying to do is : >Theoretically I know that I can use the "predict" command in R to >generate the prediction interval for a set of points. The idea is to find >the linear regression using the lm command. Then I can use the predict >command to get the prediction interval for a set of points in the domain. >Then I plot out the predicted values as well as the upper and lower >limits of the prediction intervals for those values. >My problem is to practice what I theoretically know, especially using R. > >My linear model is the following : >LinearModel.1 <- lm(GDP.per.head ~ Competitivness.score + Quality.score, >data=Dataset) >summary(LinearModel.1) >predict(LinearModel.1, se.fit = FALSE, scale = NULL, df = Inf,interval >c("none", "confidence", "prediction"),level = 0.95, type = c("response", >"terms"),terms = NULL) > >Could you please help me with my R codes ? > >Thanks for your precious help, > [[alternative HTML version deleted]] >