[R] How to simulate heteroscedasticity (correlation)

Patrizio Frederic frederic.patrizio at gmail.com
Wed Jul 23 12:22:33 CEST 2008


> 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
+-------------------------------------------------



More information about the R-help mailing list