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

📄 linearregressionmodel.java

📁 化学图形处理软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *  Copyright (C) 2004-2007  The Chemistry Development Kit (CDK) project * *  Contact: cdk-devel@lists.sourceforge.net * *  This program is free software; you can redistribute it and/or *  modify it under the terms of the GNU Lesser General Public License *  as published by the Free Software Foundation; either version 2.1 *  of the License, or (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU Lesser General Public License for more details. * *  You should have received a copy of the GNU Lesser General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */package org.openscience.cdk.qsar.model.R;import org.openscience.cdk.qsar.model.QSARModelException;import java.util.HashMap;/**  * A modeling class that provides a linear least squares regression model. * * When instantiated this class ensures that the R/Java interface has been  * initialized. The response and independent variables can be specified at construction * time or via the <code>setParameters</code> method. The actual fitting procedure is carried out by <code>build</code> after which  * the model may be used to make predictions. * <p> * Currently, the design of the class is quite sparse as it does not allow subsetting, * variable names, setting of contrasts and so on.  * It is also assumed that the values of all the variables are defined (i.e., not such that  * they are <a href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/NA.html">NA</a> * in an R session). * The use of * this class is shown in the following code snippet * <pre> * try { *     LinearRegressionModel lrm = new LinearRegressionModel(x,y); *     lrm.build(); *     lrm.setParameters("newdata", newx); *     lrm.setParameters("interval", "confidence"); *     lrm.predict(); * } catch (QSARModelException qme) { *     System.out.println(qme.toString()); * } * double[] fitted = lrm.getFitFitted(); * double[] predictedvalues = lrm.getPredictPredicted(); * </pre> * Note that when making predictions, the new X matrix and interval type can be set by calls * to setParameters(). In general, the arguments for lm() and predict.lm() can be set via * calls to setParameters(). The following table lists the parameters that can be set and their  * expected types. More detailed informationis available in the R documentation. * <center> * <table border=1 cellpadding=5> * <THEAD> * <tr> * <th>Name</th><th>Java Type</th><th>Notes</th> * </tr> * </thead> * <tbody> * <tr> * <td>x</td><td>Double[][]</td><td></td> * </tr> * <tr> * <td>y</td><td>Double[]</td><td>Length should be equal to the rows of x</td> * </tr> * <tr> * <td>weights</td><td>Double[]</td><td>Length should be equal to rows of x</td> * </tr> * <tr> * <td>newdata</td><td>Double[][]</td><td>Number of columns should be the same as in x</td> * </tr> * <tr> * <td>interval</td><td>String</td><td>Can be 'confidence' or 'predicton'</td> * </tr> * </tbody> * </table> * </center> * In general the <code>getFit*</code> methods provide access to results from the fit * and <code>getPredict*</code> methods provide access to results from the prediction (i.e., * prediction using the model on new data). The values returned correspond to the various  * values returned by the <a href="http://stat.ethz.ch/R-manual/R-patched/library/stats/html/lm.html">lm</a> * and <a href="http://stat.ethz.ch/R-manual/R-patched/library/stats/html/predict.lm.html">predict.lm</a> * functions in R. * <p> * See {@link RModel} for details regarding the R and SJava environment. * * @author Rajarshi Guha * @cdk.require r-project * @cdk.module qsar *  * @cdk.keyword regression, linear * @deprecated  */public class LinearRegressionModel extends RModel {    private static int globalID = 0;    private int currentID;    private LinearRegressionModelFit modelfit = null;    private LinearRegressionModelPredict modelpredict = null;    private HashMap params = null;    private int nvar = 0;        /**     * Constructs a LinearRegressionModel object.     *     * The constructor simply instantiates the model ID. Dependent and independent variables     * should be set via setParameters().      * <p>     * An important feature of the current implementation is that <i>all</i> the      * independent variables are used during the fit. Furthermore no subsetting is possible.     * As a result when setting these via setParameters() the caller should specify only     * the variables and observations that will be used for the fit.     */    public LinearRegressionModel(){        super();        this.params = new HashMap();        this.currentID = LinearRegressionModel.globalID;        LinearRegressionModel.globalID++;        this.setModelName("cdkLMModel"+this.currentID);    }    /**     * Constructs a LinearRegressionModel object.     *     * The constructor allows the user to specify the      * dependent and independent variables. The length of the dependent variable     * array should equal the number of rows of the independent variable matrix. If this      * is not the case an exception will be thrown.     * <p>     * An important feature of the current implementation is that <i>all</i> the      * independent variables are used during the fit. Furthermore no subsetting is possible.     * As a result when creating an instance of this object the caller should specify only     * the variables and observations that will be used for the fit.     *      * @param xx An array of independent variables. The observations should be in the rows     * and the variables should be in the columns     * @param yy an array containing the dependent variable     * @throws QSARModelException if the number of observations in x and y do not match     */    public LinearRegressionModel(double[][] xx, double[] yy) throws QSARModelException{        super();        this.params = new HashMap();        this.currentID = LinearRegressionModel.globalID;        LinearRegressionModel.globalID++;        this.setModelName("cdkLMModel"+this.currentID);        int nrow = yy.length;        this.nvar = xx[0].length;        if (nrow != xx.length) {            throw new QSARModelException("The number of values for the dependent variable does not match the number of rows of the design matrix");        }                Double[][] x = new Double[nrow][this.nvar];        Double[] y = new Double[nrow];        Double[] weights = new Double[nrow];        for (int i = 0; i < nrow; i++) {            y[i] = new Double(yy[i]);            weights[i] = new Double(1.0);        }        for (int i = 0; i < nrow; i++) {            for (int j = 0; j < this.nvar; j++)                 x[i][j] = new Double(xx[i][j]);        }        params.put("x", x);        params.put("y", y);        params.put("weights", weights);    }            /**     * Constructs a LinearRegressionModel object.     *     * The constructor allows the user to specify the      * dependent and independent variables as well as weightings for     * the observations.      * <p>     * The length of the dependent variable     * array should equal the number of rows of the independent variable matrix. If this      * is not the case an exception will be thrown.     * <p>     * An important feature of the current implementation is that <i>all</i> the      * independent variables are used during the fit. Furthermore no subsetting is possible.     * As a result when creating an instance of this object the caller should specify only     * the variables and observations that will be used for the fit.     *      * @param xx An array of independent variables. The observations should be in the rows     * and the variables should be in the columns     * @param yy an array containing the dependent variable     * @param weights Specifies the weights for each observation. Unit weights are equivilant     * to OLS     * @throws QSARModelException if the number of observations in x and y do not match     */    public LinearRegressionModel(double[][] xx, double[] yy, double[] weights) throws QSARModelException{        super();        this.params = new HashMap();        this.currentID = LinearRegressionModel.globalID;        LinearRegressionModel.globalID++;        this.setModelName("cdkLMModel"+this.currentID);        int nrow = yy.length;        this.nvar = xx[0].length;        if (nrow != xx.length) {            throw new QSARModelException("The number of values for the dependent variable does not match the number of rows of the design matrix");        }        if (nrow != weights.length) {            throw new QSARModelException("The length of the weight vector does not match the number of rows of the design matrix");        }                Double[][] x = new Double[nrow][this.nvar];        Double[] y = new Double[nrow];        Double[] wts = new Double[nrow];        for (int i = 0; i < nrow; i++) {            y[i] = new Double(yy[i]);            wts[i] = new Double(weights[i]);        }        for (int i = 0; i < nrow; i++) {            for (int j = 0; j < this.nvar; j++)                 x[i][j] = new Double(xx[i][j]);        }        params.put("x", x);        params.put("y", y);        params.put("weights", wts);    }    protected void finalize() {        revaluator.voidEval("rm("+this.getModelName()+",pos=1)");    }                /**     * Fits a linear regression model.     *     * This method calls the R function to fit a linear regression model     * to the specified dependent and independent variables. If an error     * occurs in the R session, an exception is thrown.     * <p>     * Note that, this method should be called prior to calling the various get     * methods to obtain information regarding the fit.     */    public void build() throws QSARModelException {        // lets do some checks in case stuff was set via setParameters()        Double[][] x;        Double[] y,weights;        x = (Double[][])this.params.get("x");        y = (Double[])this.params.get("y");        weights = (Double[])this.params.get("weights");        if (this.nvar == 0) this.nvar = x[0].length;        else {            if (y.length != x.length) {                throw new QSARModelException("Number of observations does no match number of rows in the design matrix");            }            if (weights.length != y.length) {                throw new QSARModelException("The weight vector must have the same length as the number of observations");            }        }

⌨️ 快捷键说明

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