开发者

Adding random error to a discrete variable in R

开发者 https://www.devze.com 2023-03-08 20:55 出处:网络
I need to apply some econometric methodology, and I have to consider a continuous variable among my regressors. The pr开发者_Python百科oblem is that I just have discrete variables.

I need to apply some econometric methodology, and I have to consider a continuous variable among my regressors. The pr开发者_Python百科oblem is that I just have discrete variables.

Could someone tell me how I can add small random error (residual) with mean 0 to a discrete variable (one column in my data base), and save it in my data base? I'm still a R beginner.

Example: I have

mA <- data.frame(Asexo=c(1, 0, 0, 1, 0))

and I want to add a small error to mA$Asexo so that it became a continuous variable:

mA <- data.frame(Asexocontiuous=c(1.03, 0.34, 0.18, 0, 1.5))


If you want to 'jitter' a 0/1 variable in order to make sure there are no duplicates (or to use a method that requires continuous variables), the simplest approach is

mydat$sexcont <- rnorm(nrow(mydat),mean=mydat$sexbinary,sd=csd)

where csd is your chosen standard deviation. A little more elegantly,

mydat <- transform(mydat,sexcont=rnorm(nrow(mydat),mean=sexbinary,sd=csd))

If sexbinary is a factor then use as.numeric(sexbinary) (or as.numeric(sexbinary)-1 if you need it to be a 0/1 rather than a 1/2 variable)

You can also see ?jitter, although that is more commonly used in the context of avoiding point overlaps in graphics.

0

精彩评论

暂无评论...
验证码 换一张
取 消