+ All Categories
Home > Documents > Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful...

Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful...

Date post: 08-Oct-2020
Category:
Upload: others
View: 5 times
Download: 0 times
Share this document with a friend
31
Computer Lab 4: Useful Graphics for Multilevel Modeling C..J. Anderson Document Modified March/21/2020 Table of Contents 1 Set Up....................................................... 3 1.1. The following packages will be used......................3 1.2 Read in the data..........................................3 1.3 Create some variables by re-coding........................4 1.3.1 Dummy code gender:....................................4 1.3.2 Dummy code grade:..................................... 4 2.3.3 Center math around school mean math...................4 1.3.4 Make id a factor (not really neccesary)...............4 1.3.5 Put some variables on same scale (and check), so we’ll just do it now.............................................. 4 2. Draw random sample of data..................................5 3. Lattice Plot................................................ 5 (a) With points for students and linear regression for each school...................................................... 5 (b) Smooth.................................................. 6 4 Produce lattice (xyplots) plots for Hours TV.................6 (a)......................................................... 6 (b) Smooth regressions...................................... 6 5 Produce lattice (xyplots) plots for Hours Computer Games.....7 (a)......................................................... 7 (b) Smooth (loess) regressions..............................7 6 Regressions all schools in the same plot:....................7 (a)......................................................... 7 1
Transcript
Page 1: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

Computer Lab 4: Useful Graphics for Multilevel ModelingC..J. Anderson

Document Modified March/21/2020

Table of Contents1 Set Up.......................................................................................................................................................................... 3

1.1. The following packages will be used...................................................................................................3

1.2 Read in the data............................................................................................................................................. 3

1.3 Create some variables by re-coding......................................................................................................4

1.3.1 Dummy code gender:..........................................................................................................................4

1.3.2 Dummy code grade:.............................................................................................................................4

2.3.3 Center math around school mean math.....................................................................................4

1.3.4 Make id a factor (not really neccesary)......................................................................................4

1.3.5 Put some variables on same scale (and check), so we’ll just do it now........................4

2. Draw random sample of data.......................................................................................................................... 5

3. Lattice Plot............................................................................................................................................................... 5

(a) With points for students and linear regression for each school..........................................5

(b) Smooth.......................................................................................................................................................... 6

4 Produce lattice (xyplots) plots for Hours TV.............................................................................................6

(a)............................................................................................................................................................................ 6

(b) Smooth regressions.................................................................................................................................6

5 Produce lattice (xyplots) plots for Hours Computer Games...............................................................7

(a)............................................................................................................................................................................ 7

(b) Smooth (loess) regressions..................................................................................................................7

6 Regressions all schools in the same plot:....................................................................................................7

(a)............................................................................................................................................................................ 7

(b) Repeat part (a) but use the random sub-sample........................................................................8

7 Plot mean science by hours watching TV....................................................................................................8

8 Plot mean science by hours playing computer games...........................................................................9

9 Overlay school specific regressions of science on hours TV...............................................................9

