Hi, I would like to generate two correlated variables. I found that funktion for doing that: a <- rmvnorm(n=10000,mean=c(20,20),sigma=matrix(c(5,0.8*sqrt(50), 0.8*sqrt(50),10),2,2)) (using library(mvtnorm)) Now I also want to generate two correlated variables where the error variance vary over the variable-correlation. And I want to plot this for showing heteroscedasticity. Like shown here: http://upload.wikimedia.org/wikipedia/de/1/1b/Heteroske2.png Is that possible with R?
Patrizio Frederic
2008-Jul-23 10:22 UTC
[R] How to simulate heteroscedasticity (correlation)
> Now I also want to generate two correlated variables where the error > variance vary over the variable-correlation. > And I want to plot this for showing heteroscedasticity. > > Like shown here: > http://upload.wikimedia.org/wikipedia/de/1/1b/Heteroske2.png > > Is that possible with R? >of course it is. And it' very simple seed(123456) x = rnorm(500,1,1) b0 = 1 # intercept chosen at your choice b1 = 1 # coef chosen at your choice h = function(x) 1+.4*x # h performs heteroscedasticity function (here I used a linear one) eps = rnorm(500,0,h(x)) y = b0 + b1*x + eps plot(x,y) abline(lsfit(x,y)) abline(b0,b1,col=2) regards PF ps notice that in heteroscedasticity case the random vector (X,Y) is not a bivariate normal but it is: Y|X=x ~ normal(b0+b1 x; h(x)) ie every conditional Y is normal +------------------------------------------------- | Patrizio Frederic | Research associate in Statistics, | Department of Economics, | University of Modena and Reggio Emilia, | Via Berengario 51, | 41100 Modena, Italy | | tel: +39 059 205 6727 | fax: +39 059 205 6947 | mail: patrizio.frederic at unimore.it +-------------------------------------------------