-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathOrdinaryLeastSquares.java
More file actions
103 lines (86 loc) · 2.85 KB
/
OrdinaryLeastSquares.java
File metadata and controls
103 lines (86 loc) · 2.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package examples;
import com.github.rcaller.rstuff.RCaller;
import com.github.rcaller.rstuff.RCode;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Mehmet Hakan Satman
* @since 2.0
* @version 2.0
*
*/
public class OrdinaryLeastSquares {
public static void main(String[] args) {
new OrdinaryLeastSquares();
}
/**
*
* Ordinary Least Squares with RCaller.
* This class runs the lm() function of R
* for regressing user defined vector y on vector x from java
*/
public OrdinaryLeastSquares() {
try {
/**
* Creating an instance of RCaller class
*/
RCaller caller = RCaller.create();
RCode code = RCode.create();
/**
* Creating vectors x and y
*/
double[] x = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
double[] y = new double[]{2, 4, 6, 8, 10, 12, 14, 16, 18, 30};
/**
* Converting Java arrays to R arrays
*/
code.addDoubleArray("x", x);
code.addDoubleArray("y", y);
/**
* R code for regression of y on x
*/
code.addRCode("ols<-lm(y~x)");
/**
* Run all! Our regression code returns something
* and we want to handle the variable 'ols'
*/
caller.setRCode(code);
caller.runAndReturnResult("ols");
/**
* Getting names of components of the variable 'ols'
*/
System.out.println("Available results from lm() object:");
System.out.println(caller.getParser().getNames());
/**
* Getting ols$residulas, ols$coefficients and ols$fitted.values
* The names after '$' are components of the lm() object in R language
*/
double[] residuals = caller.getParser().getAsDoubleArray("residuals");
double[] coefficients = caller.getParser().getAsDoubleArray("coefficients");
double[] fitteds = caller.getParser().getAsDoubleArray("fitted_values");
/**
* Printing results
*/
System.out.println("Coefficients:");
for (int i = 0; i < coefficients.length; i++) {
System.out.println("Beta " + i + " = " + coefficients[i]);
}
System.out.println("Residuals:");
for (int i = 0; i < residuals.length; i++) {
System.out.println(i + " = " + residuals[i]);
}
System.out.println("Fitted Values:");
for (int i = 0; i < fitteds.length; i++) {
System.out.println(i + " = " + fitteds[i]);
}
} catch (Exception e) {
/**
* Note that, RCaller does some OS based works such as creating an external process and
* reading files from temporary directories or creating images for plots. Those operations
* may cause exceptions for those that user must handle the potential errors.
*/
Logger.getLogger(OrdinaryLeastSquares.class.getName()).log(Level.SEVERE, e.getMessage());
}
}
}