0

I'm trying to create a multilevel data structure for power simulation using 'simr' package.

The data has 5 columns: pupil, class, extroversion, teacher experience, pupil gender. Pupils are nested in class, extroversion (grand mean centered,range between -4.21 and 4.78) and gender are pupil level predictors, and teacher experience is class level predictor (grand mean centered, range from -12 to 11). I want the dataframe to contain 600 observation (pupils), 20 nested in each of the 30 classes.

I'm able to create the dataframe using the below code, but it's aesthetically unacceptable by many standards.Another issue is I hope pupil extraversion and teacher experience can be normally distributed, my code seems to output a smaller range..

library(truncnorm)

set.seed(123)

pupil <- as.factor(seq(1:600))
class <- as.factor(rep(1:30,20))
extrav<-rtruncnorm(n=600,a=-4.21,b=4.78)
X <- cbind(pupil=pupil,class=class, extrav=extrav)

class<-seq(1:30)
texp<-rtruncnorm(30,-12,11)
class_texp<-cbind(class,texp)
X<-merge(X,class_texp)
sex<-rep(1:2,300)
sex<-sex-1
X<-cbind(X,sex)
View(X)
Tokaalmighty
  • 402
  • 4
  • 15
  • Be sure to list any non-base packages that you are using (`rtruncnorm` is not in base R or simr). Use `set.seed()` to make examples reproducible. What exactly counts as "aesthetically acceptable"? – MrFlick Apr 01 '19 at 19:20
  • I hope 'class' variable can be created using 1 line, but other methods I've tried seems to produce many more rows than I expect.. – Tokaalmighty Apr 01 '19 at 19:26

0 Answers0