(*:Mathematica:: V2.2.1 *) (*:Context: "clminput`" *) (*:CLM Version: 1.1 *) (*:Title: input check for CLM *) (*:Summary: InputQ: check consistency of components *) (*:Keywords: composite linear model, incomplete categorical data *) (*:References: Baker, S.G. 1992. A simple method for computing the observed information matrix when using the EM algorithm with categorical data, Journal of Computational and Graphical Statistics, 1. 63-76. Baker,S.G. 1994. Composite linear models for incomplete categorical data, Statistics in Medicine, in press. *) (*Requirements: matrixlx`: matrix functions *) (*Author: 1993 Stuart G. Baker *) BeginPackage["clm`clminput`", "clm`matrixlx`"] (*--------------------examples---------------------*) (* incomplete data: structural*) exgen::usage="genetic linkage model" exhaz::usage="survival:incomplete covariates" exlat::uasge="latent variable diagnostic tests" exoa2::usage="observer-agreement with 2 categories and replications" extryp::usage=" overdispersion example using trypanasome data" exmisclas::usage="misclassification: Palmgren model" (*incomplete data: planned *) exdbl::usage="double sampling" excens::usage="grouped survival:informative censoring and double sampling" exnrgs::usage="new test, reference test, gold standard" expepe::usage="internal validation sample by M. S. Pepe" (*incomplete data unplanned *) exclosed::usage="loglinear model closed-form solutions" exlead::usage="Poisson regression for blood lead levels" exll::usage="loglinear model: nonresponse in a survey" exllsys::usage="loglinear + logit model: nonresponse" exlrsys::usage="2 logit models: nonresponse in a survey" exmh::usage="marginal homogeneity with ordered categories" extruman::usage="nonresponse in 1948 poll Truman vs Dewey" excross::usage="crossover data from Ezzet and Whitehead" extestev::usage="prostate cancer screenning;Cooner'data" exfam::usage="familial cluster" exobese::usage="repeated obesity data from Muscatine Risk Factor Study" (*complete data*) exordm::usage="ordered categorical: McCullough model" exordc::usage="ordered catetorical: continuation ratio" exfitz::usage="binary cross over data from Fitzmaurice and Laird" exblr::usage="binary logistic regression: McCullough" (*------------------------InputQ-----------------------------*) InputQ::usage= "InputQ[y,{exfunc,modelname}] checks dimensions of matrices and names of mstep functions in the exfunc." (*--------------------------Messages---------------------------*) clminput::badmod="The modelname is unknown." clminput::badspec="There is an error in the output of the example function." clminput::cy= "Matrices c and y do not conform. Dimensions of c are `1`; Dimensions of y are `2`." clminput::cn= "Matrices c and n do not conform. Dimensions of c are `1`; Length of n is `2`." clminput::func="The mstep function is not known." clminput::zx= "Matrices z and x do not conform in sub-model `1`. Dimensions of z are `2`; Dimensions of x are `3`." clminput::gx= "Matrices g and x do not conform in sub-model `1`. Dimensions of g are `2`; Dimensions of x are `3`." clminput::wg= "Matrices w and g do not conform in sub-model `1`. Dimensions of w are `2`; Dimensions of g are `3`." clminput::cw= "Matrices c and w do not conform in sub-model `1`. Dimensions of c are `2`; Dimensions of w are `3`." Clear[InputQ] Begin["Private`"] InputQ[yi_,{exfunc_,modelname_}]:= Module[{y,test}, (* create model inputs*) y=N[yi]; test=exfunc[y,modelname]; res=Which[ Head[test] =!= List,False;Message[clminput::badmod], Length[test] =!= 4, False;Message[clminput::badspec], True,pInputQ[y,test]]; Return[res]] pInputQ[y_,test_]:= Module[{c,model,ratio,name, cdim,ydim,resc, components,n,resn, mstepfunc,possiblefunctions,funcstring,resf, w,g,z,x,partm,mat,resp}, {c,model,ratio,name}=test; cdim=Dimensions[c]; ydim=Dimensions[y]; components=model[[1]]; n=components[[1,1]]; (*check that c and y conform*) If[cdim[[1]]=!=ydim[[1]], resc=False; Message[clminput::cy,cdim,ydim], resc=True]; (*check that c and n conform*) If[cdim[[2]]=!=Length[n] && n=!=1, resn=False; Message[clminput::cn,cdim,Length[n]], resn=True]; (*check that M-step function is valid*) mstepfunc=model[[2]]; possiblefunctions={"NPS","NPSLogit","NLR","NPR","NLLR", "NIPF","NIPFCount","NGLR","NWLS"}; funcstring=ToString[#]& /@ mstepfunc; If[And @@ (MemberQ[possiblefunctions,#]& /@ funcstring), resf=True, resf=False; Message[clminput::func]]; (*check that component matrices conform*) w=components[[3]]; g=components[[4]]; z=components[[6]]; x=components[[7]]; partm=Range[Length[x]]; mat={partm,w,g,z,x}; resp=And @@ MapThread[pInputQStep[c,#1,#2,#3,#4,#5]&,mat]; res=resf && resp && resn && resc; Return[res]] (* check dimensions for each sub-model*) pInputQStep[c_,model_,w_,g_,z_,x_]:= Module[{wdim,gdim,zdim,xdim}, cdim=Dimensions[c]; wdim=Dimensions[w]; gdim=Dimensions[g]; zdim=Dimensions[z]; xdim=Dimensions[x]; res=True; (*multiple if's for multiple messages*) (*no error if special z with more than 2 dimensions*) If[zdim[[1]]=!=xdim[[1]], res=False; Message[clminput::zx,model,zdim,xdim]]; If[gdim[[2]]=!=xdim[[1]], res=False; Message[clminput::gx,model,gdim,xdim]]; If[wdim[[1]]=!=gdim[[1]], res=False; Message[clminput::wg,model,wdim,gdim]]; If[cdim[[2]]=!=wdim[[1]], res=False; Message[clminput::cw,model,cdim,wdim]]; Return[res]]; End[] (*Protect[InputQ]*) EndPackage[]