Mixed Modeling Action Set
The mixed Action
Student Scores on Math Tests
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts. A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
This example demonstrates how you can perform a mixed model analysis of covariance when you have many groups. Suppose you are an educational researcher who studies how student scores on math tests change over time. Students are tested four times, and you want to estimate the overall rise or fall, accounting for correlation between test response behaviors of students in the same neighborhood and school. One way to model this correlation is by using a random-effects analysis of covariance, where in addition to having a student-specific quadratic mean response function, the scores for students from the same neighborhood and school are all assumed to share the same quadratic mean test response function whose parameters are random. The following statements simulate data that have this structure; these statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
data mycas.SchoolSample;
do SchoolID = 1 to 1000;
do nID = 1 to 50;
Neighborhood = (SchoolID-1)*15 + nId;
bInt = 15*ranuni(1);
bTime = 15*ranuni(1);
bTime2 = ranuni(1);
do sID = 1 to 2;
do Time = 1 to 4;
Math = bInt + bTime*Time + bTime2*Time*Time + rannor(2);
output;
end;
end;
end;
end;
run;
These data comes from 1,000 schools and about 15,035 neighborhoods; neighborhoods are associated with more than one school and vice versa. There are 400,000 observations and 50,000 levels of the subject effect.
The following statements use the mixed action to fit a linear mixed model to these data and produce Output 14.1.1 through Output 14.1.4:
proc cas;
action mixed.mixed /
table={name='SchoolSample'},
class={{vars={'Neighborhood', 'SchoolID'}}},
model={depVars={{name='Math'}},
effects={{vars={'Time'}},
{vars={'Time','Time'},interaction='CROSS'}},
printsol = TRUE},
random={{noint=FALSE,
effects={{vars={'Time'}},
{vars={'Time','Time'},interaction='CROSS'}},
type='RANDOM',
subject= {{vars={'Neighborhood','SchoolID'},interaction='CROSS'}},
covType='UN',
printsol=TRUE}};
run;
This model fits a quadratic mean response model with an unstructured covariance matrix to model the covariance between the random parameters of the response model. With 15,035 combinations of neighborhoods and schools, this model can be computationally daunting to fit, but the mixed action finishes quickly, by distributing the data and the likelihood computation for different subjects in a CAS computing environment.
Output 14.1.1 displays the "Model Information," "Optimization Information," "Class Level Information," "Number of Observations Information," and "Dimensions" tables.
Output 14.1.1: Mixed Model Analysis of Covariance
| Model Information | |
|---|---|
| Data Source | SCHOOLSAMPLE |
| Response Variable | Math |
| Estimation Method | Restricted Maximum Likelihood (REML) |
| Degrees of Freedom Method | Residual |
| Design Matrix Method | Dense |
| Optimization Information | |
|---|---|
| Optimization Technique | Newton-Raphson with Ridging |
| Hessian in Optimization | Exact |
| Parameters in Optimization | 6 |
| Lower Boundaries | 3 |
| Upper Boundaries | 0 |
| Residual Variance | Profiled |
| Starting Values From | Data |
| Class Level Information | ||
|---|---|---|
| Class | Levels | Values |
| Neighborhood | 15035 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... |
| SchoolID | 1000 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ... |
| Number of Observations Read | 400000 |
|---|---|
| Number of Observations Used | 400000 |
| Dimensions | |
|---|---|
| G-side Covariance Parameters | 6 |
| R-side Covariance Parameters | 1 |
| Columns in X | 3 |
| Columns in Z per Subject | 3 |
| Subjects (Blocks in V) | 50000 |
Output 14.1.2 displays the convergence status and the "Covariance Parameter Estimation" table.
Output 14.1.2: Mixed Model Analysis of Covariance
| Convergence criterion (ABSGCONV=0.00001) satisfied. |
| Covariance Parameter Estimates | ||
|---|---|---|
| Cov Parm | Subject | Estimate |
| UN(1,1) | Neighborhood*SchoolID | 18.7964 |
| UN(2,1) | Neighborhood*SchoolID | -0.1215 |
| UN(2,2) | Neighborhood*SchoolID | 18.9874 |
| UN(3,1) | Neighborhood*SchoolID | -0.00044 |
| UN(3,2) | Neighborhood*SchoolID | -0.00805 |
| UN(3,3) | Neighborhood*SchoolID | 0.08394 |
| Residual | 0.9998 | |
Output 14.1.3 displays the "Fit Statistics" table.
Output 14.1.3: Mixed Model Analysis of Covariance
| Fit Statistics | |
|---|---|
| -2 Res Log Likelihood | 1681870 |
| AIC (smaller is better) | 1681884 |
| AICC (smaller is better) | 1681884 |
| BIC (smaller is better) | 1681945 |
| CAIC (smaller is better) | 1681952 |
| HQIC (smaller is better) | 1681903 |
Output 14.1.4 displays the "Solution for Fixed Effects" table.
Output 14.1.4: Mixed Model Analysis of Covariance
| Solution for Fixed Effects | |||||
|---|---|---|---|---|---|
| Effect | Estimate | Standard Error | DF | t Value | Pr > |t| |
| Intercept | 7.4961 | 0.02129 | 4E5 | 352.04 | <.0001 |
| Time | 7.4939 | 0.02108 | 4E5 | 355.55 | <.0001 |
| Time*Time | 0.5009 | 0.002044 | 4E5 | 245.03 | <.0001 |
These estimates are very close to the quadratic coefficients that are used in the SAS DATA step, multiplied by 1/2, which is the expected value of a uniform random variable on [0,1].
Student Scores on Math Tests
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the schoolSample data to the comma-separated-value (CSV) file schoolSample.csv and then use the following code to load the CSV file into CAS:
s:loadtable{casLib="casuser", path="schoolSample.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
The following code loads the mixed modeling action set and then uses the mixed action to perform a linear mixed model analysis of the data:
s:loadactionset{actionset='mixed'}
res=s:mixed{
table='schoolSample',
class={ {vars={'Neighborhood','SchoolID'}}},
model={ depVars={{name='Math'}},
effects={ {vars={'Time'}},
{vars={'Time','Time'},interaction='CROSS'}},
printsol = 'TRUE'},
random={ {effects={{vars={'Time'}},
{vars={'Time','Time'},interaction='CROSS'}},
noint='FALSE',
subject={{vars={'Neighborhood','SchoolID'},interaction='CROSS'}},
type='RANDOM',
covType='UN',
printsol='TRUE'}},
}
The following commands display the tables that the mixed action produces:
print(res.ModelInfo)
print(res.OptInfo)
print(res.ClassInfo)
print(res.NObs)
print(res.Dimensions)
print(res.ConvergenceStatus)
print(res.CovParms)
print(res.FitStatistics)
print(res.ParameterEstimates)
The following command displays all the tables that the mixed action produces along with their full paths:
print(res)
For more information about the results of this analysis, see the CASL version of this example.
Student Scores on Math Tests
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
Note: In order to run this code, the data that are described in the CASL version need to be accessible to the CAS server. One way to do this is to convert the schoolSample data to the comma-separated-value (CSV) file schoolSample.csv and then use the following code to load the CSV file into CAS:
s.upload_file('schoolSample.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
The following code loads the mixed modeling action set and then uses the mixed action to perform a linear mixed model analysis of the data:
s.loadactionset(actionset='mixed')
res=s.mixed(
table='schoolSample',
classVars=['Neighborhood','SchoolID'],
model={'depVars':'Math',
'effects':['Time',
{'vars':['Time','Time'],'interact':'CROSS'}
],
'printsol':'TRUE'},
random=[{'effects':['Time',
{'vars':['Time','Time'],'interact':'CROSS'}
],
'noint':'False',
'subject':[{'vars':{'Neighborhood','SchoolID'},'interact':'CROSS'}],
'type':'RANDOM',
'covType':'UN',
'printsol':'True'}
]
)
The following commands display the tables that the mixed action produces:
print(res.ModelInfo)
print(res.OptInfo)
print(res.ClassInfo)
print(res.NObs)
print(res.Dimensions)
print(res.ConvergenceStatus)
print(res.CovParms)
print(res.FitStatistics)
print(res.ParameterEstimates)
The following command displays all the tables that the mixed action produces along with their full paths:
print(res)
For more information about the results of this analysis, see the CASL version of this example.
Student Scores on Math Tests
This example is not available for the R programming language.