Friedericksen Hope
2009-Jul-20 18:16 UTC
[R] Simulate residuals with different properties for a linear model (regression)
Hey guys, for educational purposes I wonder if it is possible to simulate different data sets (or specifically residuals) for a linear regression. I would like to show my students residuals with different means, variances and distributions (normal, but also not normal) in the plots created with the plot command for a lm-object. In addition it would be nice to simulate although influencal values (high cooks distance and leverage) lm.results <- lm(y~x,data) plot(lm.results) Is there an easy way to do this? Or can this be done at all (and if yes, any hints?:-) Thanks and Greetings! Friedericksen
Juliet Hannah
2009-Jul-20 23:56 UTC
[R] Simulate residuals with different properties for a linear model (regression)
Here are a couple of examples. # residuals not normal n <- 100; x = seq(n) y = 10 + 10 *x + 20 * rchisq(n,df=2) non_normal_lm = lm(y~x) #non-constant variance n <- 100; x = seq(n) y = 100 + 3 * x + rnorm(n,0,3) * x; het_var_lm = lm(y~x) #For each of these try: plot(non_normal_lm) plot(het_var_lm) #or specify which one you want plot(non_normal_lm,which=1) Best, Juliet On Mon, Jul 20, 2009 at 2:16 PM, Friedericksen Hope<friedericksen.hope at gmail.com> wrote:> Hey guys, > > for educational purposes I wonder if it is possible to simulate > different data sets (or specifically residuals) for a linear regression. > I would like to show my students residuals with different means, > variances and distributions (normal, but also not normal) in the plots > created with the plot command for a lm-object. In addition it would be > nice to simulate although influencal values (high cooks distance and > leverage) > > lm.results <- lm(y~x,data) > plot(lm.results) > > Is there an easy way to do this? Or can this be done at all (and if yes, > any hints?:-) > > Thanks and Greetings! > Friedericksen > > ______________________________________________ > 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. >
hadley wickham
2009-Jul-21 02:30 UTC
[R] Simulate residuals with different properties for a linear model (regression)
> for educational purposes I wonder if it is possible to simulate > different data sets (or specifically residuals) for a linear regression. > I would like to show my students residuals with different means, > variances and distributions (normal, but also not normal) in the plots > created with the plot command for a lm-object. In addition it would be > nice to simulate although influencal values (high cooks distance and > leverage) > > lm.results <- lm(y~x,data) > plot(lm.results) > > Is there an easy way to do this? Or can this be done at all (and if yes, > any hints?:-)Check out some neat ideas here: http://www4.stat.ncsu.edu/~stefanski/NSF_Supported/Hidden_Images/stat_res_plots.html Hadley -- http://had.co.nz/
Friedericksen Hope
2009-Jul-21 05:44 UTC
[R] Simulate residuals with different properties for a linear model (regression)
Thank you both for your responses and the hints for help! Greetings, Friedericksen Friedericksen Hope wrote:> Hey guys, > > for educational purposes I wonder if it is possible to simulate > different data sets (or specifically residuals) for a linear regression. > I would like to show my students residuals with different means, > variances and distributions (normal, but also not normal) in the plots > created with the plot command for a lm-object. In addition it would be > nice to simulate although influencal values (high cooks distance and > leverage) > > lm.results <- lm(y~x,data) > plot(lm.results) > > Is there an easy way to do this? Or can this be done at all (and if yes, > any hints?:-) > > Thanks and Greetings! > Friedericksen >