Mikołaj Hnatiuk
2012-Nov-20 15:55 UTC
[R] Multilevel analysis using nlme (lme) . Error using z-scores
Hi, i am trying to learn something about multilevel analysis using a great "Discovering statistics using R". I constructed some sample data and then tried to fit a model. Generally model fits well, however when trying to fit the same model using z-score (standarizded) variables i got an error: Error in lme.formula(z_wyn ~ z_IQ + Kasa, data = la, random = ~z_IQ | : nlminb problem, convergence error code = 1 message = iteration limit reached without convergence (10) Below i attach all the code required to reproduce. My question is : is it a bug or a feature, in other words: is it normal for a model not to converge using z-scores ? If so, how to estimate standarized coefficents ? Or maybe i messed up something to begin with? Here is what i do: library(nlme) # I am creating random data # df of 900 rows la <- data.frame(1:900) # municipality: the variable i want to base my multilevel analysis on la$municipality[1:300] <- "Warszawa" # name of municipality: "Warsaw" la$municipality[301:600] <- "Płock" # la$municipality[601:900] <- "Gniezno" # # money that each municipality spends on schools (per student) la$money_per_municipality[1:300] <- 500 # name of municipality: "Warsaw" la$money_per_municipality[301:600] <- 300 #"Płock" # la$money_per_municipality[601:900] <- 200 #"Gniezno" # # In my model, i want to estimate student's achievments, #so i pick some random data, but I also want to #differentiate between the municipalities la$students_score[la$municipality=="Warszawa"] <- rnorm(n=nrow(la[la$municipality=="Warszawa",]),mean=33,sd=4) la$students_score[la$municipality=="Płock"] <- rnorm(n=nrow(la[la$municipality=="Płock",]),mean=22,sd=4) la$students_score[la$municipality=="Gniezno"] <-rnorm(n=nrow(la[la$municipality=="Gniezno",]),mean=5,sd=4) ### I assume IQ ~ achievement relation 2x + stderr la$IQ <- la$students_score * 2 + rnorm(n=nrow(la),mean=0,sd=30) with(la,cor(students_score,IQ)) #### [1] 0.6202439 ## convert factor/string to integer: la$mun <- as.integer(factor(la$municipality)) ### Z SCORES for IQ and achievements la$z_IQ <- (la$IQ-mean(la$IQ))/sd(la$IQ) la$z_sscore <- (la$students_score-mean(la$students_score))/sd(la$students_score) ## Estimate a model with random slopes based on IQ for each municipality. ##Again: z_IQ is plain IQ, money_per_municipiality does not very between municipialities # THIS runs without an error studentModel<- lme(z_sscore ~ IQ + money_per_municipality, data=la,random=~IQ|mun,method="ML") # THIS causes an error. The only difference is one variable (z_IQ instead of IQ) studentModel<- lme(z_sscore ~ z_IQ + money_per_municipality, data=la,random=~z_IQ|mun,method="ML") Output again: Error in lme.formula(z_sscore ~ z_IQ + money_per_municipality, data = la, : nlminb problem, convergence error code = 1 message = iteration limit reached without convergence (10) Best, Mikołaj Hnatiuk, Educational Research Institute, Warsaw, Poland [[alternative HTML version deleted]]