SPIDA 2010: Hierarchical Models: Additional material

From MathWiki

2a) fixed effects model (version 1)

Approach:

  1. Fit the least-squares intercept and slope in each school
  2. Get the average intercept and the average slope in each sector and test whether they are equal between sectors using an OLS linear hypothesis test
> 
>   lml <- lm( mathach ~ factor(school)/ses -1,
+               hs)
>   summary(lml)

Call:
lm(formula = mathach ~ factor(school)/ses - 1, data = hs)

Residuals:
     Min       1Q   Median       3Q      Max 
-18.6836  -4.4382   0.1976   4.7039  16.2807 

Coefficients:
                       Estimate Std. Error t value Pr(>|t|)    
factor(school)1317     12.73776    1.04063  12.240  < 2e-16 ***
factor(school)1906     14.88548    1.09651  13.575  < 2e-16 ***
factor(school)2208     14.28893    0.96850  14.754  < 2e-16 ***
factor(school)2458     13.31218    0.85674  15.538  < 2e-16 ***
factor(school)2626     13.66244    0.99747  13.697  < 2e-16 ***
factor(school)2629     14.93838    0.82437  18.121  < 2e-16 ***
factor(school)2639      6.00779    1.76026   3.413 0.000656 ***
factor(school)2658     12.24309    1.10735  11.056  < 2e-16 ***
factor(school)2771     13.29296    0.98978  13.430  < 2e-16 ***
factor(school)3013     12.78065    0.84248  15.170  < 2e-16 ***
factor(school)3610     14.99988    0.77727  19.298  < 2e-16 ***
factor(school)3992     14.44867    0.98231  14.709  < 2e-16 ***
factor(school)4292     12.78627    0.94795  13.488  < 2e-16 ***
factor(school)4511     13.41359    0.81562  16.446  < 2e-16 ***
factor(school)4530     10.03903    1.07131   9.371  < 2e-16 ***
factor(school)4868     11.84561    1.17867  10.050  < 2e-16 ***
factor(school)5619     13.20633    0.92154  14.331  < 2e-16 ***
factor(school)5640     13.83607    0.84581  16.358  < 2e-16 ***
factor(school)5650     14.25826    0.91076  15.655  < 2e-16 ***
factor(school)5720     14.20198    0.83988  16.910  < 2e-16 ***
factor(school)5761     12.14195    0.93145  13.036  < 2e-16 ***
factor(school)5762      3.11409    2.56262   1.215 0.224443    
factor(school)6074     14.20226    0.89333  15.898  < 2e-16 ***
factor(school)6484     13.02454    1.06922  12.181  < 2e-16 ***
factor(school)6897     13.84607    0.96560  14.339  < 2e-16 ***
factor(school)7172      8.35504    1.00340   8.327  < 2e-16 ***
factor(school)7232     12.99336    0.85744  15.154  < 2e-16 ***
factor(school)7342     11.61982    1.02679  11.317  < 2e-16 ***
factor(school)7345     11.19851    0.81675  13.711  < 2e-16 ***
factor(school)7688     18.40069    0.87577  21.011  < 2e-16 ***
factor(school)7697     14.91185    1.17419  12.700  < 2e-16 ***
factor(school)7890      7.99822    1.14471   6.987 3.87e-12 ***
factor(school)7919     13.02414    1.32715   9.814  < 2e-16 ***
factor(school)8531     12.17452    1.11469  10.922  < 2e-16 ***
factor(school)8627     10.68773    0.84818  12.601  < 2e-16 ***
factor(school)8707     12.35783    0.89805  13.761  < 2e-16 ***
factor(school)8854      5.70700    1.49406   3.820 0.000138 ***
factor(school)8874     13.45096    1.13088  11.894  < 2e-16 ***
factor(school)9550     10.88273    1.13671   9.574  < 2e-16 ***
factor(school)9586     13.82508    1.15452  11.975  < 2e-16 ***
factor(school)1317:ses  1.27391    1.60168   0.795 0.426505    
factor(school)1906:ses  2.14551    1.38022   1.554 0.120241    
factor(school)2208:ses  2.63664    1.32926   1.984 0.047451 *  
factor(school)2458:ses  2.95669    1.23946   2.385 0.017155 *  
factor(school)2626:ses  4.09968    1.79247   2.287 0.022296 *  
factor(school)2629:ses  0.22235    1.15539   0.192 0.847414    
factor(school)2639:ses -0.63010    1.54163  -0.409 0.682787    
factor(school)2658:ses  2.62990    1.43789   1.829 0.067556 .  
factor(school)2771:ses  4.26819    1.61779   2.638 0.008401 ** 
factor(school)3013:ses  3.83980    1.76458   2.176 0.029675 *  
factor(school)3610:ses  2.95585    1.21810   2.427 0.015333 *  
factor(school)3992:ses  0.53788    1.39884   0.385 0.700639    
factor(school)4292:ses -0.16061    1.17236  -0.137 0.891049    
factor(school)4511:ses  0.04251    1.39142   0.031 0.975630    
factor(school)4530:ses  1.64743    1.24891   1.319 0.187299    
factor(school)4868:ses  1.28647    1.49728   0.859 0.390337    
factor(school)5619:ses  5.25753    1.26822   4.146 3.54e-05 ***
factor(school)5640:ses  3.82774    1.39972   2.735 0.006303 ** 
factor(school)5650:ses  0.68062    1.18376   0.575 0.565383    
factor(school)5720:ses  2.46631    1.27510   1.934 0.053236 .  
factor(school)5761:ses  3.10801    1.20064   2.589 0.009710 ** 
factor(school)5762:ses -1.01410    1.97477  -0.514 0.607641    
factor(school)6074:ses  1.52909    1.31308   1.165 0.244364    
factor(school)6484:ses  0.60568    1.50515   0.402 0.687432    
factor(school)6897:ses  3.58049    1.18393   3.024 0.002526 ** 
factor(school)7172:ses  0.99448    1.37676   0.722 0.470180    
factor(school)7232:ses  5.00160    1.48889   3.359 0.000797 ***
factor(school)7342:ses  1.01246    1.43205   0.707 0.479653    
factor(school)7345:ses  4.21192    0.99725   4.224 2.52e-05 ***
factor(school)7688:ses  0.11634    1.48618   0.078 0.937610    
factor(school)7697:ses  3.13622    1.78822   1.754 0.079622 .  
factor(school)7890:ses -0.65597    1.45586  -0.451 0.652350    
factor(school)7919:ses  3.98937    1.89645   2.104 0.035545 *  
factor(school)8531:ses  3.31823    1.41381   2.347 0.019027 *  
factor(school)8627:ses  1.86956    1.19662   1.562 0.118368    
factor(school)8707:ses  3.39153    1.10759   3.062 0.002229 ** 
factor(school)8854:ses  1.93884    1.36483   1.421 0.155605    
factor(school)8874:ses  4.09630    1.44630   2.832 0.004671 ** 
factor(school)9550:ses  3.89194    1.47075   2.646 0.008207 ** 
factor(school)9586:ses  1.67208    1.34772   1.241 0.214879    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 6.107 on 1897 degrees of freedom
Multiple R-squared: 0.8297,     Adjusted R-squared: 0.8225 
F-statistic: 115.5 on 80 and 1897 DF,  p-value: < 2.2e-16 

