Script 1
#1. Set random seed to a fixed number
set.seed(631)
#2. Create IDs
ID <- c(1:150)
#3. Random assignment of instruction method
method <- rbinom(150,1,0.5)
methodf <- factor(method,labels=c("A", "B"))
#4. Sex of the child
sex <-rbinom(150,1,0.4)
sexf <-factor(sex,labels=c("girl", "boy"))
#5. Take random sample
library(MASS)
mu <- c(100,130)
sigma <- matrix(c(64,40,40,100),2,2)
X <- mvrnorm(150,mu,sigma)
X <- round(X,digits=0)
colnames(X) <- c("IQ","arith0")
IQ <- cbind(X[,1])
arith0 <- cbind(X[,2])
arith0 <- round(arith0)
#6. Generate normally distributed variables
e1 <- rnorm(150,0,3)
e2 <- rnorm(150,0,3)
e3 <- rnorm(150,0,3)
#7. Compute the arithmetic score after one year
arith1 = round(0.8*method + 0.3*(arith0 + e1) + 1.5*(IQ + e2) + 0.6*sex + e3, 0)
#8. Parental education
Xeduc <- rmultinom(150, 1, c(0.3, 0.5, 0.2))
Xeduct <-t(Xeduc)
v<-c(0,1,2)
educ <- Xeduct %*% v
educf <- factor(educ,labels=c("low","middle","high"))
#9. Store variables in dataframe
mydata <- data.frame(ID, method, methodf, sex, sexf, IQ, arith0, arith1, educ, educf); mydata
#10. Save data
save(mydata, file="mydata.Rdata")
set.seed(631)
#2. Create IDs
ID <- c(1:150)
#3. Random assignment of instruction method
method <- rbinom(150,1,0.5)
methodf <- factor(method,labels=c("A", "B"))
#4. Sex of the child
sex <-rbinom(150,1,0.4)
sexf <-factor(sex,labels=c("girl", "boy"))
#5. Take random sample
library(MASS)
mu <- c(100,130)
sigma <- matrix(c(64,40,40,100),2,2)
X <- mvrnorm(150,mu,sigma)
X <- round(X,digits=0)
colnames(X) <- c("IQ","arith0")
IQ <- cbind(X[,1])
arith0 <- cbind(X[,2])
arith0 <- round(arith0)
#6. Generate normally distributed variables
e1 <- rnorm(150,0,3)
e2 <- rnorm(150,0,3)
e3 <- rnorm(150,0,3)
#7. Compute the arithmetic score after one year
arith1 = round(0.8*method + 0.3*(arith0 + e1) + 1.5*(IQ + e2) + 0.6*sex + e3, 0)
#8. Parental education
Xeduc <- rmultinom(150, 1, c(0.3, 0.5, 0.2))
Xeduct <-t(Xeduc)
v<-c(0,1,2)
educ <- Xeduct %*% v
educf <- factor(educ,labels=c("low","middle","high"))
#9. Store variables in dataframe
mydata <- data.frame(ID, method, methodf, sex, sexf, IQ, arith0, arith1, educ, educf); mydata
#10. Save data
save(mydata, file="mydata.Rdata")
Reacties
Een reactie posten