SHARE
TWEET

Untitled

a guest Oct 12th, 2017 42 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require(gsheet)
  2. data <- read.csv(text =
  3.      gsheet2text('https://docs.google.com/spreadsheets/d/1QgtDcGJebyfW7TJsB8n6rAmsyAnlz1xkT3RuPFICTdk/edit?usp=sharing',
  4.         format ='csv'))
  5.    
  6. > head(data)
  7.   Subject Auditorium Education Time  Emotion Caffeine Recall
  8. 1     Jim          A        HS    0 Negative       95 125.80
  9. 2     Jim          A        HS    0  Neutral       86 123.60
  10. 3     Jim          A        HS    0 Positive      180 204.00
  11. 4     Jim          A        HS    1 Negative      200  95.72
  12. 5     Jim          A        HS    1  Neutral       40  75.80
  13. 6     Jim          A        HS    1 Positive       30  84.56
  14.    
  15. library(ggplot2)
  16. p <- ggplot(data, aes(x = Caffeine, y = Recall, colour = Subject)) +
  17.   geom_point(size=3) +
  18.   geom_line(aes(y = predict(fit1)),size=1)
  19. print(p)
  20.    
  21. p <- ggplot(data, aes(x = Caffeine, y = Recall, colour = Subject)) +
  22.   geom_point(size=3) +
  23.   geom_line(aes(y = predict(fit2)),size=1)
  24. print(p)
  25.    
  26. > data$predict = predict(fit2)
  27. > head(data)
  28.   Subject Auditorium Education Time  Emotion Caffeine Recall   predict
  29. 1     Jim          A        HS    0 Negative       95 125.80 132.45609
  30. 2     Jim          A        HS    0  Neutral       86 123.60 130.55145
  31. 3     Jim          A        HS    0 Positive      180 204.00 150.44439
  32. 4     Jim          A        HS    1 Negative      200  95.72 112.37045
  33. 5     Jim          A        HS    1  Neutral       40  75.80  78.51012
  34. 6     Jim          A        HS    1 Positive       30  84.56  76.39385
  35.    
  36. $`Time:Subject`
  37.          (Intercept)  Caffeine
  38. 0:Jason     75.03040 0.2116271
  39. 0:Jim       94.96442 0.2116271
  40. 0:Ron       58.72037 0.2116271
  41. 0:Tina      70.81225 0.2116271
  42. 0:Victor    86.31101 0.2116271
  43. 1:Jason     59.85016 0.2116271
  44. 1:Jim       52.65793 0.2116271
  45. 1:Ron       57.48987 0.2116271
  46. 1:Tina      68.43393 0.2116271
  47. 1:Victor    79.18386 0.2116271
  48. 2:Jason     43.71483 0.2116271
  49. 2:Jim       42.08250 0.2116271
  50. 2:Ron       58.44521 0.2116271
  51. 2:Tina      44.73748 0.2116271
  52. 2:Victor    36.33979 0.2116271
  53.  
  54. $Subject
  55.        (Intercept)  Caffeine
  56. Jason     30.40435 0.2116271
  57. Jim       79.30537 0.2116271
  58. Ron       13.06175 0.2116271
  59. Tina      54.12216 0.2116271
  60. Victor   132.69770 0.2116271
  61.    
  62. > coef(fit2)[[1]][2,1]
  63. [1] 94.96442
  64. > coef(fit2)[[2]][2,1]
  65. [1] 79.30537
  66. > coef(fit2)[[1]][2,2]
  67. [1] 0.2116271
  68. > data$Caffeine[1]
  69. [1] 95
  70. > coef(fit2)[[1]][2,1] + coef(fit2)[[2]][2,1] + coef(fit2)[[1]][2,2] * data$Caffeine[1]
  71. [1] 194.3744
  72.    
  73. > ranef(fit2)
  74. $`Time:Subject`
  75.          (Intercept)
  76. 0:Jason    13.112130
  77. 0:Jim      33.046151
  78. 0:Ron      -3.197895
  79. 0:Tina      8.893985
  80. 0:Victor   24.392738
  81. 1:Jason    -2.068105
  82. 1:Jim      -9.260334
  83. 1:Ron      -4.428399
  84. 1:Tina      6.515667
  85. 1:Victor   17.265589
  86. 2:Jason   -18.203436
  87. 2:Jim     -19.835771
  88. 2:Ron      -3.473053
  89. 2:Tina    -17.180791
  90. 2:Victor  -25.578477
  91.  
  92. $Subject
  93.        (Intercept)
  94. Jason   -31.513915
  95. Jim      17.387103
  96. Ron     -48.856516
  97. Tina     -7.796104
  98. Victor   70.779432
  99.    
  100. > summary(fit2)$coef[1]
  101. [1] 61.91827             # Overall intercept for Fixed Effects
  102. > ranef(fit2)[[1]][2,]  
  103. [1] 33.04615             # Time:Subject random intercept for Jim
  104. > ranef(fit2)[[2]][2,]
  105. [1] 17.3871              # Subject random intercept for Jim
  106. > summary(fit2)$coef[2]
  107. [1] 0.2116271            # Fixed effect slope
  108. > data$Caffeine[1]
  109. [1] 95                   # Value of caffeine
  110.  
  111. summary(fit2)$coef[1] + ranef(fit2)[[1]][2,] + ranef(fit2)[[2]][2,] +
  112.                     summary(fit2)$coef[2] * data$Caffeine[1]
  113. [1] 132.4561
  114.    
  115. > summary(fit2)
  116. Linear mixed model fit by REML ['lmerMod']
  117. Formula: Recall ~ (1 | Subject/Time) + Caffeine
  118.    Data: data
  119.    
  120. REML criterion at convergence: 444.5
  121.  
  122. Scaled residuals:
  123.  Min       1Q   Median       3Q      Max
  124. -1.88657 -0.46382 -0.06054  0.31430  2.16244
  125.  
  126. Random effects:
  127.  Groups       Name        Variance Std.Dev.
  128.  Time:Subject (Intercept)  558.4   23.63  
  129.  Subject      (Intercept) 2458.0   49.58  
  130.  Residual                  675.0   25.98  
  131. Number of obs: 45, groups:  Time:Subject, 15; Subject, 5
  132.  
  133. Fixed effects:
  134. Estimate Std. Error t value
  135. (Intercept) 61.91827   25.04930   2.472
  136. Caffeine     0.21163    0.07439   2.845
  137.  
  138. Correlation of Fixed Effects:
  139.  (Intr)
  140. Caffeine -0.365
  141.    
  142. > ranef(fit2)
  143. $`Time:Subject`
  144.          (Intercept)
  145. 0:Jason    13.112130
  146. 0:Jim      33.046151
  147. 0:Ron      -3.197895
  148. 0:Tina      8.893985
  149. 0:Victor   24.392738
  150. 1:Jason    -2.068105
  151. 1:Jim      -9.260334
  152. 1:Ron      -4.428399
  153. 1:Tina      6.515667
  154. 1:Victor   17.265589
  155. 2:Jason   -18.203436
  156. 2:Jim     -19.835771
  157. 2:Ron      -3.473053
  158. 2:Tina    -17.180791
  159. 2:Victor  -25.578477
  160.    
  161. $Subject
  162.        (Intercept)
  163. Jason   -31.513915
  164. Jim      17.387103
  165. Ron     -48.856516
  166. Tina     -7.796104
  167. Victor   70.779432
RAW Paste Data
Top