~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/qsar/model/data/lm_2.R

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#############################################
 
2
# Linear regression fit/predict converters
 
3
#############################################
 
4
lmFitConverter <-
 
5
function(obj,...)
 
6
{
 
7
    .JNew('org.openscience.cdk.qsar.model.R.LinearRegressionModelFit',
 
8
    obj$coefficients, obj$residuals,
 
9
    obj$fitted, obj$rank, obj$df.residual)
 
10
}
 
11
lmPredictConverter <- function(preds,...) {
 
12
    .JNew('org.openscience.cdk.qsar.model.R.LinearRegressionModelPredict',
 
13
    preds$fit[,1], preds$se.fit, preds$fit[,2], preds$fit[,3],
 
14
    preds$df, preds$residual.scale)
 
15
}
 
16
lmSummaryConverter <- function(sumry,...) {
 
17
    .JNew('org.openscience.cdk.qsar.model.R.LinearRegressionModelSummary',
 
18
    sumry$residuals, sumry$coeff,
 
19
    sumry$sigma, sumry$r.squared, sumry$adj.r.squared,
 
20
    sumry$df[2], sumry$fstatistic,
 
21
    attr(sumry$coeff, 'dimnames')[[1]],
 
22
    attr(sumry$coeff, 'dimnames')[[2]])
 
23
}
 
24
 
 
25
buildLM <- function(modelname, params) {
 
26
    # params is a java.util.HashMap containing the parameters
 
27
    # we need to extract them and add them to this environment
 
28
    paramlist <- hashmap.to.list(params)
 
29
    attach(paramlist)
 
30
 
 
31
    # x will come in as a double[][]
 
32
    x <- matrix(unlist(x), nrow=length(x), byrow=TRUE)
 
33
 
 
34
    # assumes y ~ all columns of x
 
35
    d <- data.frame(y=y,x)
 
36
    assign(modelname, lm(y~., d, weights=weights), pos=1)
 
37
    detach(paramlist)
 
38
    get(modelname)
 
39
}
 
40
 
 
41
predictLM <- function( modelname, params) {
 
42
    # params is a java.util.HashMap containing the parameters
 
43
    # we need to extract them and add them to this environment
 
44
    paramlist <- hashmap.to.list(params)
 
45
    attach(paramlist)
 
46
 
 
47
    newx <- data.frame( matrix(unlist(newdata), nrow=length(newdata), byrow=TRUE) )
 
48
    names(newx) <- names(get(modelname)$coef)[-1]
 
49
    if (interval == '' || !(interval %in% c('confidence','prediction')) ) { 
 
50
        interval = 'confidence'
 
51
    } 
 
52
    preds <- predict( get(modelname), newx, se.fit = TRUE, interval=interval);
 
53
    class(preds) <- 'lmregprediction'
 
54
 
 
55
    detach(paramlist)
 
56
    preds
 
57
}
 
58
 
 
59