(a) Linear `regression.................................................................................................................................... 9

(b) For smooth curves change the method..........................................................................................9

1

Page 2: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

10 Overlay school specific regressions of science on hours play computer games.....................9

11 Plot science by type of community...........................................................................................................10

(a) Using linear regression........................................................................................................................10

(b) Using Smooth curves............................................................................................................................10

(c) Check frequencies.................................................................................................................................. 11

12 R^2 and Meta R^2: Simple model.............................................................................................................12

13 R^2 and Meta R^2: More complex model..............................................................................................13

14 R^2 and meta R^2 for categorical TV and computer games.........................................................13

15 Comparision........................................................................................................................................................14

16 Easiest regression diagnostic to obtain..................................................................................................15

(a) Plot of Residuals..................................................................................................................................... 15

(b) Use HLMdiag to get conditional residuals..................................................................................15

17 A panel of diagnostic plots........................................................................................................................... 16

18 Influence diagnostics using HLMdiag functions.................................................................................18

(a) Cook’s distances..................................................................................................................................... 18

(b) Mdfits.......................................................................................................................................................... 18

19 Not required for Homework but informative......................................................................................19

Zeta plot............................................................................................................................................................. 19

Panel................................................................................................................................................................... 20

20 Not required Profile confidence intervals.............................................................................................21

21 Not required: Plot EBLUPs........................................................................................................................... 21

step 1 extract random effects.................................................................................................................. 21

step 2 put in separate objects..................................................................................................................21

step 3 sort by ranU which is “condval”................................................................................................21

step 4 Add intergers for schools so that I can plot in order.......................................................21

setp 5 Draw graph for U0j......................................................................................................................... 22

Now for U1j:..................................................................................................................................................... 22

QQplots of EBLUPs............................................................................................................................................ 23

Graphics are powerful tools; however, producing them can be challenging. This document is a semi-tutorial that will guide you through the steps to create graphics particularly useful in multilevel modeling. I have set up this document such that if in the future you want to produce a particular graphic, you can find an example and then copy the code (and modify to suit your analysis).

2

Page 3: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

In this computer lab, you will produce various graphics designed for exploaratory analysis and some diagnostic plots. I assume that you already know basic R.

1 Set Up

1.1. The following packages will be usedThe use of each package in this document is given next to the package name. If you need additonal information on these, check online for package documentation.

library(lme4) # Fitting HLM modelslibrary(lmerTest) # Sattherwait df and t-testslibrary(lattice) # Lattice/pannel plotslibrary(ggplot2) # Some nice grahicslibrary(stringi) # install before HLMdiag or HLMdiag won't load properlylibrary(HLMdiag) # Some diagnostic graphics for hlmlibrary(texreg) # Nice tabular outputlibrary(optimx) # If need to change optimizer

1.2 Read in the data.Note that the data is basically the same as lab 3, but all variables are present in this data set. You need to change the path to where your data live.

lab4<-read.table("D:/Dropbox/edps587/Lab_and_homework/Computer lab4/lab4_data.txt",header=TRUE)

Check the data

names(lab4)

## [1] "idschool" "idstudent" "grade" ## [4] "gender" "science" "math" ## [7] "genshortages" "hoursTV" "hourscomputergames"## [10] "typecommunity" "shortages" "isolate" ## [13] "rural" "suburban" "urban" ## [16] "short0" "short1" "short2" ## [19] "short3"

head(lab4)

## idschool idstudent grade gender science math genshortages hoursTV## 1 10 100101 3 girl 146.7 137.0 a_little 3## 2 10 100103 3 girl 148.8 145.3 a_little 2## 3 10 100107 3 girl 150.0 152.3 a_little 4

3

Page 4: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

## 4 10 100108 3 girl 146.9 144.3 a_little 3## 5 10 100109 3 boy 144.3 140.3 a_little 3## 6 10 100110 3 boy 156.5 159.2 a_little 2## hourscomputergames typecommunity shortages isolate rural suburban urban## 1 1 2 1 0 1 0 0## 2 3 2 1 0 1 0 0## 3 2 2 1 0 1 0 0## 4 1 2 1 0 1 0 0## 5 1 2 1 0 1 0 0## 6 1 2 1 0 1 0 0## short0 short1 short2 short3## 1 0 1 0 0## 2 0 1 0 0## 3 0 1 0 0## 4 0 1 0 0## 5 0 1 0 0## 6 0 1 0 0

1.3 Create some variables by re-coding

1.3.1 Dummy code gender:lab4$boy = ifelse(lab4$gender=="boy", 1, 0) lab4$boy[is.na(lab4$boy)] <- 1

1.3.2 Dummy code grade:lab4$third <- ifelse(lab4$grade==3, 1, 0)

2.3.3 Center math around school mean mathschool.mean <- as.data.frame(aggregate(math~idschool, data=lab4, "mean"))

names(school.mean) <- c('idschool', 'school.mean')

lab4 <- merge(lab4,school.mean, by=c('idschool'))

lab4$centered.math <- lab4$math - lab4$school.mean

1.3.4 Make id a factor (not really neccesary)lab4$idschool<-as.factor(lab4$idschool)

4

Page 5: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

1.3.5 Put some variables on same scale (and check), so we’ll just do it now.lab4$centered.math <- lab4$centered.math/sd(lab4$centered.math)

lab4$school.mean <- lab4$school.mean/sd(lab4$school.mean)

sd(lab4$centered.math)

## [1] 1

sd(lab4$school.mean)

## [1] 1

We are done with all the set up

2. Draw random sample of dataSince this is a very large data set, it is sometimes nice to have a smaller random sample to use. The following code will do this. The 1st line determines which schools are randomly sampled (without replacement), and the 2nd line extracts the data for the randomly sampled school.

groups <- unique(lab4$idschool)[sample(1:145,25)]

subset <- lab4[lab4$idschool%in%groups,]

3. Lattice PlotThe first graph is a lattice plot for looking at students within schools. We’ll Use the random sample and plot science by math scores (otherwise we’ld have 146 plots).

(a) With points for students and linear regression for each school

The basic command here is xyplot. The plot will have both points (‘p’) and linear regression lines (‘r’). I have also used the layout command below to specfiy that the panel should have 5 rows and 5 columns of plots. It sometimes will default to odd arrangements.

xyplot(science ~ math | idschool, data=subset, col.line=c('black','blue'), type=c('p','r'), main='Varability in Science ~ math relationship', xlab="Math Scores", ylab="Science Scores", layout=c(5,5) )

5

Page 6: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

A lattice plot where points are joined is illustrated in the lecture notes on longitudinal data. You do not need to do this, because it would be very hard to see what’s going on. To join points, use the option ‘l’ (“ell”) for line; that is, replace the type command by

type=c('p','l'),

(b) SmoothDo the same plot as in part (a), but use smooth regression rather than linear regression line. To do this change the type of plot to

type=c('p','smooth')

4 Produce lattice (xyplots) plots for Hours TVUsing the sub-sample of the data, draw lattice plot of science by hours of watching TV (as numeric variable). You can copy and edit the example given in part 3. Do this for the following 2 cases

(a)

Points and Linear regression lines

(b) Smooth regressions

Points and smooth lines

6

Page 7: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

5 Produce lattice (xyplots) plots for Hours Computer GamesUsing the sub-sample of the data, draw lattice plot of science by hours of of playing computer games (as numeric variable). You can copy and edit the example given in part 4. Do this for the following 2 cases

(a)

Points and Linear regression lines

(b) Smooth (loess) regressions

Points and smooth lines

6 Regressions all schools in the same plot:There are (at least) 2 ways of doing this. An easy way and a bit harder way. I prefer the look of the hard way. The plots produced by the harder way are in most of the lectures and the R code is on the course web-site that go with the lectures. The easier way is below.

Using ggplot

(a)ggplot(lab4, aes(x=math, y=science, col=idschool,type='l'), main='Regressions all in one plot') + geom_smooth(method="lm", se=FALSE ) + theme(legend.position="none")

7

Page 8: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

(b) Repeat part (a) but use the random sub-sample.

Perhaps this it is more informative using less data; that is, easier to see what’s going on.

7 Plot mean science by hours watching TVFirst compute the means

(y <- aggregate(science~hoursTV, data=lab4, "mean"))

## hoursTV science## 1 1 147.8550## 2 2 150.4784## 3 3 151.9487## 4 4 152.0076## 5 5 147.2271

and then plot Means by mean for hours watching TV,

plot(y[,1], y[,2], type='b', ylab='Mean Science', xlab='Hours Watching TV', main='Marginal of Science x HoursTV')

8

Page 9: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

8 Plot mean science by hours playing computer gamesYou can revise the code from item 7 to do this.

9 Overlay school specific regressions of science on hours TVYou will do this using linear regression and then smooth curves

(a) Linear `regressionggplot(lab4, aes(x=hoursTV, y=science, col=idschool,type='l')) + geom_smooth(method="lm", se=FALSE ) + theme(legend.position="none")

9

Page 10: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

(b) For smooth curves change the methodgeom_smooth(method="loess", se=FALSE)

10 Overlay school specific regressions of science on hours play computer gamesRepeat item 9 parts (a) and (b), except use hours play computer games.

11 Plot science by type of communitySince type of community is a nominal (discrete) varaible, we will add it to graphs by putting in distinct line for each community.

(a) Using linear regression

Frist change type of community to a factor and the graph.

lab4$community <- as.factor(lab4$typecommunity)

ggplot(lab4, aes(x=math, y=science, col=community, type='l')) + geom_smooth(method='lm', se=FALSE)

10

Page 11: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

(b) Using Smooth curves

For smooth curves change the “geom_smooth” option is indicated below.

ggplot(lab4, aes(x=math, y=science, col=community, type='l')) + geom_smooth(method='loess', se=FALSE)

11

Page 12: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

(c) Check frequencies

It may be the case that some communities are very small so when viewing graphs we may want to place less weight on them.

table(lab4$typecommunity)

## ## 1 2 3 4 ## 70 1042 2483 3502

Recall that “1” is isolate, “2” is rural, “3” is suburban, and “4” is urban.

An even better check, “discretize” science score and create 10 groups as follows

lab4$group <- cut(lab4$science,10)table(lab4$group,lab4$typecommunity)

