Generalized Linear Multitask Learning Action Set
Synthetic Data
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 (SAS Viya: Machine Learning Procedures). 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 shows how to use the graphMultiReg action to estimate regression models from observations in a data table. These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The following DATA step creates the input data table, mycas.ToyData, in your CAS session. This data table contains seven variables: id is the row ID variable; X1, X2, and X3 are the input variables; and Y1, Y2, and Y3 are the target variables.
data mycas.toydata;
input id X1 X2 X3 Y1 Y2 Y3;
cards;
0 0.912621 1.288759 1.568040 2.813182 2.792080 2.792080
1 -0.696839 -1.487805 1.383056 0.187085 0.559484 0.559484
2 0.970062 2.405601 -1.638553 -1.903736 -2.301208 -2.301208
3 0.291515 -0.176170 -0.175393 3.842113 3.799427 3.799427
4 0.721477 0.472668 -0.522688 5.171737 4.906125 .
5 1.563781 -1.04760 -0.298725 21.160617 20.762561 .
6 0.426927 1.319816 -0.022467 -2.231491 -2.361400 .
7 -1.046538 -0.190017 -1.75929 -9.438538 -9.388092 .
8 0.155960 0.299715 -0.366969 0.109865 -0.00258535 .
9 1.568768 -0.900524 0.334290 20.422254 19.799980 19.799980
;
run;
The following DATA step creates the input graph table, mycas.ToyR, in your CAS session. This data table encodes the relationships between the targets. In this example, the targets Y2 and Y3 are connected, meaning that the regression weights for Y2 and Y3 are expected to be similar. This data table contains four variables: id is the row ID variable, and Y1, Y2, and Y3 are the target variables.
data mycas.toyr;
input id Y1 Y2 Y3;
cards;
0 0 1 -1
;
run;
The following statements run the graphMultiReg action and output the results to ODS tables:
proc cas;
loadactionset "gleam";
action gleam.graphMultiReg result=r / table={name='toydata'},
inputs={{name='X1'}, {name='X2'}, {name='X3'}},
targets={'Y1', 'Y2', 'Y3'},
graphTable={name='toyr'},
graphType='CUSTOM',
regL1=0.5,
regL2=1.0,
maxIters=200,
tolerance=1e-2,
seed=123,
modelOut={name='gmr_outW', replace=true},
graphOut={name='gmr_outR', replace=true},
saveState={name='gmr_ast', replace=true},
output={casOut={name='gmr_out', replace=true},
copyVars={'id', 'X1', 'X2', 'X3'}};
run;
The table parameter names the input data table. The inputs parameter specifies that the X1, X2, and X3 variables be used as inputs. The targets parameter specifies that the Y1, Y2, and Y3 variables be used as targets. The graphTable parameter specifies the user-defined graph table. The graphType parameter indicates the use of a custom graph table that is specified in the graphTable parameter. The regL1 parameter specifies the value of the penalization weight. The
regL2 parameter specifies the value of the penalization weight. The
maxiters parameter specifies the maximum number of iterations. The tolerance parameter specifies the optimization tolerance as a stopping criterion. The seed parameter specifies the seed to use for pseudorandom number generation. The modelOut parameter writes the estimated multitask regression weights to the data table mycas.gmr_outW. The graphOut parameter writes the graph table to the data table mycas.gmr_outR. The savestate parameter stores the estimated model in the data table mycas.mtl_ast for future scoring. The output parameter writes the scored results to the data table mycas.gmr_out, and the copyVars parameter copies the id, X1, X2, and X3 variables to the output.
Synthetic Data
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 toydata data to the comma-separated-value (CSV) file toydata.csv, convert the toyr data to the CSV file toyr.csv, and then use the following code to load the CSV files into CAS:
s:loadtable{casLib="casuser", path="toydata.csv"}
s:loadtable{casLib="casuser", path="toyr.csv"}
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
The following statements run the graphMultiReg action to estimate regression models from observations in a data table:
s:loadactionset{actionset="gleam"}
s:graphMultiReg{table={name="toydata"},
inputs={"X1", "X2", "X3"},
targets={"Y1", "Y2", "Y3"},
graphTable={name="toyr"},
graphType="CUSTOM",
regL1=0.5,
regL2=1.0,
maxIters=200,
tolerance=1e-2,
seed=123,
modelOut={name="gmr_outW", replace="TRUE"},
graphOut={name="gmr_outR", replace="TRUE"},
saveState={name="gmr_ast", replace="TRUE"},
output={casOut={name="gmr_out", replace="TRUE"},
copyVars={"id", "X1", "X2", "X3"}}};
The table parameter names the input data table. The inputs parameter specifies that the X1, X2, and X3 variables be used as inputs. The targets parameter specifies that the Y1, Y2, and Y3 variables be used as targets. The graphTable parameter specifies the user-defined graph table. The graphType parameter indicates the use of a custom graph table that is specified in the graphTable parameter. The regL1 parameter specifies the value of the penalization weight. The
regL2 parameter specifies the value of the penalization weight. The
maxiters parameter specifies the maximum number of iterations. The tolerance parameter specifies the optimization tolerance as a stopping criterion. The seed parameter specifies the seed to use for pseudorandom number generation. The modelOut parameter writes the estimated multitask regression weights to the data table gmr_outW. The graphOut parameter writes the graph table to the data table gmr_outR. The savestate parameter stores the estimated model in the data table mtl_ast for future scoring. The output parameter writes the scored results to the data table gmr_out, and the copyVars parameter copies the id, X1, X2, and X3 variables to the output.
Synthetic Data
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 toydata data to the comma-separated-value (CSV) file toydata.csv, convert the toyr data to the CSV file toyr.csv, and then use the following code to load the CSV files into CAS:
s.upload_file('toydata.csv')
s.upload_file('toyr.csv')
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
The following statements run the graphMultiReg action to estimate regression models from observations in a data table:
s.loadactionset(actionset="gleam")
s.graphMultiReg(table={"name":"toydata"},
inputs=["X1","X2","X3"],
targets=["Y1","Y2","Y3"],
graphTable="toyr",
graphType="CUSTOM",
regL1=0.5,
regL2=1.0,
maxIters=200,
tolerance = 1e-2,
seed=123,
outputTables={"names":{"ModelInfo":"ModelInfo",
"nobs":"nobs",
"DescStats":"DescStats",
"OptIterHistory":"OptIterHistory"}},
modelOut={"name":"gmr_outW", "replace":"TRUE"},
graphOut={"name":"gmr_graphOut", "replace":"TRUE"},
saveState={"name":"mtl_ast", "replace":True},
output={"casout":{ "name":"gmr_out", "replace":"TRUE" },
"copyvars":{"id","X1","X2","X3"}}
)
The table parameter names the input data table. The inputs parameter specifies that the X1, X2, and X3 variables be used as inputs. The targets parameter specifies that the Y1, Y2, and Y3 variables be used as targets. The graphTable parameter specifies the user-defined graph table. The graphType parameter indicates the use of a custom graph table that is specified in the graphTable parameter. The regL1 parameter specifies the value of the penalization weight. The
regL2 parameter specifies the value of the penalization weight. The
maxiters parameter specifies the maximum number of iterations. The tolerance parameter specifies the optimization tolerance as a stopping criterion. The seed parameter specifies the seed to use for pseudorandom number generation. The outputTables parameter saves the ODS tables to the specified data tables. The modelOut parameter writes the estimated multitask regression weights to the data table gmr_outW. The graphOut parameter writes the graph table to the data table gmr_outR. The savestate parameter stores the estimated model in the data table mtl_ast for future scoring. The output parameter writes the scored results to the data table gmr_out, and the copyVars parameter copies the id, X1, X2, and X3 variables to the output.
Synthetic Data
This section contains R 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 toydata data to the comma-separated-value (CSV) file toydata.csv, convert the toyr data to the CSV file toyr.csv, and then use the following code to load the CSV files into CAS:
m <- cas.read.csv(s, "toydata.csv", casOut=list(name="toydata"))
m <- cas.read.csv(s, "toyr.csv", casOut=list(name="toyr"))
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
The following statements run the graphMultiReg action to estimate regression models from observations in a data table:
loadActionSet(s,'gleam')
rs <- cas.gleam.graphMultiReg(s,
table=list(name="toydata"),
inputs=list("X1","X2","X3"),
targets=list("Y1","Y2","Y3"),
graphTable="toyr",
graphType="CUSTOM",
regL1=0.5,
regL2=1.0,
maxIters=200,
tolerance = 1e-2,
seed=123,
modelOut=list(name="gmr_outW", replace=TRUE),
graphOut=list(name="gmr_outR", replace=TRUE),
saveState=list(name="gmr_ast", replace=TRUE),
output=list(casout = list(name="gmr_out", replace=TRUE)))
The table parameter names the input data table. The inputs parameter specifies that the X1, X2, and X3 variables be used as inputs. The targets parameter specifies that the Y1, Y2, and Y3 variables be used as targets. The graphTable parameter specifies the user-defined graph table. The graphType parameter indicates the use of a custom graph table that is specified in the graphTable parameter. The regL1 parameter specifies the value of the penalization weight. The
regL2 parameter specifies the value of the penalization weight. The
maxiters parameter specifies the maximum number of iterations. The tolerance parameter specifies the optimization tolerance as a stopping criterion. The seed parameter specifies the seed to use for pseudorandom number generation. The modelOut parameter writes the estimated multitask regression weights to the data table gmr_outW. The graphOut parameter writes the graph table to the data table gmr_outR. The savestate parameter stores the estimated model in the data table mtl_ast for future scoring. The output parameter writes the scored results to the data table gmr_out.