Search in sources :

Example 1 with GeneralizedLinearModel

use of dr.inference.distribution.GeneralizedLinearModel in project beast-mcmc by beast-dev.

the class GeneralizedLinearModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    //        System.err.println("PASSED 0");
    XMLObject cxo = xo.getChild(DEPENDENT_VARIABLES);
    Parameter dependentParam = null;
    if (cxo != null)
        dependentParam = (Parameter) cxo.getChild(Parameter.class);
    String family = xo.getStringAttribute(FAMILY);
    GeneralizedLinearModel glm;
    if (family.compareTo(LOGISTIC_REGRESSION) == 0) {
        glm = new LogisticRegression(dependentParam);
    } else if (family.compareTo(NORMAL_REGRESSION) == 0) {
        glm = new LinearRegression(dependentParam, false);
    } else if (family.compareTo(LOG_NORMAL_REGRESSION) == 0) {
        glm = new LinearRegression(dependentParam, true);
    } else if (family.compareTo(LOG_LINEAR) == 0) {
        glm = new LogLinearModel(dependentParam);
    } else
        throw new XMLParseException("Family '" + family + "' is not currently implemented");
    if (glm.requiresScale()) {
        cxo = xo.getChild(SCALE_VARIABLES);
        Parameter scaleParameter = null;
        //                DesignMatrix designMatrix = null;
        Parameter scaleDesign = null;
        if (cxo != null) {
            scaleParameter = (Parameter) cxo.getChild(Parameter.class);
            XMLObject gxo = cxo.getChild(INDICATOR);
            if (gxo != null)
                scaleDesign = (Parameter) gxo.getChild(Parameter.class);
        //                    designMatrix = (DesignMatrix) cxo.getChild(DesignMatrix.class);
        }
        if (scaleParameter == null)
            throw new XMLParseException("Family '" + family + "' requires scale parameters");
        if (scaleDesign == null)
            scaleDesign = new Parameter.Default(dependentParam.getDimension(), 0.0);
        else {
            if (scaleDesign.getDimension() != dependentParam.getDimension())
                throw new XMLParseException("Scale (" + dependentParam.getDimension() + ") and scaleDesign parameters (" + scaleDesign.getDimension() + ") must be the same dimension");
            for (int i = 0; i < scaleDesign.getDimension(); i++) {
                double value = scaleDesign.getParameterValue(i);
                if (value < 1 || value > scaleParameter.getDimension())
                    throw new XMLParseException("Invalid scaleDesign value");
                scaleDesign.setParameterValue(i, value - 1);
            }
        }
        glm.addScaleParameter(scaleParameter, scaleDesign);
    }
    //        System.err.println("START 0");
    addIndependentParameters(xo, glm, dependentParam);
    //        System.err.println("START 1");
    addRandomEffects(xo, glm, dependentParam);
    //        System.err.println("START 2");
    boolean checkIdentifiability = xo.getAttribute(CHECK_IDENTIFIABILITY, true);
    if (checkIdentifiability) {
        if (!glm.getAllIndependentVariablesIdentifiable()) {
            throw new XMLParseException("All design matrix predictors are not identifiable in " + xo.getId());
        }
    }
    //        System.err.println("PASSED B");
    checkFullRankOfMatrix = xo.getAttribute(CHECK_FULL_RANK, true);
    //        System.err.println("PASSED C");
    return glm;
}
Also used : GeneralizedLinearModel(dr.inference.distribution.GeneralizedLinearModel) Parameter(dr.inference.model.Parameter) LogLinearModel(dr.inference.distribution.LogLinearModel) LogisticRegression(dr.inference.distribution.LogisticRegression) LinearRegression(dr.inference.distribution.LinearRegression)

Aggregations

GeneralizedLinearModel (dr.inference.distribution.GeneralizedLinearModel)1 LinearRegression (dr.inference.distribution.LinearRegression)1 LogLinearModel (dr.inference.distribution.LogLinearModel)1 LogisticRegression (dr.inference.distribution.LogisticRegression)1 Parameter (dr.inference.model.Parameter)1