The Octave-version of GPstuff can be used from R by using the RcppOctave package. RcppOctave allows to call any Octave functions and browse their documentation from R, and pass variables between R and Octave.
To start using GPstuff with R, load RcppOctave and add GPstuff directory to (Octave) path.
#install.packages("RcppOctave") if RcppOctave not already installed require(RcppOctave) o_source(text=" P = genpath('/path/to/gpstuff/);' addpath(P); ")
The GPstuff functionality can be used in R e.g. by running demos from files
o_source("/path/to/gpstuff/gp/demo_classific.m")
or by running commands in blocks surrounded by o_source(text=" ").
o_source(text=" L = strrep(S, 'demo_classific.m', 'demodata/synth.tr'); x = load(L); y=x(:,end); y = 2.*y-1; ")
Currently, Octave (version 4.0) does not support all the design choices of GPstuff and therefore the performance and functionality are not equal to the MATLAB-version. Below are, however, some fixes to known problems along with some demos that work with RcppOctave.
Note that when saving and loading structs, Octave fails to properly save the function (and subfunction) handles GPstuff uses. After loading a struct, the handles can be fixed with the provided fixFunctionHandles-function (currently only included in the development version of GPstuff).
o_source(text=" gp = gp_set('lik',lik_gaussian(),'cf',gpcf_sexp()); save('file','gp') clear('gp') load('file') ") #this fails due to broken function handles: o_source(text=" example_x = [-1 -1 ; 0 0 ; 1 1]; [K, C] = gp_trcov(gp, example_x) ") #this works: o_source(text=" example_x = [-1 -1 ; 0 0 ; 1 1]; gp = fixFunctionHandles(gp); [K, C] = gp_trcov(gp, example_x) ")
Sometimes the figures plotted in Octave fail to appear when using RcppOctave (more details here). This can be fixed by creating a new figure window or updating the figure after plot-command.
#Figure might fail to appear. o_source(text=" x = [1 5; 1 1]; plot(x) ") #Force figure to appear o_source(text=" x = [1 5; 1 1]; figure; plot(x(1,:),x(2,:)) %drawnow; this is an alternative to figure; this refreshes the plot ")
Alternatively, the object can be passed to and plotted with R.
#Pass object to R and plot it o_source(text=" x = [1 5; 1 1]; ") x <- .O$x plot(x[1,],x[2,],type="l")
All demos work by simply running the code blocks inside o_source(text="block"). Below are links to three examples edited for R.