Search in sources :

Example 1 with LogLinearModel

use of dr.inference.distribution.LogLinearModel 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)

Example 2 with LogLinearModel

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

the class OldGLMSubstitutionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DataType dataType = DataTypeUtils.getDataType(xo);
    if (dataType == null)
        dataType = (DataType) xo.getChild(DataType.class);
    int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
    LogLinearModel glm = (LogLinearModel) xo.getChild(GeneralizedLinearModel.class);
    int length = glm.getXBeta().length;
    if (length != rateCount) {
        throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (rateCount) + " dimensions.  However GLM dimension is " + length);
    }
    XMLObject cxo = xo.getChild(dr.oldevomodelxml.substmodel.ComplexSubstitutionModelParser.ROOT_FREQUENCIES);
    FrequencyModel rootFreq = (FrequencyModel) cxo.getChild(FrequencyModel.class);
    if (dataType != rootFreq.getDataType()) {
        throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its rootFrequencyModel.");
    }
    return new OldGLMSubstitutionModel(xo.getId(), dataType, rootFreq, glm);
}
Also used : FrequencyModel(dr.evomodel.substmodel.FrequencyModel) GeneralizedLinearModel(dr.inference.distribution.GeneralizedLinearModel) DataType(dr.evolution.datatype.DataType) LogLinearModel(dr.inference.distribution.LogLinearModel) OldGLMSubstitutionModel(dr.evomodel.substmodel.OldGLMSubstitutionModel)

Example 3 with LogLinearModel

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

the class GLMSubstitutionModelParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    DataType dataType = DataTypeUtils.getDataType(xo);
    if (dataType == null)
        dataType = (DataType) xo.getChild(DataType.class);
    int rateCount = (dataType.getStateCount() - 1) * dataType.getStateCount();
    LogLinearModel glm = (LogLinearModel) xo.getChild(GeneralizedLinearModel.class);
    int length = glm.getXBeta().length;
    if (length != rateCount) {
        throw new XMLParseException("Rates parameter in " + getParserName() + " element should have " + (rateCount) + " dimensions.  However GLM dimension is " + length);
    }
    XMLObject cxo = xo.getChild(ComplexSubstitutionModelParser.ROOT_FREQUENCIES);
    FrequencyModel rootFreq = (FrequencyModel) cxo.getChild(FrequencyModel.class);
    if (dataType != rootFreq.getDataType()) {
        throw new XMLParseException("Data type of " + getParserName() + " element does not match that of its rootFrequencyModel.");
    }
    return new GLMSubstitutionModel(xo.getId(), dataType, rootFreq, glm);
}
Also used : FrequencyModel(dr.oldevomodel.substmodel.FrequencyModel) GeneralizedLinearModel(dr.inference.distribution.GeneralizedLinearModel) DataType(dr.evolution.datatype.DataType) LogLinearModel(dr.inference.distribution.LogLinearModel) GLMSubstitutionModel(dr.oldevomodel.substmodel.GLMSubstitutionModel)

Aggregations

GeneralizedLinearModel (dr.inference.distribution.GeneralizedLinearModel)3 LogLinearModel (dr.inference.distribution.LogLinearModel)3 DataType (dr.evolution.datatype.DataType)2 FrequencyModel (dr.evomodel.substmodel.FrequencyModel)1 OldGLMSubstitutionModel (dr.evomodel.substmodel.OldGLMSubstitutionModel)1 LinearRegression (dr.inference.distribution.LinearRegression)1 LogisticRegression (dr.inference.distribution.LogisticRegression)1 Parameter (dr.inference.model.Parameter)1 FrequencyModel (dr.oldevomodel.substmodel.FrequencyModel)1 GLMSubstitutionModel (dr.oldevomodel.substmodel.GLMSubstitutionModel)1