| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inferencexml.glm; |
| 27 |
|
| 28 |
import cern.colt.matrix.impl.DenseDoubleMatrix2D; |
| 29 |
import cern.colt.matrix.linalg.SingularValueDecomposition; |
| 30 |
import dr.inference.distribution.DensityModel; |
| 31 |
import dr.inference.distribution.ParametricDistributionModel; |
| 32 |
import dr.inference.glm.*; |
| 33 |
import dr.inference.model.DesignMatrix; |
| 34 |
import dr.inference.model.Likelihood; |
| 35 |
import dr.inference.model.Parameter; |
| 36 |
import dr.util.Transform; |
| 37 |
import dr.xml.*; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class GeneralizedLinearModelParser extends AbstractXMLObjectParser { |
| 43 |
|
| 44 |
public static final String GLM_LIKELIHOOD = "glmModelNew"; |
| 45 |
|
| 46 |
public static final String DEPENDENT_VARIABLES = "dependentVariables"; |
| 47 |
public static final String INDEPENDENT_VARIABLES = "independentVariables"; |
| 48 |
|
| 49 |
public static final String MODEL = "model"; |
| 50 |
public static final String LINK_FUNCTION = "linkFunction"; |
| 51 |
|
| 52 |
public static final String INDICATOR = "indicator"; |
| 53 |
public static final String RANDOM_EFFECTS = "randomEffects"; |
| 54 |
public static final String CHECK_IDENTIFIABILITY = "checkIdentifiability"; |
| 55 |
public static final String CHECK_FULL_RANK = "checkFullRank"; |
| 56 |
|
| 57 |
public String getParserName() { |
| 58 |
return GLM_LIKELIHOOD; |
| 59 |
} |
| 60 |
|
| 61 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 62 |
|
| 63 |
|
| 64 |
XMLObject cxo = xo.getChild(MODEL); |
| 65 |
String linkFunctionName = cxo.getAttribute(LINK_FUNCTION, "identity"); |
| 66 |
DensityModel model = (DensityModel) cxo.getChild(DensityModel.class); |
| 67 |
GeneralizedLinearModel.LinkFunction linkFunction = GeneralizedLinearModel.LinkFunction.valueOf(linkFunctionName.toUpperCase()); |
| 68 |
|
| 69 |
cxo = xo.getChild(DEPENDENT_VARIABLES); |
| 70 |
Parameter dependentParam = (Parameter) cxo.getChild(Parameter.class); |
| 71 |
|
| 72 |
GeneralizedLinearModel glm = new GeneralizedLinearModel(dependentParam, model, linkFunction); |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
addIndependentParameters(xo, glm, dependentParam); |
| 108 |
|
| 109 |
addRandomEffects(xo, glm, dependentParam); |
| 110 |
|
| 111 |
|
| 112 |
boolean checkIdentifiability = xo.getAttribute(CHECK_IDENTIFIABILITY, true); |
| 113 |
if (checkIdentifiability) { |
| 114 |
if (!glm.getAllIndependentVariablesIdentifiable()) { |
| 115 |
throw new XMLParseException("All design matrix predictors are not identifiable in "+ xo.getId()); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
checkFullRankOfMatrix = xo.getAttribute(CHECK_FULL_RANK,true); |
| 120 |
|
| 121 |
return glm; |
| 122 |
} |
| 123 |
|
| 124 |
public void addRandomEffects(XMLObject xo, GeneralizedLinearModel glm, |
| 125 |
Parameter dependentParam) throws XMLParseException { |
| 126 |
int totalCount = xo.getChildCount(); |
| 127 |
|
| 128 |
for (int i = 0; i < totalCount; i++) { |
| 129 |
if (xo.getChildName(i).compareTo(RANDOM_EFFECTS) == 0) { |
| 130 |
XMLObject cxo = (XMLObject) xo.getChild(i); |
| 131 |
Parameter randomEffect = (Parameter) cxo.getChild(Parameter.class); |
| 132 |
checkRandomEffectsDimensions(randomEffect, dependentParam); |
| 133 |
glm.addRandomEffectsParameter(randomEffect); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
public void addIndependentParameters(XMLObject xo, GeneralizedLinearModel glm, |
| 139 |
Parameter dependentParam) throws XMLParseException { |
| 140 |
int totalCount = xo.getChildCount(); |
| 141 |
|
| 142 |
|
| 143 |
for (int i = 0; i < totalCount; i++) { |
| 144 |
if (xo.getChildName(i).compareTo(INDEPENDENT_VARIABLES) == 0) { |
| 145 |
XMLObject cxo = (XMLObject) xo.getChild(i); |
| 146 |
Parameter independentParam = (Parameter) cxo.getChild(Parameter.class); |
| 147 |
DesignMatrix designMatrix = (DesignMatrix) cxo.getChild(DesignMatrix.class); |
| 148 |
checkDimensions(independentParam, dependentParam, designMatrix); |
| 149 |
cxo = cxo.getChild(INDICATOR); |
| 150 |
Parameter indicator = null; |
| 151 |
if (cxo != null) { |
| 152 |
indicator = (Parameter) cxo.getChild(Parameter.class); |
| 153 |
if (indicator.getDimension() != independentParam.getDimension()) |
| 154 |
throw new XMLParseException("dim(" + independentParam.getId() + ") != dim(" + indicator.getId() + ")"); |
| 155 |
} |
| 156 |
|
| 157 |
if (checkFullRankOfMatrix) { |
| 158 |
checkFullRank(designMatrix); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
glm.addIndependentParameter(independentParam, designMatrix, indicator); |
| 166 |
|
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
private boolean checkFullRankOfMatrix; |
| 172 |
|
| 173 |
private void checkFullRank(DesignMatrix designMatrix) throws XMLParseException { |
| 174 |
int fullRank = designMatrix.getColumnDimension(); |
| 175 |
|
| 176 |
SingularValueDecomposition svd = new SingularValueDecomposition( |
| 177 |
new DenseDoubleMatrix2D(designMatrix.getParameterAsMatrix())); |
| 178 |
int realRank = svd.rank(); |
| 179 |
if (realRank != fullRank) { |
| 180 |
throw new XMLParseException( |
| 181 |
"rank(" + designMatrix.getId() + ") = " + realRank + |
| 182 |
".\nMatrix is not of full rank as colDim(" + designMatrix.getId() + ") = " + fullRank |
| 183 |
); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
private void checkRandomEffectsDimensions(Parameter randomEffect, Parameter dependentParam) |
| 188 |
throws XMLParseException { |
| 189 |
if (dependentParam != null) { |
| 190 |
if (randomEffect.getDimension() != dependentParam.getDimension()) { |
| 191 |
throw new XMLParseException( |
| 192 |
"dim(" + dependentParam.getId() + ") != dim(" + randomEffect.getId() + ")" |
| 193 |
); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
private void checkDimensions(Parameter independentParam, Parameter dependentParam, DesignMatrix designMatrix) |
| 199 |
throws XMLParseException { |
| 200 |
if (dependentParam != null) { |
| 201 |
if ((dependentParam.getDimension() != designMatrix.getRowDimension()) || |
| 202 |
(independentParam.getDimension() != designMatrix.getColumnDimension())) |
| 203 |
throw new XMLParseException( |
| 204 |
"dim(" + dependentParam.getId() + ") != dim(" + designMatrix.getId() + " %*% " + independentParam.getId() + ")" |
| 205 |
); |
| 206 |
} else { |
| 207 |
if (independentParam.getDimension() != designMatrix.getColumnDimension()) { |
| 208 |
throw new XMLParseException( |
| 209 |
"dim(" + independentParam.getId() + ") is incompatible with dim (" + designMatrix.getId() + ")" |
| 210 |
); |
| 211 |
} |
| 212 |
|
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 221 |
return rules; |
| 222 |
} |
| 223 |
|
| 224 |
private final XMLSyntaxRule[] rules = { |
| 225 |
AttributeRule.newBooleanRule(CHECK_IDENTIFIABILITY, true), |
| 226 |
AttributeRule.newBooleanRule(CHECK_FULL_RANK, true), |
| 227 |
new ElementRule(MODEL, |
| 228 |
new XMLSyntaxRule[]{ |
| 229 |
AttributeRule.newStringRule(LINK_FUNCTION), |
| 230 |
new ElementRule(ParametricDistributionModel.class), |
| 231 |
}), |
| 232 |
new ElementRule(DEPENDENT_VARIABLES, |
| 233 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, false), |
| 234 |
new ElementRule(INDEPENDENT_VARIABLES, |
| 235 |
new XMLSyntaxRule[]{ |
| 236 |
new ElementRule(Parameter.class, true), |
| 237 |
new ElementRule(DesignMatrix.class), |
| 238 |
new ElementRule(INDICATOR, |
| 239 |
new XMLSyntaxRule[]{ |
| 240 |
new ElementRule(Parameter.class) |
| 241 |
}, true), |
| 242 |
}, 1, 3), |
| 243 |
new ElementRule(RANDOM_EFFECTS, |
| 244 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}, 0, 3), |
| 245 |
|
| 246 |
|
| 247 |
}; |
| 248 |
|
| 249 |
public String getParserDescription() { |
| 250 |
return "Calculates the generalized linear model likelihood of the dependent parameters given one or more blocks of independent parameters and their design matrix."; |
| 251 |
} |
| 252 |
|
| 253 |
public Class getReturnType() { |
| 254 |
return GeneralizedLinearModel.class; |
| 255 |
} |
| 256 |
} |