📄 linearregressionmodel.java
字号:
package org.openscience.cdk.qsar.model.R2;import org.openscience.cdk.qsar.model.QSARModelException;import org.openscience.cdk.tools.LoggingTool;import org.rosuda.JRI.REXP;import org.rosuda.JRI.RList;import java.io.File;import java.util.HashMap;/** * A modeling class that provides a linear least squares regression model. * <p/> * 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> * double[][] x; * double[] y; * 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.getFittedValues() * double[] predicted = lrm.getModelPredict().asList.at("fit").asDoubleArray(); * </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 rJava environment. * * @author Rajarshi Guha * @cdk.require r-project * @cdk.require java1.5+ * @cdk.module qsar * @cdk.keyword linear regression * @cdk.keyword R */public class LinearRegressionModel extends org.openscience.cdk.qsar.model.R2.RModel { private static int globalID = 0; private int nvar = 0; private RList modelPredict = null; private static LoggingTool logger; /** * Constructs a LinearRegressionModel object. * <p/> * 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() throws QSARModelException { super(); logger = new LoggingTool(this); params = new HashMap(); int currentID = LinearRegressionModel.globalID; org.openscience.cdk.qsar.model.R2.LinearRegressionModel.globalID++; this.setModelName("cdkLMModel" + currentID); } /** * Constructs a LinearRegressionModel object. * <p/> * 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 org.openscience.cdk.qsar.model.QSARModelException * if the number of observations in x and y do not match */ public LinearRegressionModel(double[][] xx, double[] yy) throws QSARModelException { super(); params = new HashMap(); int currentID = LinearRegressionModel.globalID; LinearRegressionModel.globalID++; this.setModelName("cdkLMModel" + 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. * <p/> * 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 org.openscience.cdk.qsar.model.QSARModelException * if the number of observations in x and y do not match */ public LinearRegressionModel(double[][] xx, double[] yy, double[] weights) throws QSARModelException { super(); params = new HashMap(); int currentID = LinearRegressionModel.globalID; org.openscience.cdk.qsar.model.R2.LinearRegressionModel.globalID++; this.setModelName("cdkLMModel" + 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); } /** * Fits a linear regression model. * <p/> * 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"); } } // lets build the model String paramVarName = loadParametersIntoRSession(); String cmd = "buildLM(\"" + getModelName() + "\", " + paramVarName + ")"; REXP ret = rengine.eval(cmd); if (ret == null) { logger.debug("Error in buildLM"); throw new QSARModelException("Error in buildLM"); } // remove the parameter list rengine.eval("rm(" + paramVarName + ")"); // save the model object on the Java side modelObject = ret.asList(); } /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -