src/rJava/RSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rJava; | |
import org.rosuda.JRI.REXP; | |
import org.rosuda.JRI.Rengine; | |
public class RSample { | |
public static void main(String[] args) { | |
System.out.println("R_HOME:" + System.getenv("R_HOME")); | |
System.out.println("java.library.path:" | |
+ System.getProperty("java.library.path")); | |
Rengine engine = new Rengine(new String[] { | |
"--no-save" | |
}, false, null); | |
try { | |
String filename = "dist.dat"; | |
engine.eval("data<-read.csv(\"" + filename + "\")"); | |
engine.eval("x<-data[,1]"); | |
engine.eval("y<-data[,2]"); | |
String[] exprs = { | |
"a*x+b", "a*x^b", "a*b^x" | |
}; | |
for (String expr : exprs) { | |
double[] r = func(engine, expr); | |
String out = expr; | |
out += (": a=" + r[0]); | |
out += (", b=" + r[1]); | |
out += (" (AIC " + r[2] + ")"); | |
System.out.println(out); | |
} | |
} finally { | |
engine.end(); | |
} | |
} | |
private static double[] func(Rengine engine, String f) { | |
engine.eval("result<-nls(y~(" + f + "), start=c(a=1, b=1))"); | |
REXP result0 = engine.eval("summary(result)"); | |
REXP result1 = engine.eval("AIC(result)"); | |
double[] c = ((result0.asVector()).at("parameters")).asDoubleArray(); | |
double[] ret = { | |
c[0], c[1], result1.asDouble() | |
}; | |
return ret; | |
} | |
} |
dist.dat
0,22 1,25 2,28 3,19 4,20 5,18 6,18 7,12 8,15 9,11 10,22 11,16 12,19 13,22 14,18 15,10 16,16 17,11 18,11 19,11 20,10 21,15 22,12 23,10 24,10 25,11 26,12 27,10 28,7 29,8 30,10 31,11 32,9 33,6 34,9 35,10 36,11 37,6 38,5 39,6 40,8 41,10 42,15 43,7 44,5 45,3 46,4 47,2 48,5 49,9 50,3 51,7 52,6 53,5 54,5 55,1 56,7 57,5 58,5 59,4 60,3 61,2 62,2 63,7 64,4 65,4 66,9 67,5 68,7 69,6 70,6 71,2 72,2 73,2 74,3 75,2 76,1 77,2 78,4 79,1 80,3 81,1 82,3 83,3 84,4 85,2 86,4 87,6 88,0 89,7 90,3 91,3 92,3 93,2 94,8 95,0 96,0 97,2 98,3 99,3 100,4 101,5 102,1 103,3 104,3 105,1 106,3 107,3 108,0 109,3 110,4 111,1 112,2 113,0 114,3 115,2 116,0 117,2 118,2 119,0 120,1 121,1 122,2 123,1 124,2 125,2 126,3 127,3 128,0 129,1 130,3 131,1 132,4 133,2 134,0 135,1 136,1 137,1 138,2 139,1 140,1 141,0 142,2 143,0 144,0 145,0 146,2 147,0 148,0 149,0 150,2 151,0 152,0 153,0 154,0 155,4 156,1 157,0 158,0 159,1 160,1 161,1 162,1 163,0 164,0 165,0 166,1 167,1 168,1 169,1 170,1 171,0 172,0 173,0 174,0 175,1 176,0 177,0 178,0 179,0 180,0 181,2 182,1 183,0 184,1 185,2 186,0 187,0 188,0 189,0 190,0 191,0 192,1 193,2 194,0 195,0 196,1 197,0 198,0 199,0 200,1 201,0 202,0 203,1 204,1 205,0 206,0 207,0 208,1 209,0 210,0 211,0 212,1 213,0 214,0 215,1 216,0 217,0 218,0 219,0 220,0 221,1 222,0 223,0 224,0 225,0 226,0 227,0 228,0 229,0 230,0 231,0 232,0 233,0 234,0 235,0 236,0 237,0 238,0 239,0 240,0 241,0 242,0 243,0 244,0 245,0 246,0 247,1 248,1 249,1 250,0 251,0 252,0 253,0 254,0 255,0 256,0 257,0 258,0 259,0 260,0 261,0 262,0 263,0 264,0 265,0 266,0 267,0 268,0 269,0 270,0 271,0 272,0 273,0 274,0 275,0 276,0 277,0 278,0 279,0 280,0 281,0 282,0 283,0 284,0 285,0 286,0 287,0 288,0 289,0 290,0 291,0 292,0 293,0 294,0 295,0 296,0 297,0 298,0 299,0 300,0 301,0 302,0 303,0 304,0 305,0 306,0 307,0 308,0 309,0 310,0 311,0 312,0 313,0 314,0 315,0 316,0 317,0 318,0 319,0 320,0 321,1 322,0 323,0 324,0 325,0 326,1
結果
R_HOME:/usr/local/Cellar/r/3.1.2/R.framework/Versions/3.1/Resources java.library.path:.:/usr/local/Cellar/r/3.1.2/R.framework/Versions/3.1/Resources/library/rJava/jri a*x+b: a=-0.03550333158708252, b=8.498046248230285 (AIC 1712.5095155708004) a*x^b: a=39.75900134122691, b=-0.5633965054825036 (AIC 1550.0470141120295) a*b^x: a=21.2572188567732, b=0.9751177432706232 (AIC 1240.9971794263668)
0 件のコメント:
コメントを投稿