Stata Textbook Examples
Applied Linear Statistical Models by Neter, Kutner, et. al.
Chapter 24: Random and Mixed Effects Models
Inputting the Apex Enterprises data
clear
input rating officer candidate
76 1 1
65 1 2
85 1 3
74 1 4
59 2 1
75 2 2
81 2 3
67 2 4
49 3 1
63 3 2
61 3 3
46 3 4
74 4 1
71 4 2
85 4 3
89 4 4
66 5 1
84 5 2
80 5 3
79 5 4
end
Table 24.1, p. 964.
table officer candidate, contents(mean rating) col
sum rating
---------------------------------------------
| candidate
officer | 1 2 3 4 Total
----------+----------------------------------
1 | 76 65 85 74 75
2 | 59 75 81 67 70.5
3 | 49 63 61 46 54.75
4 | 74 71 85 89 79.75
5 | 66 84 80 79 77.25
---------------------------------------------
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
rating | 20 71.45 11.87423 46 89
Fig. 24.2, p. 964.
twoway scatter officer rating

Table 24.2, p. 965.
anova rating officer
Number of obs = 20 R-squared = 0.5897
Root MSE = 8.56057 Adj R-squared = 0.4803
Source | Partial SS df MS F Prob > F
-----------+----------------------------------------------------
Model | 1579.7 4 394.925 5.39 0.0068
|
officer | 1579.7 4 394.925 5.39 0.0068
|
Residual | 1099.25 15 73.2833333
-----------+----------------------------------------------------
Total | 2678.95 19 140.997368
Estimating the overall mean and calculating the 90% confidence limit, p.
967.
scalar mstr = e(ss_1)/e(df_1)
scalar N = r(N)
sum rating
di "s^2 = " mstr/N
di "s = " sqrt(mstr/N)
di "t = " invttail(4,.05)
di "mean = " r(mean)
di "lower = " r(mean)-invttail(n,.05)*sqrt(mstr/N)
di "upper = " r(mean)+invttail(n,.05)*sqrt(mstr/N)
s^2 = 19.74625
s = 4.4436753
t = 2.1318468
mean = 71.45
lower = 61.976765
upper = 80.923235
Estimating the 90% confidence limit of simga_mu2/(sigma_mu2 + sigma2), p. 968-969.
quietly: anova rating officer
scalar mse = e(rmse)^2
scalar mstr = e(ss_1)/e(df_1)
scalar n = e(df_1)
scalar rbyn = e(df_r)
di "L = " (1/n)*((round(mstr,.1)/round(mse,.1))*(1/round(invF(n,rbyn,0.95),.01))-1)
di "U = "(1/n)*((round(mstr,.1)/round(mse,.1))*(1/round(invF(n,rbyn,0.05),.01))-1)
di "L* = " (1/n)*((round(mstr,.1)/round(mse,.1))*(1/round(invF(n,rbyn,0.95),.01))-1) ///
/(1+(1/n)*((round(mstr,.1)/round(mse,.1))*(1/round(invF(n,rbyn,0.95),.01))-1))
di "U* = "(1/n)*((round(mstr,.1)/round(mse,.1))*(1/round(invF(n,rbyn,0.05),.01))-1) ///
/(1+(1/n)*((round(mstr,.1)/round(mse,.1))*(1/round(invF(n,rbyn,0.05),.01))-1))
L = .19015105
U = 7.6727189
L* = .15977052
U* = .88469591
Estimating the standard error of sigma2, p. 970.
quietly: anova rating officer
scalar mse = e(rmse)^2
scalar rbyn = e(df_r)
di "sigma2 L = " rbyn*mse/invchi2(rbyn,.95)
di "sigma2 U = " rbyn*mse/invchi2(rbyn,.05)
di "sigma L = " sqrt(rbyn*mse/invchi2(rbyn,.95))
di "sigma U = " sqrt(rbyn*mse/invchi2(rbyn,.05))
sigma2 L = 43.977406
sigma2 U = 151.39216
sigma L = 6.6315462
sigma U = 12.304152
Point and interval estimation of sigma_mu2, p. 972-973.
quietly: anova rating officer
scalar n = e(df_1)
scalar rbyn = e(df_r)
scalar mse = e(rmse)^2
scalar mstr = e(ss_1)/n
scalar sigma_mu2 = (mstr-mse)/n
di "sigma_mu2 = " sigma_mu2
di "df = " (n*sigma_mu2)^2 / ((mstr^2/n)+mse^2/rbyn)
di "sigma_mu2 L = " 3*sigma_mu2/invchi2(3,.95)
di "sigma_mu2 U = " 3*sigma_mu2/invchi2(3,.05)
di "sigma_mu L = " sqrt(3*sigma_mu2/invchi2(3,.95))
di "sigma_mu U = " sqrt(3*sigma_mu2/invchi2(3,.05))
sigma_mu2 = 80.410417
df = 2.6290917
sigma_mu2 L = 30.868797
sigma_mu2 U = 685.61539
sigma_mu L = 5.5559695
sigma_mu U = 26.184258
Interval estimation of sigma_mu2 using the MLS Procedure, p. 974.
quietly: anova rating officer
scalar ms1 = round(e(ss_1)/e(df_1),.1)
scalar ms2 = round(e(rmse)^2,.1)
scalar df1 = e(df_1)
scalar df2 = e(df_r)
scalar c1 = 1/df1
scalar c2 = -1/df1
scalar sigma_mu2 = round((ms1-ms2)/df1,.1)
di "c1=" c1 ", c2=" c2 ", ms1=" ms1 ", ms2=" ms2 ", df1=" df1 ", df2=" df2 ", Lhat=" sigma_mu2 "."
scalar f1 = round(invF(df1,10000,.95),.01)
scalar f2 = round(invF(df2,10000,.95),.01)
scalar f3 = round(invF(10000,df1,.95),.01)
scalar f4 = round(invF(10000,df2,.95),.01)
scalar f5 = round(invF(df1,df2,.95),.01)
scalar f6 = round(invF(df2,df1,.95),.01)
di "f1=" f1 ", f2=" f2 ", f3=" f3 ", f4=" f4 ", f5=" f5 ", f6=" f6 "."
scalar g1 = round(1-(1/f1),.0001)
scalar g2 = round(1 - (1/f2),.0001)
scalar g3 = round(((f5-1)^2 - (g1*f5)^2 - (f4-1)^2)/f5,.0001)
scalar g4 = round(f6*( ((f6-1)/f6)^2 - ((f3-1)/f6)^2 - g2^2),.0001)
di "g1=" g1 ", g2=" g2 ", g3=" g3 ", g4=" g4 "."
scalar h_l = ((g1*c1*ms1)^2 + ((f4-1)*c2*ms2)^2 - g3*c1*c2*ms1*ms2)^(1/2)
scalar h_u = (((f3-1)*c1*ms1)^2 + (g2*c2*ms2)^2 - g4*c1*c2*ms1*ms2)^(1/2)
di "H_l=" h_l ", H_u=" h_u "."
di "sigma_mu2 L = " sigma_mu2-h_l
di "sigma_mu2 U = " sigma_mu2+h_u
di "sigma_mu L = " sqrt(sigma_mu2-h_l)
di "sigma_mu U = " sqrt(sigma_mu2+h_u)
c1=.25, c2=-.25, ms1=394.9, ms2=73.3, df1=4, df2=15, Lhat=80.4.
f1=2.37, f2=1.67, f3=5.63, f4=2.07, f5=3.06, f6=5.86.
g1=.5781, g2=.4012, g3=-.01, g4=-.5708.
H_l=60.197101, H_u=456.02504.
sigma_mu2 L = 20.202899
sigma_mu2 U = 536.42504
sigma_mu L = 4.4947635
sigma_mu U = 23.160851
The training example p. 982-983, 987-988 and 989 was omitted because the data is not available.
Inputting Sheffield Foods Co. data, table 24.11, p. 995.
table rep lab, contents(mean fat) by(method)
----------------------------------
method | lab
and rep | 1 2 3 4
----------+-----------------------
1 |
1 | 5.19 4.09 4.62 3.71
2 | 5.09 3.99 4.32 3.86
3 | 3.75 4.35 3.79
4 | 4.04 4.59 3.63
5 | 4.06
6 |
----------+-----------------------
2 |
1 | 3.26 3.02 3.08 2.98
2 | 3.48 3.32 2.95 2.89
3 | 3.24 2.83 2.98 2.75
4 | 3.41 2.96 2.74 3.04
5 | 3.35 3.23 3.07 2.88
6 | 3.04 3.07 2.7 3.2
----------------------------------
Figure 24.3, p. 995
dotplot fat if method==1, over(lab) name(method1)
dotplot fat if method==2, over(lab) name(method2)

Figure 24.4, p. 996.
Note this code uses the user-written program anovaplot, to find and download
this program type "findit anovaplot" (without the quotation marks) in the Stata command window.
Table 24.13-24.14 and fig. 24.5 were not generated, p. 1000-1001.
UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services
The content of this web site should not be
construed as an endorsement of any particular web site, book, or software
product by the University of California
|