(*:Mathematica:: V2.2.1 *) (*: CLM Version:: 1.1 *) (*:Context:: "cdxsymbo`" *) (*:Title:: Symoblic parameters for CLM *) (*:Summary:: This package creates symbolic parameters used in composite linear models as either names in parameter tables or for expected cell counts. SymbolicExpectedCount: symbolic expected counts SymbolicParameters: parameter for CLM /loglinear CategoricalParameters: parameters for loglinear model *) (*:Keywords:: design matrix, loglinear model*) (*:Requirements:: matrixlx: matrix functions cdxdesig: Hierarchical Interactions *) (*:Author:: 1992 Stuart G. Baker *) BeginPackage["clm`cdxsymbo`","clm`cdxdesig`","clm`matrixlx`"] CategoricalParameters::usage= "CategoricalParameters[dim,model,options_] creates a list of parameters for the specified dimensions,for example, CategoricalParameters[{2,2,2},{{1,2},{2,3}}], where index 1 varies fastest, then 2,etc. The Baseline option specifies which level is baseline in the parametrization,i.e., Baseline->{2,1, 2} says that the baseline levels are 2 for variable 1, 1 for variable 2, and 2 for variable 3. The default is \"First\",which specifies level 1 as the baseline for each variable. The ModelType option specifies the type of model to be fit, either \"Hierarchical\"(the default) or \"NonHierarchical\". The Letter option specifies the letter (a string) which becomes the name of the parameter; the default is\"q\"." Options[CategoricalParameters]={Baseline->"First",Letter->"q", ModelType->"Hierarchical"} cdxsymbo::limit="There is a limit of 26 levels." cdxsymbo::badpar="The configuration is not consistent with the dimensions." cdxsymbo::badbase="The baseline is not consistent with the dimensions." Clear[CategoricalParameters] Begin["Private`"] CategoricalParameters[dim_,config_:Automatic,opts___Rule]:= Module[{baseline,letter,modeltype,mod,base}, {baseline,letter,modeltype}= {Baseline,Letter,ModelType} /. {opts} /. Options[CategoricalParameters]; If[config===Automatic,mod={Range[Length[dim]]},mod=config]; If[baseline==="First",base=Flatten[J[1,Length[dim]]], base=baseline]; ToExpression @ pCategoricalParameters[dim,letter,mod,modeltype,base]] pCategoricalParameters[dim_,letter_,config_,modeltype_,base_]:= Module[{mod,temp}, If[modeltype==="Hierarchical", mod=HierarchicalInteractions[config], mod=config]; temp=pInteractions2Parameters[letter,dim,mod,base]; (*add main effect parameter to list *) parameters=Prepend[temp,StringJoin[letter,"0"]]; Return[parameters]]/; Which[ Max[dim]>26, Message[cdxsymbo::limit];False, !(pDimModelQ[dim,config]), Message[cdxsymbo::badpar];False, !(pDimBaselineQ[dim,base]), Message[cdxsymbo::badbase];False, True,True] pDimModelQ[dim_,config_]:= And @@ (MemberQ[Range[Length[dim]],#]& /@ Flatten[config]) pDimBaselineQ[dim_,baseline_]:= Module[{i}, And @@ Table[MemberQ[Range[dim[[i]]],baseline[[i]]], {i,1,Length[dim]}] ] (*===============Internal Functions=====*) (*convert number of level to corresponding letter for exampe pInteger2Letter[2] -> "b" *) pInteger2Letter[integer_]:= {"a","b","c","d","e","f","h","i","j","k", "l","m","n","o","p","q","r","s","t","u","v", "w","x","y","z"}[[integer]] (* create numeral from variable interaction,with hyphen if variable is greater than 10, for example pInteraction2Numeral[{2,3}] ="23" and pInteraction2Numeral[{2,10}] = "2-10" *) pInteraction2Numeral[variables_]:= StringJoin[ToString /@ variables] /; Max[variables] < 10 pInteraction2Numeral[variables_]:= Module[{s,first,rest}, s=ToString /@ variables; first=First[s]; rest=StringJoin["-",#]& /@ Rest[s]; numeral=StringJoin[first,rest]; Return[numeral]]/; Max[variables] >= 10 (* create list of levels from dimensions and variable, pVariable2Levels[dimensions,variable] for example pVariable2Levels[{2,3,4},3] -> {"b","c","d"} i,e., it creates 3 letters for variable 3 (with 4 levels) by convention, the first level is aliased *) pVariable2Levels[dim_,x_,base_]:= pInteger2Letter/@ Drop[Range[dim[[x]]],{base[[x]]}]; (* create a list of level interactions from dimensions & interactions for example pInteraction2Levels[{2,3,4}, {2,3},{1,1,1}] returns {bb, bc, bd, cb, cc, cd}] ,i.e, the levels corresponding to the interaction of variable 2 (with 3 levels) with variable 3 (with 4 levels *) pInteraction2Levels[dim_,x_,base_]:= Module[{letters,set}, (*create letters for all levels *) letters=pVariable2Levels[dim,#,base]& /@ x; (*create all interactions *) set=Distribute[letters,List]; (*join letters to form strings *) levels=Apply[StringJoin,#]& /@ set; Return[levels]] (* create parameters fromm dimensions and interaction pInteraction2Parameters[letter,dimensions,interaction] for example pInteraction2Parameters["q",{2,3,4},{3,4}] returns {q23bb, q23bc, q23bd, q23cb, q23cc, q23cd}.*) pInteraction2Parameters[letter_,dim_,variables_,base_] := Module[{levels,numerals}, levels=pInteraction2Levels[dim,variables,base]; numerals=pInteraction2Numeral[variables]; parameters=StringJoin[letter,numerals,#]& /@ levels; Return[parameters]] (* create parameters from dimensions and list of interactions pInteractions2Parameters[letter,dimensions,interactions (for example pInteractions2Parameters["q",{2,3,4},{{1},{2},{2,3}}] returns {q1b, q2b, q2c, q23bb, q23bc, q23,bd, q23cb, q23cc, q23cd}.*) pInteractions2Parameters[letter_,dim_,x_,base_]:= Flatten[pInteraction2Parameters[letter,dim,#,base]& /@ x] End[] (*Protect[SymbolicExpectedCount,SymbolicParameters,CategoricalParameters]*) EndPackage[]