⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linearregressionmodel.java

📁 化学图形处理软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Sets parameters required for building a linear model or using one for prediction.     * <p/>     * This function allows the caller to set the various parameters available     * for the lm() and predict.lm() R routines. See the R help pages for the details of the available     * parameters.     *     * @param key A String containing the name of the parameter as described in the     *            R help pages     * @param obj An Object containing the value of the parameter     * @throws org.openscience.cdk.qsar.model.QSARModelException     *          if the type of the supplied value does not match the     *          expected type     */    public void setParameters(String key, Object obj) throws QSARModelException {        // since we know the possible values of key we should check the coresponding        // objects and throw errors if required. Note that this checking can't really check        // for values (such as number of variables in the X matrix to build the model and the        // X matrix to make new predictions) - these should be checked in functions that will        // use these parameters. The main checking done here is for the class of obj and        // some cases where the value of obj is not dependent on what is set before it        if (key.equals("y")) {            if (!(obj instanceof Double[])) {                throw new QSARModelException("The class of the 'y' object must be Double[]");            }        }        if (key.equals("x")) {            if (!(obj instanceof Double[][])) {                throw new QSARModelException("The class of the 'x' object must be Double[][]");            }        }        if (key.equals("weights")) {            if (!(obj instanceof Double[])) {                throw new QSARModelException("The class of the 'weights' object must be Double[]");            }        }        if (key.equals("interval")) {            if (!(obj instanceof String)) {                throw new QSARModelException("The class of the 'interval' object must be String");            }            if (!(obj.equals("confidence") || obj.equals("prediction"))) {                throw new QSARModelException("The type of interval must be: prediction or confidence");            }        }        if (key.equals("newdata")) {            if (!(obj instanceof Double[][])) {                throw new QSARModelException("The class of the 'newdata' object must be Double[][]");            }        }        this.params.put(key, obj);    }    /**     * Uses a fitted model to predict the response for new observations.     * <p/>     * This function uses a previously fitted model to obtain predicted values     * for a new set of observations. If the model has not been fitted prior to this     * call an exception will be thrown. Use <code>setParameters</code>     * to set the values of the independent variable for the new observations and the     * interval type.     *     * @throws org.openscience.cdk.qsar.model.QSARModelException     *          if the model has not been built prior to a call     *          to this method. Also if the number of independent variables specified for prediction     *          is not the same as specified during model building     */    public void predict() throws QSARModelException {        if (modelObject == null)            throw new QSARModelException("Before calling predict() you must fit the model using build()");        Double[][] newx = (Double[][]) params.get("newdata");        if (newx[0].length != nvar) {            throw new QSARModelException("Number of independent variables used for prediction must match those used for fitting");        }        String pn = loadParametersIntoRSession();        REXP ret = rengine.eval("predictLM(\"" + getModelName() + "\", " + pn + ")");        if (ret == null) throw new QSARModelException("Error occured in prediction");        // remove the parameter list        rengine.eval("rm(" + pn + ")");        modelPredict = ret.asList();    }    /**     * Get the R object obtained from <code>predict.lm()</code>.     *     * @return The result of the prediction. Contains a number of fields corresponding to     *         predicted values, SE and other items depending on the parameters that we set.     *         Note that the call to <code>predict.lm()</code> is performde with <code>se.fit = TRUE</code>     */    public RList getModelPredict() {        return modelPredict;    }    /**     * Returns an <code>RList</code> object summarizing the linear regression model.     * <p/>     * The return object can be queried via the <code>RList</code> methods to extract the     * required components.     *     * @return A summary for the linear regression model     * @throws org.openscience.cdk.qsar.model.QSARModelException     *          if the model has not been built prior to a call     *          to this method     */    public RList summary() throws QSARModelException {        if (modelObject == null)            throw new QSARModelException("Before calling summary() you must fit the model using build()");        REXP ret = rengine.eval("summary(" + getModelName() + ")");        if (ret == null) {            logger.debug("Error in summary()");            throw new QSARModelException("Error in summary()");        }        return ret.asList();    }    /**     * Loads an LinearRegressionModel object from disk in to the current session.     *     * @param fileName The disk file containing the model     * @throws org.openscience.cdk.qsar.model.QSARModelException     *          if the model being loaded is not a linear regression model     *          object  or the file does not exist     */    public void loadModel(String fileName) throws QSARModelException {        File f = new File(fileName);        if (!f.exists()) throw new QSARModelException(fileName + " does not exist");        rengine.assign("tmpFileName", fileName);        REXP ret = rengine.eval("loadModel(tmpFileName)");        if (ret == null) throw new QSARModelException("Model could not be loaded");        String name = ret.asList().at("name").asString();        if (!isOfClass(name, "lm")) {            removeObject(name);            throw new QSARModelException("Loaded object was not of class \'lm\'");        }        modelObject = ret.asList().at("model").asList();        setModelName(name);        nvar = getCoefficients().length - 1; // since the intercept is also returned    }    /**     * Loads an LinearRegressionModel object from a serialized string into the current session.     *     * @param serializedModel A String containing the serialized version of the model     * @param modelName       A String indicating the name of the model in the R session     * @throws org.openscience.cdk.qsar.model.QSARModelException     *          if the model being loaded is not a linear regression model     *          object     */    public void loadModel(String serializedModel, String modelName) throws QSARModelException {        rengine.assign("tmpSerializedModel", serializedModel);        rengine.assign("tmpModelName", modelName);        REXP ret = rengine.eval("unserializeModel(tmpSerializedModel, tmpModelName)");        if (ret == null) throw new QSARModelException("Model could not be unserialized");        String name = ret.asList().at("name").asString();        if (!isOfClass(name, "lm")) {            removeObject(name);            throw new QSARModelException("Loaded object was not of class \'lm\'");        }        modelObject = ret.asList().at("model").asList();        setModelName(name);        nvar = getCoefficients().length - 1; // as the intercept is also returned    }// Autogenerated code: assumes that 'modelObject' is// a RList object    /**     * Gets the <code>assign</code> field of an <code>'lm'</code> object.     *     * @return The value of the assign field     */    public int[] getAssign() {        return modelObject.at("assign").asIntArray();    }    /**     * Gets the <code>coefficients</code> field of an <code>'lm'</code> object.     *     * @return The value of the coefficients field     */    public double[] getCoefficients() {        return modelObject.at("coefficients").asDoubleArray();    }    /**     * Gets the <code>df.residual</code> field of an <code>'lm'</code> object.     *     * @return The value of the df.residual field     */    public int getDfResidual() {        return modelObject.at("df.residual").asInt();    }    /**     * Gets the <code>effects</code> field of an <code>'lm'</code> object.     *     * @return The value of the effects field     */    public double[] getEffects() {        return modelObject.at("effects").asDoubleArray();    }    /**     * Gets the <code>fitted.values</code> field of an <code>'lm'</code> object.     *     * @return The value of the fitted.values field     */    public double[] getFittedValues() {        return modelObject.at("fitted.values").asDoubleArray();    }    /**     * Gets the <code>model</code> field of an <code>'lm'</code> object.     *     * @return The value of the model field     */    public RList getModel() {        return modelObject.at("model").asList();    }    /**     * Gets the <code>qr</code> field of an <code>'lm'</code> object.     *     * @return The value of the qr field     */    public RList getQr() {        return modelObject.at("qr").asList();    }    /**     * Gets the <code>rank</code> field of an <code>'lm'</code> object.     *     * @return The value of the rank field     */    public int getRank() {        return modelObject.at("rank").asInt();    }    /**     * Gets the <code>residuals</code> field of an <code>'lm'</code> object.     *     * @return The value of the residuals field     */    public double[] getResiduals() {        return modelObject.at("residuals").asDoubleArray();    }    /**     * Gets the <code>xlevels</code> field of an <code>'lm'</code> object.     *     * @return The value of the xlevels field     */    public RList getXlevels() {        return modelObject.at("xlevels").asList();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -