📄 linearregressionmodel.java
字号:
// lets build the model try { this.modelfit = (LinearRegressionModelFit)revaluator.call("buildLM", new Object[]{ getModelName(), this.params }); } catch (Exception re) { throw new QSARModelException(re.toString()); } } /** * Sets parameters required for building a linear model or using one for prediction. * * 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 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. * * 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 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 (this.modelfit == null) throw new QSARModelException("Before calling predict() you must fit the model using build()"); Double[][] newx = (Double[][])this.params.get(new String("newdata")); if (newx[0].length != this.nvar) { throw new QSARModelException("Number of independent variables used for prediction must match those used for fitting"); } try { this.modelpredict = (LinearRegressionModelPredict)revaluator.call("predictLM", new Object[]{ getModelName(), this.params }); } catch (Exception re) { throw new QSARModelException(re.toString()); } } /** * Returns an object summarizing the linear regression model. * * The return object simply wraps the fields from the summary.lm * return value. Various details can be extracted from the return object, * See {@link LinearRegressionModelSummary} for more details. * * @return A summary for the linear regression model * @throws QSARModelException if the model has not been built prior to a call * to this method */ public LinearRegressionModelSummary summary() throws QSARModelException { if (this.modelfit == null) throw new QSARModelException("Before calling summary() you must fit the model using build()"); LinearRegressionModelSummary s = null; try { s = (LinearRegressionModelSummary)revaluator.call("summaryModel", new Object[]{ getModelName() }); } catch (Exception re) { throw new QSARModelException(re.toString()); } return(s); } /** * Loads an LinearRegressionModel object from disk in to the current session. * * @param fileName The disk file containing the model * @throws QSARModelException if the model being loaded is not a linear regression model * object */ public void loadModel(String fileName) throws QSARModelException { // should probably check that the fileName does exist Object model = (Object)revaluator.call("loadModel", new Object[]{ (Object)fileName }); String modelName = (String)revaluator.call("loadModel.getName", new Object[] { (Object)fileName }); if (model.getClass().getName().equals("org.openscience.cdk.qsar.model.R.LinearRegressionModelFit")) { this.modelfit =(LinearRegressionModelFit)model; this.setModelName(modelName); Double tmp = (Double)revaluator.eval("length("+modelName+"$coefficients)-1"); nvar = (int)tmp.doubleValue(); } else throw new QSARModelException("The loaded model was not a LinearRegressionModel"); } /** * 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 QSARModelException if the model being loaded is not a linear regression model * object */ public void loadModel(String serializedModel, String modelName) throws QSARModelException { // should prxbably check that the fileName does exist Object model = (Object)revaluator.call("unserializeModel", new Object[]{ (Object)serializedModel, (Object)modelName }); String modelname = modelName; if (model.getClass().getName().equals("org.openscience.cdk.qsar.model.R.LinearRegressionModelFit")) { this.modelfit =(LinearRegressionModelFit)model; this.setModelName(modelname); Double tmp = (Double)revaluator.eval("length("+modelName+"$coefficients)-1"); nvar = (int)tmp.doubleValue(); } else throw new QSARModelException("The loaded model was not a LinearRegressionModel"); } /* interface to fit object */ /** * Gets the rank of the fitted linear model. * * This method only returns meaningful results if the <code>build</code> * method of this class has been previously called. * * @return An integer indicating the rank */ public int getFitRank() { return(this.modelfit.getRank()); } /** * Returns the residuals. * * The residuals are the response minus the fitted values. * This method only returns meaningful results if the <code>build</code> * method of this class has been previously called. * @return A double[] contaning the residuals for each observation */ public double[] getFitResiduals() { return(this.modelfit.getResiduals()); } /** * Returns the estimated coefficients. * * This method only returns meaningful results if the <code>build</code> * method of this class has been previously called. * @return A double[] containing the coefficients */ public double[] getFitCoefficients() { return(this.modelfit.getCoefficients()); } /** * Returns the residual degrees of freedom. * * This method only returns meaningful results if the <code>build</code> * method of this class has been previously called. * @return An integr indicating the residual degrees of freedom */ public int getFitDFResidual() { return(this.modelfit.getdfResidual()); } /** * Returns the fitted mean values. * * This method only returns meaningful results if the <code>build</code> * method of this class has been previously called. * @return A double[] containing the fitted values */ public double[] getFitFitted() { return(this.modelfit.getFitted()); } /* interface to predict object */ /** * Returns the degrees of freedom for residual. * * @return An integer indicating degrees of freedom */ public int getPredictDF() { return(this.modelpredict.getDF()); } /** * Returns the residual standard deviations. * * @return A double indicating residual standard deviations */ public double getPredictResidualScale() { return(this.modelpredict.getResidualScale()); } /** * Returns the predicted values for the prediction set. * * This function only returns meaningful results if the <code>predict</code> * method of this class has been called. * * @return A double[] containing the predicted values */ public double[] getPredictPredicted() { return(this.modelpredict.getPredicted()); } /** * Returns the lower prediction bounds. * * By default the bounds (both lower and upper) are confidence bounds. However * the call to <code>predict</code> can specify prediction bounds. * This function only returns meaningful results if the <code>predict</code> * method of this class has been called. * * @return A double[] containing the lower bounds for the predictions */ public double[] getPredictLowerBound() { return(this.modelpredict.getLower()); } /** * Returns the upper prediction bounds. * * By default the bounds (both lower and upper) are confidence bounds. However * the call to <code>predict</code> can specify prediction bounds. * This function only returns meaningful results if the <code>predict</code> * method of this class has been called. * * @return A double[] containing the upper bounds for the predictions */ public double[] getPredictUpperBound() { return(this.modelpredict.getUpper()); } /** * Returns the standard error of predictions. * * This function only returns meaningful results if the <code>predict</code> * method of this class has been called. * * @return A double[] containing the standard error of predictions. */ public double[] getPredictSEPredictions() { return(this.modelpredict.getSEFit()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -