楼主: Lisrelchen
2182 11

Courses Notes: Applied Multilevel Modeling [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2016-5-29 05:29:17 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

Applied Multilevel Modeling

This course covers the theory and application of multilevel statistical models. Research data in the social sciences are often grouped in ways that impact our statistical analyses (e.g., in marital studies, spouses are more similar to one another than other study participants) and lead to interesting and substantive hypotheses (e.g., how do qualities of the relationship interact with an individual’s personality?). We will focus on why these types of data are problematic for classical statistics and the advantages of multilevel approaches. The seminar will heavily emphasize the practical application of multilevel models and rely on examples to demonstrate their need and application. We will cover the general aspects of multilevel models as well as their extension to longitudinal and multivariate data.

Note that the R code below is a modified version of the R code posted at UCLA’s ATS website for Singer and Willett’s Applied Longitudinal Data Analysis book:http://www.ats.ucla.edu/stat/R/examples/alda/




二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Multilevel Modeling Applied Courses Course individual another similar course impact

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2016-5-29 05:31:06
  1. ### R code to accompany Singer & Willett's ALDA book (12/27/06)
  2. #
  3. ################################################################################
  4. ###
  5. ###                         Chapter 2
  6. ###
  7. ################################################################################
  8. #
  9. ### NOTE: datasets can be downloaded from:
  10. #
  11. ### http://www.ats.ucla.edu/stat/examples/alda.htm
  12. #
  13. ### The R code below uses the "comma separated text files" linked toward the top
  14. ### of the page.
  15. #
  16. #inputting and printing the tolerance "person-level" data
  17. tolerance <- read.table(file = file.choose(), sep=",", header=TRUE)
  18. names(tolerance) <- tolower(names(tolerance))
  19. summary(tolerance)
  20. str(tolerance)
  21. print(tolerance)

  22. ### NOTE: perhaps something on converting wide to long formats using reshape lib
  23. #
  24. tol.df2 <- reshape(tolerance, idvar = "id", direction = "long",
  25.                   varying = list(names(tolerance)[2:6]), v.name = "tolerance")
  26. tol.df2 <- reshape(tolerance, idvar = "id", direction = "long",
  27.                   varying = list(c("tol11","tol12","tol13","tol14","tol15")), v.name = "tolerance")
  28. tol.df2$age <- tol.df2$time + 10
  29. ### see reshape library as well (and functions melt() and cast()
  30. #
  31. ### re-order data.frame by id and time
  32. tol.df2 <- with(tol.df2, tol.df2[order(id, time),])

  33. ### also see sort_df in reshape library

  34. #inputting and printing the tolerance "person-level" data
  35. tolerance.pp <- read.table(file = file.choose(), sep=",", header=T)
  36. names(tolerance.pp) <- tolower(names(tolerance.pp))
  37. summary(tolerance.pp)

  38. print(tolerance.pp[c(1:9, 76:80), ])

  39. #    Table 2.1, p. 20.
  40. #    Bivariate correlation among tolerance scores assessed on five occasions.

  41. round(cor(tolerance[ , 2:6]), 2)

  42. #    Fig. 2.2, p. 25.
  43. #    Empirical growth plots.

  44. library(lattice)  # needed for xyplot()
  45. tolerance.pp$id <- factor(tolerance.pp$id)
  46. ### NOTE: changing id to factor will print id levels in panels

  47. trellis.device(theme = col.whitebg)
  48. xyplot(tolerance ~ age | id, data = tolerance.pp, as.table = TRUE, ,
  49.       xlab = "AGE", ylab = "TOL", pch = 16, ylim = c(0, 4))

  50. #    Fig. 2.3, p. 27.
  51. #    Smooth nonparametric trajectories superimposed on empirical growth plots.

  52. ### NOTE: following won't work with panel.loess due to small n
  53. xyplot(tolerance ~ age | id, data = tolerance.pp,
  54.   panel = function(x, y){
  55.     panel.xyplot(x, y, pch = 16)
  56.     panel.lines(smooth.spline(x, y, df=4), type = "l", col = "red", lwd = 2)
  57.   }, xlab = "AGE", ylab = "TOL", ylim=c(0, 4), as.table=T)

  58. xyplot(tolerance ~ age | male, data = tolerance.pp, as.table = TRUE, ,
  59.       xlab = "AGE", ylab = "TOL", pch = 16, ylim = c(0, 4),
  60.       type = c("p","smooth"))
  61. #    Table 2.2, p. 30.
  62. #    Fitting separate within person OLS regression models.

  63. ### alternative using lmList
  64. library(nlme)

  65. ### fit regressions by id
  66. tol.lis <- lmList(tolerance ~ time | id, data = tolerance.pp)

  67. ### get individuals coefficients "augmented" by other variables in data.frame
  68. tol.lis.aug <- coef(tol.lis, augFrame = TRUE)
  69. names(tol.lis.aug)[1] <- "intercept"
  70. print(tol.lis.aug)

  71. ### summary of individual fits
  72. tol.lis.sum <- summary(tol.lis)
  73. print(tol.lis.sum)
  74. str(tol.lis.sum)

  75. ### plot 95% CI around intercepts and slopes
  76. plot(intervals(tol.lis))

  77. #    Fig. 2.4, p. 31.

  78. ### stem and leaf of intercepts
  79. stem(coef(tol.lis)[[1]])

  80. ### stem and leaf of slopes
  81. stem(coef(tol.lis)[[2]])

  82. # stem plot for R sq
  83. names(tol.lis.sum)
  84. stem(tol.lis.sum$r.squared)

  85. #    Fig. 2.5, p. 32.
  86. #    Fitted OLS trajectories superimposed on the empirical growth plots.

  87. trellis.device(theme = col.whitebg)
  88. xyplot(tolerance ~ age | id, data = tolerance.pp,
  89.   panel = function(x, y){
  90.     panel.xyplot(x, y)
  91.     panel.lmline(x, y)
  92.   }, xlab = "AGE", ylab = "TOL", ylim=c(0, 4), as.table=T)

  93. #    Fig. 2.6, p. 34.
  94. #    The collection of the trajectories of the raw data in the top panel; in the bottom panel is the collection of fitted OLS trajectories.

  95. #plot of the raw data
  96. trellis.device(theme = col.whitebg)
  97. xyplot(tolerance ~ age, data = tolerance.pp, groups = id,
  98.         panel = panel.superpose, panel.groups = panel.lmline,
  99.         xlab = "AGE", ylab = "TOL", ylim=c(0, 4))

  100. ### alternative with mean line
  101. trellis.device(theme = col.whitebg)
  102. xyplot(tolerance ~ age, data = tolerance.pp, groups = id,
  103.         panel = function(x, y, subscripts, groups){
  104.             panel.grid()
  105.             panel.superpose(x, y, subscripts, groups, panel.groups = "panel.lmline")
  106.             panel.lmline(x,y, lwd=3)
  107.             },
  108.         xlab = "AGE", ylab = "TOL", ylim=c(0, 4)) # auto.key = TRUE adds key but too big

  109. #    Table 2.3, p. 37
  110. #    Descriptive statistics of the estimates obtained by fitting the linear model by id.

  111. #obtaining the intercepts from linear model by id
  112. mean(coef(tol.lis)[[1]])
  113. sd(coef(tol.lis)[[1]])

  114. #obtaining the slopes from linear model by id
  115. mean(coef(tol.lis)[[2]])
  116. sd(coef(tol.lis)[[2]])
  117. cor(coef(tol.lis))

  118. #    Fig. 2.7, 38
  119. #    OLS fitted trajectories separated by levels of selected predictors.
  120. #    The two first panels are separated by gender; the two last are separated by levels of exposure.

  121. ### convert to categorical variables (ie, factors) to use in trellis plots
  122. tolerance.pp$male <- factor(tolerance.pp$male, 0:1, c("Male","Female"))
  123. tolerance.pp$exp.cut <- tolerance.pp$exposure < 1.145
  124. tolerance.pp$exp.cut <- factor(tolerance.pp$exp.cut, c(FALSE, TRUE), c("Low","High"))

  125. trellis.device(theme = col.whitebg)
  126. xyplot(tolerance ~ age | male, data = tolerance.pp, groups = id,
  127.       xlab = "AGE", ylab = "TOL", type = "r", lwd = 2)

  128. ### with mean line
  129. xyplot(tolerance ~ age | male, data = tolerance.pp, groups = id,
  130.         panel = function(x, y, subscripts, groups){
  131.             panel.grid()
  132.             panel.superpose(x, y, subscripts, groups,
  133.                             panel.groups = "panel.lmline")
  134.             panel.lmline(x,y, lwd=3)
  135.             }, xlab = "AGE", ylab = "TOL", ylim=c(0, 4))

  136. trellis.device(theme = col.whitebg)
  137. xyplot(tolerance ~ age | exp.cut, data = tolerance.pp, groups = id,
  138.       xlab = "AGE", ylab = "TOL", type = "r", lwd = 2)

  139. ### with mean line
  140. xyplot(tolerance ~ age | exp.cut, data = tolerance.pp, groups = id,
  141.         panel = function(x, y, subscripts, groups){
  142.             panel.grid()
  143.             panel.superpose(x, y, subscripts, groups,
  144.                             panel.groups = "panel.lmline")
  145.             panel.lmline(x,y, lwd=3)
  146.             }, xlab = "AGE", ylab = "TOL", ylim=c(0, 4))

  147. #    Fig. 2.8, p. 40.
  148. #    OLS estimates plotted against the predictors male and exposure.

  149. par(mfrow=c(2,2))
  150. plot(intercept ~ exposure, data = tol.lis.aug, pch = 16)
  151. plot(intercept ~ male, data = tol.lis.aug, col = "yellow") # boxplot by default
  152. plot(time ~ exposure, data = tol.lis.aug, pch = 16)
  153. plot(time ~ male, data = tol.lis.aug, col = "yellow") # boxplot by default

  154. ### correlations between OLS intecepts/slopes and predictors
  155. with(tol.lis.aug, cor(as.numeric(male), intercept))
  156. with(tol.lis.aug, cor(as.numeric(male), time))
  157. with(tol.lis.aug, cor(exposure, intercept))
  158. with(tol.lis.aug, cor(exposure, time))

  159. ### NOTE: something on reliability, precision, and variance?
复制代码

藤椅
Lisrelchen 发表于 2016-5-29 05:32:17
  1. ### R code to accompany Singer & Willett's ALDA book (12/27/06)
  2. #
  3. ################################################################################
  4. ###
  5. ###                         Chapter 3
  6. ###
  7. ################################################################################

  8. #    Inputting and printing the early intervention data set, table 3.1, p. 48.

  9. # reading in the opposites data
  10. #
  11. ### NOTE: the early intervention data used in Chapter 3 was not "released" to the
  12. ###       public by the researchers; thus, the data below use the "opposites" data
  13. ###       from Chapter 7, but the analyses map on directly to those done
  14. opposites <- read.table(file.choose(), header = TRUE, sep=",")
  15. names(opposites) <- tolower(names(opposites))
  16. summary(opposites)

  17. ### "cut" cog into low/high to mimic program variable in early intervention data
  18. opposites$cog2 <- cut(opposites$cog, breaks = 2, labels = c("Low","High"))

  19. ### change id to factor for correct labels in xyplot strips
  20. opposites$id <- factor(opposites$id)

  21. #Table 3.1, p. 48
  22. print(opposites[1:12,])

  23. #    Fig. 3.1, p. 50.
  24. #    OLS trajectories superimposed on the empirical growth plots.

  25. library(lattice)

  26. trellis.device(device = pdf, height = 8.5, width = 11,
  27.     file = "My plots.pdf", theme = col.whitebg)
  28. xyplot(opp ~ time | id, data = opposites, type = c("g","p","r"),
  29.         as.table = TRUE,
  30.         layout = c(4,3,3))
  31. ### insert more graphs...
  32. dev.off()

  33. ### for a more useful ordering, order by fitted intercept

  34. trellis.device(theme = col.whitebg)
  35. xyplot(opp ~ time | id, data = opposites, type = c("g","p","r"),
  36.         as.table = TRUE, index.cond = function(x, y) coef(lm(y ~ x))[1])

  37. ### same, but order by slopes
  38. xyplot(opp ~ time | id, data = opposites, type = c("g","p","r"),
  39.         as.table = TRUE, index.cond = function(x, y) coef(lm(y ~ x))[2])

  40. #    Fig. 3.3, p. 57.
  41. #    Fitted OLS trajectories and stem plots of fitted initial status and fitted rate of change by id.

  42. # fitting the linear model by id

  43. library(nlme)
  44. lis.3.3 <- lmList(opp ~ time | id, data = opposites)
  45. coef.3.3 <- coef(lis.3.3, augFrame = TRUE)
  46. sum.3.3 <- summary(lis.3.3)

  47. #plotting the linear fit by id
  48. trellis.device(theme = col.whitebg)
  49. xyplot(opp ~ time, data = opposites, groups = id,
  50.         panel = function(x, y, subscripts, groups){
  51.             panel.grid()
  52.             panel.superpose(x, y, subscripts, groups, panel.groups = "panel.lmline")
  53.             panel.lmline(x,y, lwd=3)
  54.             },
  55.         xlab = "OPP", ylab = "TIME") # auto.key = TRUE adds key but too big

  56. # stem plot for fitted initial value
  57. stem(coef.3.3[,1])

  58. #stem plot for fitted rate of change
  59. stem(coef.3.3[,2])

  60. #stem plot for sigma.sq
  61. stem(sum.3.3$r.squared)

  62. #    Fig. 3.4, p. 59.
  63. #    The top panel represents fitted OLS trajectories for program=0;
  64. #    the bottom panel represents fitted OLS trajectories for program=1.

  65. xyplot(opp ~ time | cog2, data = opposites, groups = id,
  66.        type = c("g","r"))
  67. xyplot(opp ~ time | cog2, data = opposites, groups = id,
  68.         panel = function(x, y, subscripts, groups){
  69.             panel.grid()
  70.             panel.superpose(x, y, subscripts, groups, panel.groups = "panel.lmline")
  71.             panel.loess(x,y, lwd=3)
  72.             },
  73.         xlab = "OPP", ylab = "TIME") # auto.key = TRUE adds key but too big

  74. #    Table 3.3, p. 69

  75. model.3.3 <- lme(opp ~ cog2*time, data = opposites,
  76.                 random = ~ time | id, method = "ML",
  77.                 control = list(msVerbose = TRUE, niterEM = 0, opt = "nlminb"))
  78. summary(model.3.3)

  79. #    Fig. 3.5 on page 71.

  80. pred.3.3 <- expand.grid(time = 0:3, cog2 = c("Low","High"))
  81. pred.3.3$pred <- predict(model.3.3, pred.3.3, level = 0)

  82. library(lattice)
  83. trellis.device(theme = col.whitebg)
  84. xyplot(pred ~ time, data = pred.3.3, groups = cog2,
  85.       type = c("g","l"), lwd = 2, auto.key = list(points = FALSE, lines = TRUE))


  86. model.3.3.1 <- update(model.3.3, opp ~ -1 + cog2/time)
  87. summary(model.3.3.1)

  88. library(gmodels)
  89. fixef(model.3.3.1)
  90. c.mat <- rbind(
  91.             'H vs. L at T0'   = c(1, -1, 0, 0),
  92.             'H vs. L at T1'   = c(1, -1, 1, -1),
  93.             'H vs. L at T2'   = c(1, -1, 2, -2),
  94.             'H vs. L at T3'   = c(1, -1, 3, -3),
  95.             'H vs. L at T4'   = c(1, -1, 4, -4)
  96.             )
  97. estimable(model.3.3.1, c.mat)
复制代码

板凳
Lisrelchen 发表于 2016-5-29 05:33:03
  1. ### R code to accompany Singer & Willett's ALDA book (12/27/06)
  2. #
  3. ################################################################################
  4. ###
  5. ###                         Chapter 4
  6. ###
  7. ################################################################################

  8. alcohol1 <- read.table(file = file.choose(), header = TRUE, sep=",")
  9. names(alcohol1) <- tolower(names(alcohol1))
  10. alcohol1$id <- factor(alcohol1$id)
  11. alcohol1$coa <- factor(alcohol1$coa, 0:1, c("Not CoA", "CoA"))

  12. #    Fig. 4.1, p. 77.
  13. #    Empirical growth plots with superimposed OLS trajectories.

  14. ### how to subset a data.frame
  15. mysub <- sample(levels(alcohol1$id), 5, replace = FALSE)
  16. sample(1:20, 5)
  17. ### subset the whole dataset by groups in "mysub"
  18. mysub.df <- subset(alcohol1, id %in% mysub)
  19. mysub.df <- subset(alcohol1, coa == "CoA")

  20. library(lattice)
  21. trellis.device(theme = col.whitebg)
  22. xyplot(alcuse ~ age | id,
  23.         data = alcohol1[alcohol1$id %in% c(4, 14, 23, 32, 41, 56, 65, 82), ],
  24.         type = c("p","r"),
  25.         ylim=c(-1, 4), as.table=T)

  26. xyplot(alcuse ~ age | id,
  27.         data = alcohol1,
  28.         type = c("p","r"),
  29.         ylim=c(-1, 4), as.table=T,
  30.         subset = alcohol1$id %in% mysub)


  31. #    Fig. 4.2, p.79.
  32. #    Fitted OLS trajectories displayed separately by coa status and peer levels.

  33. ### alternative
  34. library(nlme)
  35. alcuse.lis <- lmList(alcuse ~ age_14 | id, data = alcohol1)
  36. alcuse.lis.aug <- coef(alcuse.lis, augFrame = TRUE)
  37. names(alcuse.lis.aug)[1] <- "intercept"

  38. trellis.device(theme = col.whitebg)
  39. xyplot(alcuse ~ age | coa,
  40.       data = alcohol1,
  41.       groups = id,
  42.       type = "r", lwd = 2)

  43. ### alternate for peer plot - coplot
  44. coplot(alcuse ~ jitter(age) | peer, data = alcohol1,
  45.       panel = panel.smooth, lwd=2, col = "red", span=1)
  46.       
  47. #    Table 4.1, p. 94-95.

  48. # Model A
  49. model.a <- lme(alcuse ~ 1, data = alcohol1, random = ~1 |id,
  50.               method = "ML")
  51. summary(model.a)
  52. VarCorr(model.a)

  53. #Model B
  54. model.b <- lme(alcuse ~ age_14 , data = alcohol1, random = ~ age_14 | id,
  55.            method = "ML")
  56. summary(model.b)

  57. #Model C
  58. model.c <- update(model.b, alcuse ~ coa*age_14)
  59. summary(model.c)

  60. #Model D
  61. model.d <- update(model.b, alcuse ~ age_14*(coa + peer))
  62. summary(model.d)

  63. #Model E
  64. model.e <- update(model.b, alcuse ~ coa + peer*age_14)
  65. summary(model.e)

  66. #Model F
  67. model.f <- update(model.b, alcuse ~ coa + cpeer*age_14)
  68. summary(model.f)

  69. #Model G
  70. model.g <- update(model.b, alcuse ~ ccoa + cpeer*age_14)
  71. summary(model.g)

  72. ### compare models
  73. anova(model.a, model.b, model.c, model.d, model.e, model.f, model.g)

  74. #    Fig. 4.3, p. 99.
  75. #    Model B

  76. ### alternative
  77. #
  78. ### could use predict for all these...
  79. #
  80. ### model b
  81. pred.b <- data.frame(age_14 = 0:2)
  82. pred.b$pred <- predict(model.b, pred.b, level=0)
  83. plot(pred ~ I(age_14 + 14), data = pred.b, type = "l", lwd = 2,
  84.     ylim = c(0, 2), xlim = c(13, 17),
  85.     ylab = "Alcuse", xlab = "Age")

  86. ### model c
  87. pred.c <- expand.grid(age_14 = 0:2, coa = c("Not CoA","CoA"))
  88. pred.c$pred <- predict(model.c, pred.c, level=0)
  89. xyplot(pred ~ I(age_14 + 14), data = pred.c, type = "l", lwd = 2,
  90.       groups = coa, lty = 1:2, auto.key = list(points = FALSE, lines = TRUE),
  91.       ylim = c(0, 2), xlim = c(13, 17),
  92.       ylab = "Alcuse", xlab = "Age")

  93. ### model e
  94. #
  95. ### peer is continuous, so let's plot regression surface
  96. quantile(alcohol1$peer, c(0.05, 0.95))
  97. pred.e <- expand.grid(age_14 = 0:2, coa = c("Not CoA","CoA"),
  98.                       peer = seq(0, 2, 0.1))
  99. pred.e$pred <- predict(model.e, pred.e, level=0)
  100. trellis.device(theme = col.whitebg)
  101. wireframe(pred ~ I(age_14 + 14) + peer, data = pred.e, type = "l", lwd = 2,
  102.       groups = coa, lty = 1:2, auto.key = list(points = FALSE, lines = TRUE),
  103.       ylim = c(0, 2), xlim = c(13, 17),
  104.       zlab = "Alcuse", ylab = "Peer", xlab = "Age")

  105. ### NOTE: would be easier to see with dynamic 3D graph, eg, rgl or rggobi


  106. #    Fig. 4.4
  107. par(mfrow=c(2,2))
  108. plot(intercept ~ coa, data = alcuse.lis.aug, col = "yellow")
  109. plot(intercept ~ peer, data = alcuse.lis.aug, pch=16)
  110. abline(h=0)
  111. plot(age_14 ~ coa, data = alcuse.lis.aug, col = "yellow")
  112. plot(age_14 ~ peer, data = alcuse.lis.aug, pch=16)
  113. abline(h=0)

  114. #    Fig. 4.5, p. 131.
  115. #    Normality assumption plots.

  116. ### alternative
  117. qqnorm(model.f, ~ resid(., type = "r"))

  118. #    Upper right panel

  119. ### alternative
  120. plot(model.f, resid(., type = "p") ~ as.numeric(id), abline = 0, pch = 16)
  121. plot(model.f, resid(., type = "p") ~ fitted(.), abline = 0, pch = 16)

  122. ### NOTE: id was converted to a factor, and plot() wants a numeric var
  123. #    Middle left panel

  124. ### alternative
  125. qqnorm(model.f, ~ranef(.))

  126. #    Middle right panel

  127. ### alternative
  128. qqnorm(model.f, ~ranef(.), standard = TRUE, abline = c(0,1))
  129. apply(ranef(model.f, standard = TRUE), 2, mean)
  130. apply(ranef(model.f, standard = TRUE), 2, sd)

  131. ### NOTE: standardized REs don't have SD of 1...?

  132. plot(model.f, ranef(., standard = TRUE)[[1]] ~ as.numeric(id), abline = 0, pch = 16)
  133. plot(model.f, ranef(., standard = TRUE)[[2]] ~ as.numeric(id), abline = 0, pch = 16)

  134. #    Fig. 4.6, p. 133.
  135. #    Homoscedasticity plots.
  136. #

  137. ### alternative
  138. plot(model.f, resid(.) ~ age, xlim = c(13,17), abline = 0,
  139.     type = c("p","smooth"), lwd = 2)
  140. ### NOTE: smoother suggests some nonlinearity

  141. plot(model.f, ranef(.)[[1]] ~ coa, abline = 0, pch=16)
  142. plot(model.f, ranef(.)[[1]] ~ peer, abline = 0, pch=16,
  143.     type = c("p","smooth"), lwd = 2)
  144. plot(model.f, ranef(.)[[2]] ~ coa, abline = 0, pch=16)
  145. plot(model.f, ranef(.)[[2]] ~ peer, abline = 0, pch=16,
  146.     type = c("p","smooth"), lwd = 2)

  147. ###   Fig. 4.7, p. 136
  148. plot(comparePred(alcuse.lis, model.b, primary = ~ alcohol1$age_14, length.out = 2))
  149. ### NOTE: above not working quite right for some reason...

  150. plot(augPred(model.b, level = 0:1, primary = ~ age_14, length.out = 2))
复制代码

报纸
Lisrelchen 发表于 2016-5-29 05:33:40
  1. ################################################################################
  2. ###
  3. ###                         Chapter 5
  4. ###
  5. ################################################################################

  6. reading <- read.table(file = file.choose(), header=T, sep=",")
  7. names(reading) <- tolower(names(reading))
  8. reading$id <- factor(reading$id)
  9. summary(reading)

  10. #    Table 5.1, p. 141.

  11. reading[reading$id %in% c(4, 27, 31, 33, 41, 49, 69, 77, 87), ]

  12. #    Fig. 5.1, p. 143.
  13. #    Empirical change plots with superimposed OLS trajectories.
  14. #    The +'s and solid lines correspond to time using the child's target
  15. #    age at data collection, whereas the dots and dashed lines correspond
  16. #    to time using the child's observed age.

  17. xyplot(piat ~ age | id,
  18.       data = reading[reading$id %in% c(4, 27, 31, 33, 41, 49, 69, 77, 87), ],
  19.         panel = function(x, y, subscripts){
  20.                  panel.xyplot(x, y, pch=16)
  21.                  panel.lmline(x,y, lty=4)
  22.            panel.xyplot(reading$agegrp[subscripts], y, pch=3)
  23.                  panel.lmline(reading$agegrp[subscripts],y)
  24.     },
  25.     ylim=c(0, 80), as.table=T, subscripts=T)

  26. #    Creating the centered variables called agegrp.c and age.c, p. 144.

  27. mat2 <- reading[ ,3:4] - 6.5
  28. dimnames(mat2)[[2]] <- c("agegrp.c", "age.c")
  29. reading <- cbind(reading, mat2)

  30. ### or, a bit more transparently...
  31. reading$age.c <- reading$age - 6.5
  32. reading$agegrp.c <- reading$agegrp - 6.5

  33. #    Table 5.2, p. 145.
  34. #    Note: The degres of freedom used to in the calculations of the intercept
  35. #    is different from the results in the book and this difference in partitioning
  36. #    results also in slight differences in the standard error of the estimates.

  37. #Using the agegrp variable.
  38. lme.agegrp <- lme(piat ~ agegrp.c, reading, random = ~ agegrp.c | id, method="ML")
  39. summary(lme.agegrp)

  40. #Using the age variable.
  41. lme.age <- lme(piat ~ age.c, reading, random= ~ age.c | id, method="ML")
  42. summary(lme.age)

  43. #    Inputting the wages data set.

  44. wages <- read.table(file = file.choose(), header=T, sep=",")
  45. names(wages) <- tolower(names(wages))
  46. ### NOTE: for some reason, names have underscores and not periods
  47. names(wages) <- gsub("_",".", names(wages))

  48. wages$id <- factor(wages$id)
  49. wages$black <- factor(wages$black, 0:1, c("White/Latino","Black"))

  50. #    Table 5.3, p. 147.

  51. wages[wages$id %in% c(206, 332, 1028), c(1, 3, 2, 6, 8, 10)]

  52. #    Table 5.4, p. 149.

  53. #Model A
  54. model.5a <- lme(lnw ~ exper, wages, random= ~ exper | id, method="ML",
  55.                 control = list(msVerbose = TRUE, niterEM = 200))
  56. ### NOTE: boosting EM iterations speeds convergence
  57. summary(model.5a)

  58. #Model B
  59. model.5b <- update(model.5a, lnw ~ exper*hgc.9 + exper*black)
  60. summary(model.5b)

  61. #Model C
  62. model.5c <- update(model.5b, lnw ~ exper + exper:black + hgc.9)
  63. summary(model.5c)

  64. #    Fig. 5.2, p. 150.
  65. #    Log wage trajectories for four prototypical dropouts from model C.

  66. pred.5c <- expand.grid(exper = 0:11, black = c("White/Latino", "Black"),
  67.                       hgc.9 = c(0, 3))
  68. pred.5c$pred <- predict(model.5c, pred.5c, level=0)
  69. pred.5c$hgc.9 <- factor(pred.5c$hgc.9, c(0,3), c("9th Grade","12th Grade"))

  70. xyplot(pred ~ exper |hgc.9, data = pred.5c, groups = black, type = "l", lwd = 2,
  71.       ylab = "LNW", xlab = "EXPER")

  72. #    Inputting the small wages data set.

  73. wages.small <- read.table(file.choose(), header=T, sep=",")
  74. names(wages.small) <- tolower(names(wages.small))
  75. ### NOTE: for some reason, names have underscores and not periods
  76. names(wages.small) <- gsub("_",".", names(wages.small))

  77. #    Table 5.5, p. 154.

  78. #Model A
  79. model.5.5a <- lme(lnw ~ exper + hgc.9 + exper:black, wages.small,
  80.                   random = ~ exper | id, method = "ML",
  81.                   control = list(msVerbose = TRUE, niterEM = 1000))
  82. ### NOTE: jack up EM iterations to 1000
  83. summary(model.5.5a)

  84. #Model C
  85. model.5.5c <- update(model.5.5a, random = ~ 1 | id)
  86. anova(model.5.5c, model.5.5a)

  87. ### Simpler model preferred

  88. #    Inputting the unemployment data set.

  89. unemployment <- read.table(file = file.choose(), header=T, sep=",")
  90. names(unemployment) <- tolower(names(unemployment))
  91. unemployment$id <- factor(unemployment$id)

  92. #    Table 5.6, p. 161.

  93. unemployment[unemployment$id %in% c(7589, 55697, 67641, 65441, 53782),]

  94. #    Table 5.7, p. 163.

  95. #Model A
  96. model.5.7a <- lme(cesd ~ months, data = unemployment, random = ~ months | id,
  97.               control = list(msVerbose = TRUE, niterEM = 100), method="ML")
  98. summary(model.5.7a)
  99. qqnorm(model.5.7a, ~ resid(., type = "r"))
  100. hist(resid(model.5.7a), col = "yellow"))
  101. ### NOTE: bit of skew in residuals

  102. #Model B
  103. model.5.7b <- update(model.5.7a, cesd ~ months + unemp)
  104. summary(model.5.7b)

  105. #Model C
  106. model.5.7c <- update(model.5.7b, cesd ~ months*unemp)
  107. summary(model.5.7c)

  108. #Model D
  109. model.5.7d <- update(model.5.7b, cesd ~ unemp + months:unemp,
  110.                     random = ~ unemp + months:unemp | id,
  111.                     control = list(msVerbose = TRUE, niterEM = 100,
  112.                                     opt = "optim"))
  113. summary(model.5.7d)
  114. ### NOTE: interesting that switching the optimizer to "optim" vs. "nlminb"
  115. ###       made the difference in convergence...

  116. #    Fig. 5.3, p. 165
  117. pred.5.3 <- data.frame(months = rep(0:15, 4),
  118.                         unemp = c(rep(1, 16), rep(c(1,0), c(6,10)),
  119.                                   rep(c(1,0), c(11,5)), rep(c(1,0,1), c(6, 5, 5))))
  120. pred.5.3$quad <- factor(rep(1:4, each = nrow(pred.5.3)/4))
  121. pred.5.3$pred <- predict(model.5.7b, pred.5.3, level=0)

  122. trellis.device(theme = col.whitebg)
  123. xyplot(pred ~ months | quad, data = pred.5.3, type = "l", lwd = 2,
  124.       ylab = "CES-D", xlab = "Months since job loss")
  125. ### NOTE: with significant futzing, we could make it identical...

  126. #    Fig. 5.4, p. 167.

  127. pred.5.7 <- expand.grid(months = 0:15, unemp = c(0,1))
  128. pred.5.7$pred.b <- predict(model.5.7b, pred.5.7, level = 0)
  129. pred.5.7$pred.c <- predict(model.5.7c, pred.5.7, level = 0)
  130. pred.5.7$pred.d <- predict(model.5.7d, pred.5.7, level = 0)

  131. xyplot(pred.b ~ months, data = pred.5.7, groups = unemp,
  132.       type = "l", lwd = 2, lty = 1:2, auto.key = list(lines = TRUE),
  133.       ylab = "CES-D", xlab = "Months since job loss")
  134. xyplot(pred.c ~ months, data = pred.5.7, groups = unemp,
  135.       type = "l", lwd = 2, lty = 1:2, auto.key = list(lines = TRUE),
  136.       ylab = "CES-D", xlab = "Months since job loss")
  137. xyplot(pred.d ~ months, data = pred.5.7, groups = unemp,
  138.       type = "l", lwd = 2, lty = 1:2, auto.key = list(lines = TRUE),
  139.       ylab = "CES-D", xlab = "Months since job loss")

  140. #    Table 5.8, p. 175.

  141. #Model A
  142. model.5.8a <- lme(lnw ~ hgc.9 + ue.7 + exper + exper:black, data = wages,
  143.                   random = ~ exper | id, method = "ML",
  144.                   control = list(msVerbose = TRUE, niterEM = 100))
  145. summary(model.5.8a)

  146. #Model B
  147. ### NOTE: don't see where
  148. model.5.8b <- update(model.5.8a, lnw ~ hgc.9 + ue.mean + ue.person.centered +
  149.                                   exper + exper:black)
  150. summary(model.5.8b)

  151. #Model C
  152. model.5.8c <- update(model.5.8b, lnw ~ hgc.9 + ue1 + ue.centert1 + exper + exper:black)
  153. summary(model.5.8c)

  154. #    Inputting the medication data set.

  155. medication <- read.table(file = file.choose(), header=T, sep=",")
  156. names(medication) <- tolower(names(medication))
  157. medication$id <- factor(medication$id)

  158. #    Table 5.9, p. 182.

  159. medication[c(1:6, 11, 16:21), c(3:8)]

  160. #    Table 5.10, p. 184.

  161. #Using time (Model A)
  162. model.5.10a <- lme(pos ~ treat*time, data = medication,
  163.                     random = ~ time | id, method = "ML")
  164. summary(model.5.10a)

  165. #Using time - 3.33 (Model B)
  166. model.5.10b <- update(model.5.10a, pos ~ treat*time333)
  167. summary(model.5.10b)

  168. #Using time - 6.67 (Model C)
  169. model.5.10c <- update(model.5.10b, pos ~ treat*time667)
  170. summary(model.5.10c)

  171. #    Fig. 5.5, p. 185.
  172. #    Note: The vertical lines reflect the magnitude of the effect of treatment
  173. #    when time is centered at different values.

  174. days.seq <- seq(0, 7)
  175. fixef.a <- fixef(model.5.10a)

  176. trt <- fixef.a[[1]] + fixef.a[[2]] + (fixef.a[[3]]+fixef.a[[4]])*days.seq
  177. cnt <- fixef.a[[1]] + fixef.a[[3]]*days.seq

  178. ### NOTE: most of these plots look better with PDF or postscript
  179. pdf("Figure 5.5.pdf", height = 8, width = 10.5)
  180. plot(days.seq, trt, ylim=c(140, 190), xlim=c(0, 7), type="l", lwd = 2,
  181.      xlab="Days", ylab=expression(widehat(POS)))
  182. lines(days.seq, cnt, lty=4, lwd = 2)
  183. legend(0, 190, c("treatment", "control"), lty=c(1, 4), lwd = 2)
  184. segments(0, fixef.a[[1]] + fixef.a[[3]]*0, 0,
  185.         fixef.a[[1]] + fixef.a[[2]] + (fixef.a[[3]]+fixef.a[[4]])*0, lty = 2)
  186. segments(3.33, fixef.a[[1]] + fixef.a[[3]]*3.33, 3.33,
  187.         fixef.a[[1]] + fixef.a[[2]] + (fixef.a[[3]]+fixef.a[[4]])*3.33, lty = 2)
  188. segments(6.670, fixef.a[[1]] + fixef.a[[3]]*6.670, 6.670,
  189.         fixef.a[[1]] + fixef.a[[2]] + (fixef.a[[3]]+fixef.a[[4]])*6.670, lty = 2)
  190. dev.off()