> 
>   ddu <- up( hs, ~ factor(school))
>   ddu
     school Size   Sector PRACAD DISCLIM
1317   1317  455 Catholic   0.95  -1.694
1906   1906  400 Catholic   0.87  -0.939
2208   2208 1061 Catholic   0.68  -0.864
2458   2458  545 Catholic   0.89  -1.484
2626   2626 2142   Public   0.40   0.142
2629   2629 1314 Catholic   0.81  -0.613
2639   2639 2713   Public   0.14  -0.282
2658   2658  780 Catholic   0.79  -0.961
2771   2771  415   Public   0.24   1.048
3013   3013  760   Public   0.56  -0.213
3610   3610 1431 Catholic   0.80  -0.621
3992   3992 1114 Catholic   0.73  -1.534
4292   4292 1328 Catholic   0.76  -0.674
4511   4511 1068 Catholic   0.52  -1.872
4530   4530  435 Catholic   0.60  -0.245
4868   4868  657 Catholic   1.00  -0.219
5619   5619 1118 Catholic   0.77  -1.286
5640   5640 1152   Public   0.41   0.256
5650   5650  720 Catholic   0.60  -0.070
5720   5720  381 Catholic   0.65  -0.352
5761   5761  215 Catholic   0.63  -0.892
5762   5762 1826   Public   0.24   0.364
6074   6074 2051 Catholic   0.32  -1.018
6484   6484  726   Public   0.19   0.218
6897   6897 1415   Public   0.55  -0.361
7172   7172  280 Catholic   0.05   1.013
7232   7232 1154   Public   0.20   0.975
7342   7342 1220 Catholic   0.46   0.380
7345   7345  978   Public   0.64   0.336
7688   7688 1410 Catholic   0.65  -0.575
7697   7697 1734   Public   0.20   0.279
7890   7890  311   Public   0.21   0.845
7919   7919 1451   Public   0.50  -0.402
8531   8531 2190   Public   0.58   0.132
8627   8627 2452   Public   0.25   0.742
8707   8707 1133   Public   0.48   1.542
8854   8854  745   Public   0.18  -0.228
8874   8874 2650   Public   0.20   1.742
9550   9550 1532   Public   0.45   0.791
9586   9586  262 Catholic   1.00  -2.416
>   ind <- ddu$Sector == "Catholic"
>   ind
 [1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE  TRUE  TRUE
[13]  TRUE  TRUE  TRUE  TRUE  TRUE FALSE  TRUE  TRUE  TRUE FALSE  TRUE FALSE
[25] FALSE  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[37] FALSE FALSE FALSE  TRUE
>   L <- rbind( "Catholic" = ind, 
+               "Public" = 1-ind)
>   L
         [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
Catholic    1    1    1    1    0    1    0    1    0     0     1     1     1
Public      0    0    0    0    1    0    1    0    1     1     0     0     0
         [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24]
Catholic     1     1     1     1     0     1     1     1     0     1     0
Public       0     0     0     0     1     0     0     0     1     0     1
         [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35]
Catholic     0     1     0     1     0     1     0     0     0     0     0
Public       1     0     1     0     1     0     1     1     1     1     1
         [,36] [,37] [,38] [,39] [,40]
Catholic     0     0     0     0     1
Public       1     1     1     1     0
>   L <- L/apply(L,1, sum)   
>   L
               [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
Catholic 0.04761905 0.04761905 0.04761905 0.04761905 0.00000000 0.04761905
Public   0.00000000 0.00000000 0.00000000 0.00000000 0.05263158 0.00000000
               [,7]       [,8]       [,9]      [,10]      [,11]      [,12]
Catholic 0.00000000 0.04761905 0.00000000 0.00000000 0.04761905 0.04761905
Public   0.05263158 0.00000000 0.05263158 0.05263158 0.00000000 0.00000000
              [,13]      [,14]      [,15]      [,16]      [,17]      [,18]
Catholic 0.04761905 0.04761905 0.04761905 0.04761905 0.04761905 0.00000000
Public   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.05263158
              [,19]      [,20]      [,21]      [,22]      [,23]      [,24]
Catholic 0.04761905 0.04761905 0.04761905 0.00000000 0.04761905 0.00000000
Public   0.00000000 0.00000000 0.00000000 0.05263158 0.00000000 0.05263158
              [,25]      [,26]      [,27]      [,28]      [,29]      [,30]
Catholic 0.00000000 0.04761905 0.00000000 0.04761905 0.00000000 0.04761905
Public   0.05263158 0.00000000 0.05263158 0.00000000 0.05263158 0.00000000
              [,31]      [,32]      [,33]      [,34]      [,35]      [,36]
Catholic 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Public   0.05263158 0.05263158 0.05263158 0.05263158 0.05263158 0.05263158
              [,37]      [,38]      [,39]      [,40]
Catholic 0.00000000 0.00000000 0.00000000 0.04761905
Public   0.05263158 0.05263158 0.05263158 0.00000000
>   L <- cbind( rbind( L, 0,0), rbind( 0,0,L))
>   rownames( L ) <- c("Cath Int", "Pub Int", 
+                  "Cath Slope", "Pub Slope")
>   L
                 [,1]       [,2]       [,3]       [,4]       [,5]       [,6]
Cath Int   0.04761905 0.04761905 0.04761905 0.04761905 0.00000000 0.04761905
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.05263158 0.00000000
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Slope  0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
                 [,7]       [,8]       [,9]      [,10]      [,11]      [,12]
Cath Int   0.00000000 0.04761905 0.00000000 0.00000000 0.04761905 0.04761905
Pub Int    0.05263158 0.00000000 0.05263158 0.05263158 0.00000000 0.00000000
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Slope  0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
                [,13]      [,14]      [,15]      [,16]      [,17]      [,18]
Cath Int   0.04761905 0.04761905 0.04761905 0.04761905 0.04761905 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.05263158
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Slope  0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
                [,19]      [,20]      [,21]      [,22]      [,23]      [,24]
Cath Int   0.04761905 0.04761905 0.04761905 0.00000000 0.04761905 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.05263158 0.00000000 0.05263158
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Slope  0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
                [,25]      [,26]      [,27]      [,28]      [,29]      [,30]
Cath Int   0.00000000 0.04761905 0.00000000 0.04761905 0.00000000 0.04761905
Pub Int    0.05263158 0.00000000 0.05263158 0.00000000 0.05263158 0.00000000
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Slope  0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
                [,31]      [,32]      [,33]      [,34]      [,35]      [,36]
Cath Int   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Int    0.05263158 0.05263158 0.05263158 0.05263158 0.05263158 0.05263158
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Slope  0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
                [,37]      [,38]      [,39]      [,40]      [,41]      [,42]
Cath Int   0.00000000 0.00000000 0.00000000 0.04761905 0.00000000 0.00000000
Pub Int    0.05263158 0.05263158 0.05263158 0.00000000 0.00000000 0.00000000
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.04761905 0.04761905
Pub Slope  0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
                [,43]      [,44]      [,45]      [,46]      [,47]      [,48]
Cath Int   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Cath Slope 0.04761905 0.04761905 0.00000000 0.04761905 0.00000000 0.04761905
Pub Slope  0.00000000 0.00000000 0.05263158 0.00000000 0.05263158 0.00000000
                [,49]      [,50]      [,51]      [,52]      [,53]      [,54]
Cath Int   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Cath Slope 0.00000000 0.00000000 0.04761905 0.04761905 0.04761905 0.04761905
Pub Slope  0.05263158 0.05263158 0.00000000 0.00000000 0.00000000 0.00000000
                [,55]      [,56]      [,57]      [,58]      [,59]      [,60]
Cath Int   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Cath Slope 0.04761905 0.04761905 0.04761905 0.00000000 0.04761905 0.04761905
Pub Slope  0.00000000 0.00000000 0.00000000 0.05263158 0.00000000 0.00000000
                [,61]      [,62]      [,63]      [,64]      [,65]      [,66]
Cath Int   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Cath Slope 0.04761905 0.00000000 0.04761905 0.00000000 0.00000000 0.04761905
Pub Slope  0.00000000 0.05263158 0.00000000 0.05263158 0.05263158 0.00000000
                [,67]      [,68]      [,69]      [,70]      [,71]      [,72]
Cath Int   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Cath Slope 0.00000000 0.04761905 0.00000000 0.04761905 0.00000000 0.00000000
Pub Slope  0.05263158 0.00000000 0.05263158 0.00000000 0.05263158 0.05263158
                [,73]      [,74]      [,75]      [,76]      [,77]      [,78]
Cath Int   0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Int    0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Cath Slope 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
Pub Slope  0.05263158 0.05263158 0.05263158 0.05263158 0.05263158 0.05263158
                [,79]      [,80]
Cath Int   0.00000000 0.00000000
Pub Int    0.00000000 0.00000000
Cath Slope 0.00000000 0.04761905
Pub Slope  0.05263158 0.00000000
>   wald (lml, L)
  numDF denDF  F.value p.value
1     4  1897 1446.607 <.00001
            
              Estimate Std.Error   DF   t-value p-value Lower 0.95 Upper 0.95
  Cath Int   13.340489  0.211590 1897 63.048859 <.00001  12.925516  13.755462
  Pub Int    11.313234  0.281297 1897 40.218059 <.00001  10.761549  11.864918
  Cath Slope  1.667212  0.291611 1897  5.717250 <.00001   1.095300   2.239124
  Pub Slope   2.777206  0.348113 1897  7.977892 <.00001   2.094482   3.459931

>   diffmat <- rbind( "Int" = c( -1, 1, 0, 0),
+                 Slope = c( 0 , 0, -1, 1))       
>   diffmat   
      [,1] [,2] [,3] [,4]
Int     -1    1    0    0
Slope    0    0   -1    1
>   wald (lml, diffmat %*% L)
  numDF denDF  F.value p.value
1     2  1897 21.03533 <.00001
       
         Estimate Std.Error   DF   t-value p-value Lower 0.95 Upper 0.95
  Int   -2.027255  0.351992 1897 -5.759378 <.00001  -2.717588  -1.336923
  Slope  1.109995  0.454114 1897  2.444309 0.0146    0.219379   2.000610