alc
2015-Aug-10 08:20 UTC
[R] accessing confidence interval values from the 'predict' function
Dear all, I'm wondering how can I access the confidence interval values ('upr' and 'lwr' values) produced by the 'predict' function. For example, I fitted a linear regression line using: fit <- lm(y ~ x) I then wanted to calculate a 95% confidence interval for the line, and did this using: ci <- predict(fit, data.frame(x), interval="confidence") I see that the 'ci' object has 'upr' and 'lwr' variables stored in it, but am not sure how to access them properly. I find I can do it using: upper <- as.data.frame(ci)$upr lower <- as.data.frame(ci)$lwr However, I'm wondering is there a more 'proper' way to do it in R? I find that things like ci$upr don't seem to work, but am not sure what's the right way? Kind Regards, Avril -- The Wellcome Trust Sanger Institute is operated by Genome Research Limited, a charity registered in England with number 1021457 and a company registered in England with number 2742969, whose registered office is 215 Euston Road, London, NW1 2BE. [[alternative HTML version deleted]]
Michael Dewey
2015-Aug-10 13:48 UTC
[R] accessing confidence interval values from the 'predict' function
Dear Avril I think you will find that predict.lm returns a matrix not a data frame. I find str() useful when R does things I did not expect or quite understand. Michael On 10/08/2015 09:20, alc wrote:> > > Dear all, > > I'm wondering how can I access the confidence interval values ('upr' and > 'lwr' values) produced by the 'predict' function. For example, I fitted > a linear regression line using: > > fit <- lm(y ~ x) > > I then wanted to calculate a 95% confidence interval for the line, and > did this using: > > ci <- predict(fit, data.frame(x), interval="confidence") > > I see that the 'ci' object has 'upr' and 'lwr' variables stored in it, > but am not sure how to access them properly. > > I find I can do it using: > > upper <- as.data.frame(ci)$upr > lower <- as.data.frame(ci)$lwr > > However, I'm wondering is there a more 'proper' way to do it in R? I > find that things like ci$upr don't seem to work, but am not sure what's > the right way? > > Kind Regards, > > Avril > > > >-- Michael http://www.dewey.myzen.co.uk/home.html
Adams, Jean
2015-Aug-10 18:43 UTC
[R] accessing confidence interval values from the 'predict' function
Avril, The more direct way to access these columns from the matrix is: ci[, "lwr"] ci[, "upr"] Jean On Mon, Aug 10, 2015 at 3:20 AM, alc <alc at sanger.ac.uk> wrote:> > > Dear all, > > I'm wondering how can I access the confidence interval values ('upr' and > 'lwr' values) produced by the 'predict' function. For example, I fitted > a linear regression line using: > > fit <- lm(y ~ x) > > I then wanted to calculate a 95% confidence interval for the line, and > did this using: > > ci <- predict(fit, data.frame(x), interval="confidence") > > I see that the 'ci' object has 'upr' and 'lwr' variables stored in it, > but am not sure how to access them properly. > > I find I can do it using: > > upper <- as.data.frame(ci)$upr > lower <- as.data.frame(ci)$lwr > > However, I'm wondering is there a more 'proper' way to do it in R? I > find that things like ci$upr don't seem to work, but am not sure what's > the right way? > > Kind Regards, > > Avril > > > > > -- > The Wellcome Trust Sanger Institute is operated by Genome Research > Limited, a charity registered in England with number 1021457 and a > company registered in England with number 2742969, whose registered > office is 215 Euston Road, London, NW1 2BE. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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]]