| 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.model; |
| 27 |
|
| 28 |
import dr.inference.model.*; |
| 29 |
import dr.xml.*; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class LatentFactorModelParser extends AbstractXMLObjectParser { |
| 37 |
public final static String LATENT_FACTOR_MODEL = "latentFactorModel"; |
| 38 |
public final static String NUMBER_OF_FACTORS = "factorNumber"; |
| 39 |
public final static String FACTORS = "factors"; |
| 40 |
public final static String DATA = "data"; |
| 41 |
public final static String LOADINGS = "loadings"; |
| 42 |
public static final String ROW_PRECISION = "rowPrecision"; |
| 43 |
public static final String COLUMN_PRECISION = "columnPrecision"; |
| 44 |
public static final String SCALE_DATA="scaleData"; |
| 45 |
public static final String CONTINUOUS="continuous"; |
| 46 |
public static final String COMPUTE_RESIDUALS_FOR_DISCRETE="computeResidualsForDiscrete"; |
| 47 |
public static final String RECOMPUTE_RESIDUALS="recomputeResiduals"; |
| 48 |
public static final String RECOMPUTE_FACTORS="recomputeFactors"; |
| 49 |
public static final String RECOMPUTE_LOADINGS="recomputeLoadings"; |
| 50 |
public static final String MISSING_INDICATOR = "missingIndicator"; |
| 51 |
|
| 52 |
|
| 53 |
public String getParserName() { |
| 54 |
return LATENT_FACTOR_MODEL; |
| 55 |
} |
| 56 |
|
| 57 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 58 |
|
| 59 |
MatrixParameterInterface factors; |
| 60 |
if (xo.getChild(FACTORS).getChild(FastMatrixParameter.class) == null) |
| 61 |
{ |
| 62 |
CompoundParameter factorsTemp = (CompoundParameter) xo.getChild(FACTORS).getChild(CompoundParameter.class); |
| 63 |
factors = MatrixParameter.recast(factorsTemp.getParameterName(), factorsTemp); |
| 64 |
} |
| 65 |
else { |
| 66 |
factors = (MatrixParameterInterface) xo.getChild(FACTORS).getChild(MatrixParameterInterface.class); |
| 67 |
} |
| 68 |
MatrixParameterInterface dataParameter = (MatrixParameterInterface) xo.getChild(DATA).getChild(MatrixParameterInterface.class); |
| 69 |
MatrixParameterInterface loadings = (MatrixParameterInterface) xo.getChild(LOADINGS).getChild(MatrixParameterInterface.class); |
| 70 |
Parameter missingIndicator = null; |
| 71 |
if(xo.hasChildNamed(MISSING_INDICATOR)) |
| 72 |
missingIndicator = (Parameter) xo.getChild(MISSING_INDICATOR).getChild(Parameter.class); |
| 73 |
DiagonalMatrix rowPrecision = (DiagonalMatrix) xo.getChild(ROW_PRECISION).getChild(MatrixParameter.class); |
| 74 |
DiagonalMatrix colPrecision = (DiagonalMatrix) xo.getChild(COLUMN_PRECISION).getChild(MatrixParameter.class); |
| 75 |
boolean newModel= xo.getAttribute(COMPUTE_RESIDUALS_FOR_DISCRETE, true); |
| 76 |
boolean computeResiduals= xo.getAttribute(RECOMPUTE_RESIDUALS, true); |
| 77 |
boolean computeFactors=xo.getAttribute(RECOMPUTE_FACTORS, true); |
| 78 |
boolean computeLoadings=xo.getAttribute(RECOMPUTE_LOADINGS, true); |
| 79 |
Parameter continuous=null; |
| 80 |
if(xo.getChild(CONTINUOUS)!=null) |
| 81 |
continuous=(Parameter) xo.getChild(CONTINUOUS).getChild(Parameter.class); |
| 82 |
else |
| 83 |
continuous=new Parameter.Default(colPrecision.getRowDimension(), 1.0); |
| 84 |
boolean scaleData=xo.getAttribute(SCALE_DATA, true); |
| 85 |
|
| 86 |
Parameter temp=null; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
return new LatentFactorModel(dataParameter, factors, loadings, rowPrecision, colPrecision, missingIndicator, scaleData, continuous, newModel, computeResiduals, computeFactors, computeLoadings); |
| 97 |
} |
| 98 |
|
| 99 |
private static final XMLSyntaxRule[] rules = { |
| 100 |
AttributeRule.newIntegerRule(NUMBER_OF_FACTORS), |
| 101 |
AttributeRule.newBooleanRule(SCALE_DATA, true), |
| 102 |
AttributeRule.newBooleanRule(COMPUTE_RESIDUALS_FOR_DISCRETE, true), |
| 103 |
AttributeRule.newBooleanRule(RECOMPUTE_FACTORS, true), |
| 104 |
AttributeRule.newBooleanRule(RECOMPUTE_RESIDUALS, true), |
| 105 |
AttributeRule.newBooleanRule(RECOMPUTE_LOADINGS,true), |
| 106 |
new ElementRule(DATA, new XMLSyntaxRule[]{ |
| 107 |
new ElementRule(MatrixParameterInterface.class), |
| 108 |
}), |
| 109 |
new ElementRule(FACTORS, new XMLSyntaxRule[]{ |
| 110 |
new ElementRule(CompoundParameter.class) |
| 111 |
}), |
| 112 |
new ElementRule(LOADINGS, new XMLSyntaxRule[]{ |
| 113 |
new ElementRule(MatrixParameterInterface.class) |
| 114 |
}), |
| 115 |
new ElementRule(ROW_PRECISION, new XMLSyntaxRule[]{ |
| 116 |
new ElementRule(DiagonalMatrix.class) |
| 117 |
}), |
| 118 |
new ElementRule(COLUMN_PRECISION, new XMLSyntaxRule[]{ |
| 119 |
new ElementRule(DiagonalMatrix.class) |
| 120 |
}), |
| 121 |
new ElementRule(CONTINUOUS, new XMLSyntaxRule[]{ |
| 122 |
new ElementRule(Parameter.class) |
| 123 |
}, true), |
| 124 |
new ElementRule(MISSING_INDICATOR, new XMLSyntaxRule[]{ |
| 125 |
new ElementRule(Parameter.class) |
| 126 |
}, true), |
| 127 |
}; |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 137 |
return rules; |
| 138 |
} |
| 139 |
|
| 140 |
@Override |
| 141 |
public String getParserDescription() { |
| 142 |
return "Sets up a latent factor model, with starting guesses for the loadings and factor matrices as well as the data for the factor analysis"; |
| 143 |
} |
| 144 |
|
| 145 |
@Override |
| 146 |
public Class getReturnType() { |
| 147 |
return LatentFactorModel.class; |
| 148 |
} |
| 149 |
} |