复制代码

地板
Lisrelchen 发表于 2016-5-29 05:34:17
  1. ################################################################################
  2. ###
  3. ###                         Chapter 6
  4. ###
  5. ################################################################################

  6. #    Table 6.1, p. 192.
  7. #    Creating the ged*exper variable in the wages data set.

  8. wages$ged.exper <- wages$ged*wages$exper
  9. print(wages[wages$id %in% c(206,2365,4384),c(1:5, 16)])

  10. #    Table 6.2, p. 203.

  11. model.6.2a <- lme(lnw ~ exper + hgc.9 + exper:black + ue.7, data = wages,
  12.                   random = ~ exper | id, method = "ML",
  13.                   control = list(msVerbose = TRUE, opt = "optim", trace = 1))
  14. 2*model.6.2a$logLik

  15. model.6.2b <- update(model.6.2a, lnw ~ exper + hgc.9 + exper:black + ue.7 + ged,
  16.                     random = ~ exper + ged | id)
  17. 2*model.6.2b$logLik

  18. model.6.2c <- update(model.6.2b, random = ~ exper | id)
  19. 2*model.6.2c$logLik

  20. anova(model.6.2a, model.6.2c, model.6.2b)

  21. model.6.2d <- update(model.6.2a, lnw ~ exper + hgc.9 + exper:black + ue.7 + postexp,
  22.                       random = ~ exper + postexp | id,
  23.                       control = list(msVerbose = TRUE, niterEM = 100, opt = "optim"))
  24. 2*model.6.2d$logLik

  25. anova(model.6.2a, model.6.2d)

  26. model.6.2e <- update(model.6.2d, random = ~ exper| id)
  27. 2*model.6.2e$logLik

  28. model.6.2f <- update(model.6.2a, lnw ~ exper + hgc.9 + exper:black + ue.7 + postexp + ged,
  29.                       random = ~ exper + postexp + ged | id,
  30.                       control = list(msVerbose = TRUE, niterEM = 500, opt = "optim"))
  31. ### NOTE: with big var-cov models, a bigger EM burn-in with optim boosts convergence
  32. ###       notably
  33. 2*model.6.2f$logLik

  34. model.6.2g <- update(model.6.2f, random = ~ exper + ged | id)
  35. 2*model.6.2g$logLik

  36. model.6.2h <- update(model.6.2f, random = ~ exper + postexp | id)
  37. 2*model.6.2h$logLik

  38. model.6.2i <- update(model.6.2f, lnw ~ exper + hgc.9 + exper:black + ue.7 + ged + exper:ged,
  39.                     random = ~ exper + ged + exper:ged | id)
  40. 2*model.6.2i$logLik

  41. model.6.2j <- update(model.6.2i, random = ~ ged + exper | id)
  42. 2*model.6.2j$logLik

  43. ### NOTE: examine all models; LR tests won't (all) be correct, but AIC/BIC can be
  44. ###       compared
  45. anova(model.6.2a, model.6.2b, model.6.2c, model.6.2d, model.6.2e, model.6.2f,
  46.       model.6.2g, model.6.2h, model.6.2i, model.6.2j)
  47. ### NOTE: AIC prefers model F, but BIC prefers several ahead of F with E its favorite

  48. #    Table 6.3, p. 205.
  49. #    Summary of model F.

  50. summary(model.6.2f)
  51. pairs(model.6.2f, pch = ".", cex = 2)
  52. ### NOTE: notable correlations among random-effects

  53. #    Fig. 6.4, p. 209.
  54. #    We must first read in the alcohol data set and then run model e from table 4.1, p. 94.

  55. ### we can do these using predict
  56. pred.6.4e <- expand.grid(age_14 = seq(0, 2, 0.1), coa = c("Not CoA","CoA"),
  57.                       peer = c(0.655, 1.381))
  58. pred.6.4e$pred <- predict(model.e, pred.6.4e, level=0)
  59. pred.6.4e$pred <- pred.6.4e$pred^2  # square it to go back to original metric

  60. trellis.device(theme = col.whitebg)
  61. xyplot(pred ~ I(age_14 + 14) | peer, data = pred.6.4e, type = "l", lwd = 2,
  62.       groups = coa, lty = 1:2, auto.key = list(points = FALSE, lines = TRUE, lwd=2),
  63.       xlim = c(13, 17),
  64.       ylab = "Alcuse", xlab = "Age")

  65. #reading in the alcohol data
  66. alcohol <- read.table("d:/alcohol1_pp.txt", header=T, sep=",")

  67. #table 4.1, model e
  68. model.e <- lme(alcuse~coa+peer*age_14 , data=alcohol, random= ~ age_14 | id,
  69.            method="ML")
  70. #obtaining the fixed effects parameters
  71. fixef.e <- fixef(model.e)
  72. #obtaining the predicted values and squaring them
  73. fit2.ec0p0 <- (fixef.e[[1]] + .655*fixef.e[[3]] +
  74.              alcohol$age_14[1:3]*fixef.e[[4]] +
  75.              .655*alcohol$age_14[1:3]*fixef.e[[5]])^2
  76. fit2.ec0p1 <- (fixef.e[[1]] + 1.381*fixef.e[[3]] +
  77.              alcohol$age_14[1:3]*fixef.e[[4]] +
  78.              1.381*alcohol$age_14[1:3]*fixef.e[[5]] )^2
  79. fit2.ec1p0 <- (fixef.e[[1]] + fixef.e[[2]] + .655*fixef.e[[3]] +
  80.              alcohol$age_14[1:3]*fixef.e[[4]] +
  81.              .655*alcohol$age_14[1:3]*fixef.e[[5]] )^2
  82. fit2.ec1p1 <- (fixef.e[[1]] + fixef.e[[2]] + 1.381*fixef.e[[3]] +
  83.              alcohol$age_14[1:3]*fixef.e[[4]] +
  84.              1.381*alcohol$age_14[1:3]*fixef.e[[5]])^2

  85. plot(alcohol1$age[1:3], fit2.ec0p0, ylim=c(0, 3), type="n",
  86.      ylab="predicted alcuse squared", xlab="age")
  87. lines(spline(alcohol1$age[1:3], fit2.ec0p0), pch=2, type="b")
  88. lines(spline(alcohol1$age[1:3], fit2.ec0p1), type="b", pch=0)
  89. lines(spline(alcohol1$age[1:3], fit2.ec1p0), type="b", pch=17)
  90. lines(spline(alcohol1$age[1:3], fit2.ec1p1), type="b", pch=15)

  91. title("Non-linear Change")
  92. legend(14, 3, marks=c(2, 0, 17, 15), c("COA=0, low peer", "COA=0, high peer",
  93.        "COA=1, low peer", "COA=1, high peer"))

  94. #    Reading in the berkeley data set and then creating the transformed variables for iq and age.

  95. berkeley <- read.table(file.choose(), header=T, sep=",")
  96. names(berkeley) <- tolower(names(berkeley))
  97. berkeley$age2.3 <- berkeley$age^(1/2.3)
  98. berkeley$iq2.3 <- berkeley$iq^2.3

  99. #    Fig. 6.6, p. 212.

  100. par(mfrow=c(1,3))
  101. plot(berkeley$age, berkeley$iq, type="p", ylim=c(0, 250), xlim=c(0, 60),
  102.      ylab="IQ", xlab="TIME", pch=16)
  103. plot(berkeley$age, berkeley$iq2.3, type="p", ylim=c(0, 300000), xlim=c(0, 60),
  104.      ylab="IQ^(2.3)", xlab="TIME", pch=16)
  105. plot(berkeley$age2.3, berkeley$iq, type="p", ylim=c(0, 250), xlim=c(0, 6),
  106.      ylab="IQ", xlab="TIME^(1/2.3)", pch=16)

  107. ### Table 6.4?

  108. #    Reading in the external data set.

  109. external <- read.table(file.choose(), header = TRUE, sep=",")
  110. names(external) <- tolower(names(external))

  111. #    Creating the higher order variables for grade.
  112. #
  113. ### NOTE: not needed...
  114. external$grade2 <- external$grade^2
  115. external$grade3 <- external$grade^3
  116. external$grade4 <- external$grade^4

  117. #    Fig. 6.7, p. 218.

  118. par(mfrow=c(2,4))
  119. model.6.7a <- lm(external ~ poly(grade, 2), data = external,
  120.                   subset = id == "1")
  121. plot(1:6, fitted(model.6.7a), type = "l", lwd = 2, col = "red",
  122.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  123. points(1:6, subset(external, id == "1", external)[[1]], pch = 16)
  124. lines(spline(1:6, subset(external, id == "1", external)[[1]]), lty = 3)

  125. model.6.7b <- lm(external ~ poly(grade, 2), data = external,
  126.                   subset = id == "6")
  127. plot(1:6, fitted(model.6.7b), type = "l", lwd = 2, col = "red",
  128.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  129. points(1:6, subset(external, id == "6", external)[[1]], pch = 16)
  130. lines(spline(1:6, subset(external, id == "6", external)[[1]]), lty = 3)

  131. model.6.7c <- lm(external ~ grade, data = external,
  132.                   subset = id == "11")
  133. plot(1:6, fitted(model.6.7c), type = "l", lwd = 2, col = "red",
  134.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  135. points(1:6, subset(external, id == "11", external)[[1]], pch = 16)
  136. lines(spline(1:6, subset(external, id == "11", external)[[1]]), lty = 3)
  137. ### NOTE: looks like a quadratic would fit better...

  138. model.6.7d <- lm(external ~ grade, data = external,
  139.                   subset = id == "25")
  140. plot(1:6, fitted(model.6.7d), type = "l", lwd = 2, col = "red",
  141.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  142. points(1:6, subset(external, id == "25", external)[[1]], pch = 16)
  143. lines(spline(1:6, subset(external, id == "25", external)[[1]]), lty = 3)

  144. model.6.7e <- lm(external ~ poly(grade, 3), data = external,
  145.                   subset = id == "34")
  146. plot(1:6, fitted(model.6.7e), type = "l", lwd = 2, col = "red",
  147.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  148. points(1:6, subset(external, id == "34", external)[[1]], pch = 16)
  149. lines(spline(1:6, subset(external, id == "34", external)[[1]]), lty = 3)

  150. model.6.7f <- lm(external ~ poly(grade, 4), data = external,
  151.                   subset = id == "36")
  152. plot(1:6, fitted(model.6.7f), type = "l", lwd = 2, col = "red",
  153.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  154. points(1:6, subset(external, id == "36", external)[[1]], pch = 16)
  155. lines(spline(1:6, subset(external, id == "36", external)[[1]]), lty = 3)

  156. model.6.7g <- lm(external ~ poly(grade, 2), data = external,
  157.                   subset = id == "40")
  158. plot(1:6, fitted(model.6.7g), type = "l", lwd = 2, col = "red",
  159.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  160. points(1:6, subset(external, id == "40", external)[[1]], pch = 16)
  161. lines(spline(1:6, subset(external, id == "40", external)[[1]]), lty = 3)

  162. model.6.7h <- lm(external ~ poly(grade, 2), data = external,
  163.                   subset = id == "26")
  164. plot(1:6, fitted(model.6.7h), type = "l", lwd = 2, col = "red",
  165.     ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  166. points(1:6, subset(external, id == "26", external)[[1]], pch = 16)
  167. lines(spline(1:6, subset(external, id == "26", external)[[1]]), lty = 3)

  168. ### alternative in a for loop
  169. ex.ids <- c("1","6","11","25","34","36","40","26")

  170. par(mfrow=c(2,4))
  171. for (i in 1:length(ex.ids)){
  172.     fit <- lm(external ~ poly(grade, 4), data = external,
  173.                   subset = id == ex.ids[i])
  174.     pred <- predict(fit, data.frame(grade = seq(1, 6, 0.1)))
  175.     plot(x = seq(1, 6, 0.1), y = pred, type = "l", lwd = 2, col = "red",
  176.           ylim = c(0,60), ylab = "EXTERNAL", xlab = "GRADE")
  177.     points(1:6, subset(external, id == ex.ids[i], external)[[1]], pch = 16)
  178.     lines(spline(1:6, subset(external, id == ex.ids[i], external)[[1]]), lty = 3)
  179.     }
  180. ### NOTE: could use drop1() to find best fit if we wanted...

  181. #    Table 6.5, p. 221.
  182. #    Comparison of fitting alternative polynomial change trajectories to the external data set.

  183. model.6.5a <- lme(external ~ 1, random =  ~ 1 | id, method = "ML", external)
  184. summary(model.6.5a)

  185. model.6.5b <- update(model.6.5a, external ~ time, random =  ~ time | id)
  186. summary(model.6.5b)
  187. ### NOTE: could use poly(time, degree) for orthogonal polynomials

  188. model.6.5c <- update(model.6.5a, external ~ time + I(time^2),
  189.                       random =  ~ time + I(time^2) | id,
  190.                       control = list(msVerbose = TRUE, niterEM = 100, opt = "optim"))
  191. summary(model.6.5c)

  192. model.6.5d <- update(model.6.5c, external ~ time + I(time^2) + I(time^3),
  193.                     random = ~ time + I(time^2) + I(time^3) | id)
  194. summary(model.6.5d)
  195. pairs(model.6.5d, pch = 16) # super high correlations

  196. model.6.5d.1 <- update(model.6.5c, external ~ poly(time, 3),
  197.                     random = ~ poly(time, 3) | id)
  198. summary(model.6.5d.1)
  199. ### NOTE: converges faster with orthogonal polynomials, which remove non-essential
  200. ###       correlation

  201. #    Reading in the fox and geese data.

  202. fg.df <- read.table(file.choose(), header = TRUE, sep=",")
  203. names(fg.df) <- tolower(names(fg.df))
  204. fg.df$id <- factor(fg.df$id)
  205. str(fg.df)

  206. #    Fig. 6.8, p. 227.
  207. #    Empirical growth plots for 8 children in the fox and geese data.

  208. xyplot(nmoves ~ game | id,
  209.         data=fg.df[fg.df$id %in% c(1, 4, 6, 7, 8, 11, 12, 15), ],
  210.         ylim=c(0, 25), as.table = T, pch = 16)

  211. #    Table 6.6, p. 231.
  212. #    Fitting a logistic model to the fox and geese data.

  213. model.6.6a <- nlme(nmoves ~ 1 + 19/(1 + xmid*exp( -scal*game + u)),
  214.                     fixed = scal + xmid ~ 1,
  215.                     random = scal + u ~ 1 |id,
  216.                     start = c(scal = 0.2, xmid = 12),
  217.                     data = fg.df,
  218.                     control = list(msVerbose = TRUE, niterEM = 100))
  219. summary(model.6.6a)


  220. model.6.6b <- nlme(nmoves ~ 1 + 19/(1 + xmid*exp(-scal10*game - scal01*read.c - scal11*readc.game + u)),
  221.                     fixed = scal10 + scal01 + scal11 + xmid ~ 1,
  222.                     random = scal10 + u ~ 1 |id,
  223.                     start = c(scal10 = 0.12, scal01 = -0.4, scal11 = 0.04, xmid = 12),
  224.                     data = fg.df)
  225. summary(model.b)
  226. ### NOTE: need to check book for read.c and readc.game

  227. #    Fig. 6.10, p. 232.

  228. fixef.a <- fixef(model.a)
  229. fit.a <- 1 + 19/(1 + fixef.a[[2]]*exp(-fixef.a[[1]]*fg.df$game[1:27]))

  230. plot(fg.df$game[1:27], fit.a, ylim=c(0, 25), type="l",
  231.      ylab="predicted nmoves", xlab="game")
  232. title("Model A \n Unconditional logistic growth")

  233. fixef.b <- fixef(model.b)
  234. fit.b.high <- 1 + 19/(1+fixef.b[[4]]*exp(-fixef.b[[1]]*fg.df$game[1:27] -
  235.                  fixef.b[[2]]*1.58 - fixef.b[[3]]*1.58*fg.df$game[1:27]))

  236. fit.b.low <- 1 + 19/(1+fixef.b[[4]]*exp(-fixef.b[[1]]*fg.df$game[1:27] -
  237.                  fixef.b[[2]]*(-1.58) - fixef.b[[3]]*(-1.58)*fg.df$game[1:27]))

  238. plot(fg.df$game[1:27], fit.b.high, ylim=c(0, 25), type="l",
  239.      ylab="predicted nmoves", xlab="game")
  240. lines(fg.df$game[1:27], fit.b.low, lty=3)

  241. title("Model B \n Fitted logistic growth by reading level")
  242. legend(1, 25, c("High Reading level","Low reading level"), lty=c(1, 3))
复制代码

7
Lisrelchen 发表于 2016-5-29 05:34:56
  1. ################################################################################
  2. ###
  3. ###                         Chapter 7
  4. ###
  5. ################################################################################

  6. opposites <- read.table(file.choose(), header = TRUE, sep=",")
  7. names(opposites) <- tolower(names(opposites))
  8. opposites$id <- factor(opposites$id)

  9. #    Table 7.2, p. 246.

  10. opp.reml <- lme(opp ~ time*ccog,
  11.                 data = opposites,
  12.                 random = ~ time | id,
  13.                 control = list(msVerbose = TRUE, niterEM = 100, opt = "optim"))
  14. summary(opp.reml)

  15. #    Table 7.3, p. 258-259.

  16. ### NOTE: why did S&W drop the random-effects in fitting these models?
  17. #
  18. #compound symmetry
  19. fit.cs <- gls(opp ~ time * ccog, data = opposites,
  20.               correlation = corCompSymm(form =  ~ time | id))
  21. summary(fit.cs) ; 2 * logLik(fit.cs) ; fit.cs$sigma^2

  22. #autoregressive
  23. fit.ar1 <- gls(opp ~ time * ccog, data = opposites,
  24.                 corr = corAR1(form =  ~ wave | id))
  25. 2 * logLik(fit.ar1) ; fit.ar1$sigma^2

  26. #    Table 7.4, p. 265.

  27. # Standard error covariance structure
  28. summary(opp.reml)

  29. # Unstructured error covariance structure
  30. opp.unstr <- update(opp.reml, correlation = corSymm(form =  ~ wave | id))
  31. summary(opp.unstr)

  32. ### Additional analyses
  33. #
  34. ### ACF plot from random intercept/slope model
  35. plot(ACF(opp.reml), alpha = 0.01)

  36. ### add AR1
  37. opp.reml.ar1 <- update(opp.reml, corr = corAR1(form =  ~ wave | id))
  38. anova(opp.reml, opp.reml.ar1)

  39. ### not preferred

  40. anova(fit.cs, fit.ar1, opp.reml, opp.reml.ar1, opp.unstr)

  41. ### either the gls with AR1 or random intercepts/slopes
  42. #
  43. ### NOTE: possibly examine variogram and other correlation methods
复制代码

8
richardgu26 发表于 2016-5-29 07:01:56

9
dingyuezhang 发表于 2016-5-29 08:06:55
谢谢楼主分享!

10
frankly1020 在职认证  发表于 2016-5-29 10:42:47
好书,学习啦

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-28 16:59