[R] subsetting data points within confidence limit
Greg Snow
Greg.Snow at imail.org
Fri Aug 13 19:59:39 CEST 2010
Why would you want to do this? Confidence intervals tell you about the uncertainty of the mean of y give x, not the individual data points. It may make more sense to use prediction intervals that tell you about individuals rather than means, but that still means throwing away alpha% of legitimate data.
There are packages and functions for various types of robust regression that may be better for what you are trying to accomplish (but I am just guessing until you tell us what your really trying to do).
If you really want to do this comparison, just use the predict function on the original data to get a confidence (or prediction) interval for each point, then compare each point to the corresponding interval (comparison is vectorized, this is a single step).
Your code below is filled with redundancies, you may want to brush up on the documentation and/or tutorials.
Hope this helps,
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Keun-Hyung Choi
> Sent: Friday, August 13, 2010 11:42 AM
> To: r-help at r-project.org
> Subject: [R] subsetting data points within confidence limit
>
> Dear R-list
>
>
>
> Suppose I have a data set stored in hmet, for which I did get
> confidence
> limit on a linear regression as shown below.
>
> My question is how I can subset only data points which are within the
> confidence limit.
>
>
>
> Thank you.
>
> Keun-Hyung
>
>
>
> ---------------------------------------------------------------
>
> Al=rnorm(100, 3)
>
> Cd=rnorm(100, 0.2)
>
> hmet=data.frame(Al=Al, Cd=Cd)
>
> plot(Al, Cd, xlab="Al", ylab= "Cd", , pch=16)
>
> cd.lm=lm(Cd~ Al, na.action=na.omit, data= hmet)
>
> abline(cd.lm$coeff, lty=1)
>
> xy= data.frame (Al =pretty(hmet$Al, 20))
>
> yhat=predict(cd.lm, newdata=xy, interval="confidence")
>
> ci=data.frame(lower=yhat[,"lwr"], upper=yhat[,"upr"])
>
> lines(xy$Al, ci$lower, lty= 2, lwd=2, col="grey")
>
> lines(xy$Al, ci$upper, lty=2, lwd=2, col="grey")
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.
More information about the R-help
mailing list