## ## 1 2 3 4## (103,112] 1 0 1 2## (112,120] 0 1 2 5## (120,128] 1 5 17 39## (128,136] 1 47 109 261## (136,144] 5 161 390 708## (144,152] 11 338 817 1134## (152,161] 35 351 770 939## (161,169] 14 117 289 315

12

Page 13: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

## (169,177] 1 11 46 52## (177,185] 1 11 42 47

This table indicates there are certian combinations of communities and science that have no data and very little.

The same could be done for math.

12 R^2 and Meta R^2: Simple modelWe will use a very simple model to start with: science~math.

We need some basic information about the data and to set up objects to hold the results

N <- length(unique(lab4$idschool)) # Number of schoolsnj <- table(lab4$idschool) # number per schoolnj <- as.data.frame(nj)names(nj) <- c("idschool","nj")

ssmodel <- (0) # initialize SS modelsstotal <- (0) # initialize SS totalR2 <- matrix(99,nrow=N,ncol=2) # for saving R2s

We can now fit regressions for each school and save the R^2, sums of squares for model and total (corrected for total) for each school.

index <-1 # initializefor (i in (unique(lab4$idschool))) { sub <- lab4[ which(lab4$idschool==i),] # data for one school model0 <- lm(science ~ math, data=sub) # fit the model a <- anova(model0) # save anova table ssmodel <- ssmodel + a[1,2] # add to SS model sstotal <- sstotal + sum(a[,2]) # sum of all SS # save the R2 R2[index,1:2] <- as.numeric(cbind(i,summary(model0)$r.squared))

index <- index+1 # up-date index }

We also need to compute meta R^2 and do a bit of clean up before graphing the regsults

R2meta <- ssmodel/sstotal

R2 <- as.data.frame(R2)names(R2) <- c("idschool","R2")R2.mod1 <- merge(R2,nj,by=c("idschool"))

Notice that the R^2 is call “R2.mod1”. For different models, I will want a different R object name that holds the R^2 for each school.

13

Page 14: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

And at last a graph of R^2 by sample size…

plot(R2.mod1$nj,R2.mod1$R2, type='p', ylim=c(0,1), ylab="R squared", xlab="n_j (number of observations per school)", main="Science ~ math") abline(h=R2meta,col='blue') # adds line for R2metatext(70,0.95,'R2meta=.36',col="blue") # text of R2meta

13 R^2 and Meta R^2: More complex modelCompute and plot R-squares and R^2 meta from the regression of science on math, hours watching TV, and hours playing computer games. Treate both hours watching TV and hours playing computer games as NUMERIC variables. So that we can later compare models, call the R^2 for this model

R2.mod2

14 R^2 and meta R^2 for categorical TV and computer gamesCompute and plot R-squares and R^2 meta from the regressions of science on math, hours watching TV, and hours playing computer games. Treate hours watching TV and hours

14

Page 15: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

playing comptuer games are both CATEGORICAL variables. So that we can compare R^2s from various models, call them for this model

R2.mod3

## Warning in anova.lm(model0): ANOVA F-tests on an essentially perfect fit## are unreliable

15 ComparisionTo compare the R-squares from the models from item 13 (numeric hours) versus those from item 14 (categorical hours), plot the R-squares against each other and draw in reference line.

plot(R2.mod2$R2,R2.mod3$R2, cex=1.4, ylim=c(0,1), xlim=c(0,1), ylab='Hours as Discrete', xlab='Hours as Numeric', main='Hours Discrete vs Numeric')abline(a=0,b=1, col='blue') # adds a reference line

15

Page 16: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

16 Easiest regression diagnostic to obtainWe need a model to work with. We’ll use the as our model where both hours TV and hours computer games are treated as factors (categorical variables), which is defintely warented based on pervious graphs.

modelxx <- lmer(science ~ 1 + centered.math + school.mean + hrTV + hrCG + (1 + centered.math|idschool), data=lab4, REML=FALSE, control = lmerControl(optimizer ="Nelder_Mead"))

(a) Plot of Residuals

This is a plot of standardized (Pearson) residuals by the fitted contional values of science)

plot(modelxx, xlab='Fitted Conditional', ylab='Pearson Residuals')

Why do you suppose there is straight line of points in the graph?

(b) Use HLMdiag to get conditional residuals

The conditional residuals are computed using the fixed plus estimated random effects. We’ll look at these using a dotplot.

16

Page 17: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

# Get y-(X*gamma+Z*U) where U estimated by Empricial Bayesres1 <- HLMresid(modelxx, level=1, type="EB", standardize=TRUE)

head(res1) # look at what is here

## 1 2 3 4 5 6 ## 3.3748251 1.0883016 -2.2698068 -0.4434138 -0.8822511 1.3532460

Now that we have residuals we want, let’s take a look at them

dotplot(ranef(modelxx,condVar=TRUE), lattice.options=list(layout=c(1,2)))

## $idschool

17 A panel of diagnostic plots(similar to what SAS does):

# Want 4 plots in 2x2 array on pagepar(mfrow=c(2,2))

# Graph 1: scatter plot res1 <- HLMresid(modelxx, level=1, type="EB", standardize=TRUE)

17

Page 18: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

fit <- fitted(modelxx) # get conditional fitted valuesplot(fit,res1, xlab='Conditional Fitted Values', ylab='Pearson Std Residuals', main='Conditional Residuals')

# Graph 2: QQ plot and areference line

qqnorm(res1) # draws plotabline(a=0,b=9, col='blue') # reference line

# Graph 3: Historgram with overlay normalh<- hist(res1,breaks=15,density=20) # draw historgramxfit <- seq(-40, 40, length=50) # sets range & number quantilesyfit <- dnorm(xfit, mean=0, sd=7.177) # should be normalyfit <- yfit*diff(h$mids[1:2])*length(res1) # use mid-points lines(xfit, yfit, col='darkblue', lwd=2) # draws normal

# Graph 4: Information about the modelplot.new( ) # a plot with nothing in it.text(.5,0.8,'Modelxx') # potentially useful texttext(.5,0.5,'Deviance=48,356.5')text(.5,0.2,'AIC=48,386.5')

18

Page 19: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

18 Influence diagnostics using HLMdiag functions

(a) Cook’s distancescook <- cooks.distance(modelxx, group="idschool")

dotplot_diag(x=cook, cutoff="internal", name="cooks.distance", ylab=("Cook's distance"), xlab=("School"))

(b) Mdfitsmdfit <- mdffits(modelxx,group="idschool")dotplot_diag(x=mdfit, cutoff="internal", name="mdffits", ylab=("MDFIts"), xlab=("School"))

19

Page 20: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

19 Not required for Homework but informativeFor more on the interpretation of these plots, see the lme4 documentation or better yet Bates et al. (2105). Fitting linear mixed effects models using lme4. Journal of Statistical software.

Zeta plot

The blue lines in this and the next plot should be straight lines.

p1 <- profile(lmer(science ~ 1 + centered.math + school.mean +hrTV + hrCG + ( 1 + centered.math|idschool), lab4, REML=FALSE))

## Warning in FUN(X[[i]], ...): non-monotonic profile for .sig02

xyplot(p1 , aspect =0.7)

20

Page 21: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

https://www.jstatsoft.org/article/view/v067i01/0

Panelxyplot(p1 , aspect =0.7, absVal=TRUE)

