在试验中,为了求每天价格的波动率,我决定采用ARCH模型,细看了基本原理后,觉得自己去实现挺难的。我的系统是用java写的,遗憾的是找不到java写的ARCH源码,难道,我就真得要自己写了吗?幸运的是,我发现R中有这样的ARCH包,那么,我该如何更好的利用R呢,一个疑问升起,R能不能集成到我的java系统啊?答案是:yes!
R是越来越火了,什么样的算法包都能找到,以前觉得matlab和sas是数据挖掘与分析领域的屠龙宝刀和倚天剑,那么R就是明教的圣火令了,也是一大神器。
在R中有一个rJava的包,相当于,java调用R的接口,有这样的接口,那么java调用R中算法,便如举手之劳了。下面简单介绍下配置过程。
1、下载和安装R软件;
2、在命令窗口,输入:install.packages("rJava"),下载和安装rJava包;
3、配置环境变量;注意这是r软件的安装文件里面的一些文件。这是我的相关配置:D:\R-2.11.1\library\rJava\jri ; D:\R-2.11.1\bin ;
4、引用用jar包到eclipse项目;三个包的名字分别是:JRI.jar\REngine.jar和JRIEngine.jar,在第二步下载的文件当中。
5、就是写程序测试了。
下面是个例子,把代码提出来,供参考:
002 | import java.awt.Frame; |
003 | import java.awt.FileDialog; |
005 | import java.util.Enumeration; |
007 | import org.rosuda.JRI.Rengine; |
008 | import org.rosuda.JRI.REXP; |
009 | import org.rosuda.JRI.RList; |
010 | import org.rosuda.JRI.RVector; |
011 | import org.rosuda.JRI.RMainLoopCallbacks; |
013 | class TextConsole implements RMainLoopCallbacks |
015 | public void rWriteConsole(Rengine re, String text, int oType) { |
016 | System.out.print(text); |
019 | public void rBusy(Rengine re, int which) { |
020 | System.out.println("rBusy("+which+")"); |
023 | public String rReadConsole(Rengine re, String prompt, int addToHistory) { |
024 | System.out.print(prompt); |
026 | BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); |
027 | String s=br.readLine(); |
028 | return (s==null||s.length()==0)?s:s+"\n"; |
029 | } catch (Exception e) { |
030 | System.out.println("jriReadConsole exception: "+e.getMessage()); |
035 | public void rShowMessage(Rengine re, String message) { |
036 | System.out.println("rShowMessage \""+message+"\""); |
039 | public String rChooseFile(Rengine re, int newFile) { |
040 | FileDialog fd = new FileDialog(new Frame(), (newFile==0)?"Select a file":"Select a new file", (newFile==0)?FileDialog.LOAD:FileDialog.SAVE); |
043 | if (fd.getDirectory()!=null) res=fd.getDirectory(); |
044 | if (fd.getFile()!=null) res=(res==null)?fd.getFile():(res+fd.getFile()); |
048 | public void rFlushConsole (Rengine re) { |
051 | public void rLoadHistory (Rengine re, String filename) { |
054 | public void rSaveHistory (Rengine re, String filename) { |
059 | public static void main(String[] args) { |
061 | if (!Rengine.versionCheck()) { |
062 | System.err.println("** Version mismatch - Java files don't match library version."); |
065 | System.out.println("Creating Rengine (with arguments)"); |
070 | Rengine re=new Rengine(args, false, new TextConsole()); |
071 | System.out.println("Rengine created, waiting for R"); |
073 | if (!re.waitForR()) { |
074 | System.out.println("Cannot load R"); |
082 | re.eval("data(iris)",false); |
083 | System.out.println(x=re.eval("iris")); |
085 | RVector v = x.asVector(); |
086 | if (v.getNames()!=null) { |
087 | System.out.println("has names:"); |
088 | for (Enumeration e = v.getNames().elements() ; e.hasMoreElements() ;) { |
089 | System.out.println(e.nextElement()); |
093 | RList vl = x.asList(); |
094 | String[] k = vl.keys(); |
096 | System.out.println("and once again from the list:"); |
097 | int i=0; while (i<k.length) System.out.println(k[i++]); |
101 | System.out.println(x=re.eval("iris[[1]]>mean(iris[[1]])")); |
104 | int[] bi = x.asIntArray(); |
106 | int i = 0; while (i<bi.length) { System.out.print(bi[i]==0?"F ":(bi[i]==1?"T ":"NA ")); i++; } |
107 | System.out.println(""); |
111 | boolean by[] = { true, false, false }; |
112 | re.assign("bool", by); |
113 | System.out.println(x=re.eval("bool")); |
116 | System.out.println("isTRUE? "+x.asBool().isTRUE()); |
119 | System.out.println(x=re.eval("pairlist(a=1,b='foo',c=1:5)")); |
120 | RList l = x.asList(); |
123 | String [] a = l.keys(); |
124 | System.out.println("Keys:"); |
125 | while (i<a.length) System.out.println(a[i++]); |
126 | System.out.println("Contents:"); |
128 | while (i<a.length) System.out.println(l.at(i++)); |
130 | System.out.println(re.eval("sqrt(36)")); |
131 | } catch (Exception e) { |
132 | System.out.println("EX:"+e); |
140 | long xp1 = re.rniPutString("hello"); |
141 | re.rniAssign("a", xp1, 0); |
144 | double da[] = {1.2, 2.3, 4.5}; |
145 | double db[] = {1.4, 2.6, 4.2}; |
146 | long xp3 = re.rniPutDoubleArray(da); |
147 | long xp4 = re.rniPutDoubleArray(db); |
150 | long la[] = {xp3, xp4}; |
151 | long xp5 = re.rniPutVector(la); |
154 | String sa[] = {"a","b"}; |
155 | long xp2 = re.rniPutStringArray(sa); |
156 | re.rniSetAttr(xp5, "names", xp2); |
160 | String rn[] = {"1", "2", "3"}; |
161 | long xp7 = re.rniPutStringArray(rn); |
162 | re.rniSetAttr(xp5, "row.names", xp7); |
164 | long xp6 = re.rniPutString("data.frame"); |
165 | re.rniSetAttr(xp5, "class", xp6); |
168 | re.rniAssign("b", xp5, 0); |
171 | System.out.println("Parsing"); |
172 | long e=re.rniParse("data(iris)", 1); |
173 | System.out.println("Result = "+e+", running eval"); |
174 | long r=re.rniEval(e, 0); |
175 | System.out.println("Result = "+r+", building REXP"); |
176 | REXP x=new REXP(re, r); |
177 | System.out.println("REXP result = "+x); |
180 | System.out.println("Parsing"); |
181 | long e=re.rniParse("iris", 1); |
182 | System.out.println("Result = "+e+", running eval"); |
183 | long r=re.rniEval(e, 0); |
184 | System.out.println("Result = "+r+", building REXP"); |
185 | REXP x=new REXP(re, r); |
186 | System.out.println("REXP result = "+x); |
189 | System.out.println("Parsing"); |
190 | long e=re.rniParse("names(iris)", 1); |
191 | System.out.println("Result = "+e+", running eval"); |
192 | long r=re.rniEval(e, 0); |
193 | System.out.println("Result = "+r+", building REXP"); |
194 | REXP x=new REXP(re, r); |
195 | System.out.println("REXP result = "+x); |
196 | String s[]=x.asStringArray(); |
198 | int i=0; while (i<s.length) { System.out.println("["+i+"] \""+s[i]+"\""); i++; } |
202 | System.out.println("Parsing"); |
203 | long e=re.rniParse("rnorm(10)", 1); |
204 | System.out.println("Result = "+e+", running eval"); |
205 | long r=re.rniEval(e, 0); |
206 | System.out.println("Result = "+r+", building REXP"); |
207 | REXP x=new REXP(re, r); |
208 | System.out.println("REXP result = "+x); |
209 | double d[]=x.asDoubleArray(); |
211 | int i=0; while (i<d.length) { System.out.print(((i==0)?"":", ")+d[i]); i++; } |
212 | System.out.println(""); |
214 | System.out.println(""); |
217 | REXP x=re.eval("1:10"); |
218 | System.out.println("REXP result = "+x); |
219 | int d[]=x.asIntArray(); |
221 | int i=0; while (i<d.length) { System.out.print(((i==0)?"":", ")+d[i]); i++; } |
222 | System.out.println(""); |
226 | re.eval("print(1:10/3)"); |
231 | System.out.println("Now the console is yours ... have fun"); |
235 | System.out.println("end"); |
1 | <A id=ematt:138 class=highslide href="/content/uploadfile/201208/f3ccdd27d2000e3f9255a7e3e2c4880020120804140622.jpg" target=_blank getParams="null" jQuery1346517118937="6"><IMG border=0 alt=点击查看原图 src="/content/uploadfile/201208/thum-f3ccdd27d2000e3f9255a7e3e2c4880020120804140622.jpg"></A> |