[R] creating multivariate normal random variables

asdf1234 farina2001 at gmx.de
Thu May 9 16:48:22 CEST 2013


Dear R experts,

I am trying to create a dataset, with one dependent binomial and one
independen (normal) variable.
I have the condition that my x should be created in the following way:

if y=0 my x should have mean=0.2 and variance=1
if y=1 my x should have mean=0.7 and variance=1
Furthermore, my y is chosen with a prbability of 0.5

I tried it with the following expression:

n <- 1
prob <- 0.5
result.y <- vector("list",100)
result.X <- vector("list",100)
for (i in 1:100)
{
  y <- rbern(n, prob)
  X <- if(y==0){X <- rnorm(n, mean=0.2, sd=1)}else{X <- mvrnorm(n, mean=0.7,
sd=1)}
  result.y[[i]] <- y
  result.X[[i]] <- X
}
y <- result.y
X <- result.X

matrix <- cbind(result.y, result.X)
View(matrix) 

However, when I try to run a logistic regression

glm.out = glm(formula = y ~ X, family = binomial(logit))
summary(glm.out)
hat.beta.L <- coef(glm.out)

I get the error massage

Fehler in model.frame.default(formula = y ~ X, drop.unused.levels = TRUE) : 
  ungültiger Typ (list) für die Variable 'y'

Does someone know what I am doing wrong?
Thank you very much for your help, I really appreciate it.



--
View this message in context: http://r.789695.n4.nabble.com/creating-multivariate-normal-random-variables-tp4666678.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list