21

Page 22: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

20 Not required Profile confidence intervalsHmmm, is there a problem here?

confint(p1, level=.99)

## Warning in confint.thpr(p1, level = 0.99): bad spline fit for .sig02:## falling back to linear interpolation

## 0.5 % 99.5 %## .sig01 1.53387241 2.3344245## .sig02 -0.05128605 0.8739596## .sig03 0.46712430 1.1273210## .sigma 7.00835180 7.3246400## (Intercept) -8.49642794 26.4606505## centered.math 4.75133021 5.3363461## school.mean 3.71846419 4.7720857## hrTV2 -0.62622860 1.2220820## hrTV3 -0.36287584 1.4585286## hrTV4 -0.11074220 1.9299122## hrTV5 -1.20189341 0.7586826## hrCG2 -0.31731560 0.7393753## hrCG3 -0.59903847 0.7817812## hrCG4 -1.40183779 0.7543930## hrCG5 -2.42348047 -0.4377490

21 Not required: Plot EBLUPs

step 1 extract random effectsrequire(lme4)modelxx <- lmer(science ~ 1 + centered.math + school.mean + hrTV + hrCG + (1 + centered.math|idschool), data=lab4, REML=FALSE, control = lmerControl(optimizer ="Nelder_Mead")) ranU.mer <- ranef(modelxx)ranU <- as.data.frame(ranU.mer)

step 2 put in separate objectsU0j <- ranU[which(ranU$term=="(Intercept)"),]U1j <- ranU[which(ranU$term=="centered.math"),]

step 3 sort by ranU which is “condval”U0j <-U0j[order(U0j$condval),]U1j <-U1j[order(U1j$condval),]

22

Page 23: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

step 4 Add intergers for schools so that I can plot in orderU0j$xgrp <- seq(1:146)U1j$xgrp <- seq(1:146)

setp 5 Draw graph for U0j

For sd errors, drew arrows but with no arrowhead…A hack solution

plot(U0j$xgrp,U0j$condval,type="p", main="Final Model U0j +/- 1 std error", xlab="Schools (sort by Uo)", ylab="EBLUP of U0j", col="blue")arrows(U0j$xgrp, U0j$condval-U0j$condsd, U0j$xgrp, U0j$condval+U0j$condsd, length=0.00)

Now for U1j:

For sd errors, drew arrows but with no arrowhead…A hack solution

plot(U1j$xgrp,U1j$condval,type="p", main="Final Model U1j +/- 1 std error", xlab="Schools (sort by U1j)", ylab="EBLUP of U1j", col="blue")

23

Page 24: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

arrows(U1j$xgrp, U1j$condval-U1j$condsd, U1j$xgrp, U1j$condval+U1j$condsd, length=0.00)

QQplots of EBLUPsqqdata <- qqnorm(U0j$condval)

24

Page 25: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

plot(qqdata$x,qqdata$y,type="p",pch=19,col="red",main="Normal QQ plot of U0j with 95% Confidence Bands", xlab="Theoretical Value", ylab="Estimated U0j (EBLUPS)", ylim=c(-5,5))arrows(qqdata$x, qqdata$y - 1.96*U0j$condsd, qqdata$x, qqdata$y + 1.96*U0j$condsd, length=0.00, col="gray")qqline(U0j$condval,col="steelblue")

25

Page 26: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

And the random slops for centered.math:

qqdata <- qqnorm(U1j$condval)

26

Page 27: Computer Lab 4: Useful Graphics for Multilevel Modeling€¦  · Web viewComputer Lab 4: Useful Graphics for Multilevel Modeling. C..J. Anderson. Document Modified March/21/2020.

plot(qqdata$x,qqdata$y,type="p",pch=19,col="red",main="Normal QQ plot of U1j with 95% Confidence Bands", xlab="Theoretical Value", ylab="Estimated Uuj (EBLUPS)", ylim=c(-5,5))

arrows(qqdata$x, qqdata$y - 1.96*U1j$condsd, qqdata$x, qqdata$y + 1.96*U1j$condsd, length=0.00, col="gray")qqline(U1j$condval,col="steelblue")

27


Recommended