Locally Weighted Projection Regression (LWPR) using rdyncall and the LWPR C library
This file contains 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
library(rdyncall) | |
dynbind("lwpr", " | |
lwpr_init_model(*<LWPR_Model>iiZ)i; | |
lwpr_duplicate_mode(*<LWPR_Model>*<LWPR_Model>)i; | |
lwpr_set_init_alpha(*<LWPR_Model>d)i; | |
lwpr_set_init_D(*<LWPR_Model>*dd)i; | |
lwpr_set_init_D_diagonal(*<LWPR_Model>*d)i; | |
lwpr_set_init_D_spherical(*<LWPR_Model>d)i; | |
lwpr_update(*<LWPR_Model>*d*d*d*d)i; | |
lwpr_predict(*<LWPR_Model>*dd*d*d*d)v; | |
lwpr_write_xml(*<LWPR_Model>Z)i; | |
lwpr_read_xml(*<LWPR_Model>Z*i)i; | |
") | |
parseStructInfos("LWPR_Model{iiii*d*d*ciidd*d*d*d*d*ddddddddiipp*d*d*d}nIn nInStore nOut n_data mean_x var_x name diag_only meta meta_rate penalty init_alpha norm_in norm_out init_D init_M w_gen w_prune init_lambda final_lambda tau_lambda init_S2 add_threshold kernel update_D sub ws storage xn yn") | |
testfunc = function(x) { | |
10 * sin(7.8*log(1+x)) / (1 + 0.1*x**2) | |
} | |
Ntr = 500 | |
Xtr = 10 * runif(Ntr) | |
Ytr = sapply(Xtr, testfunc) + 0.1 * rnorm(Ntr) * Xtr | |
model = new.struct(LWPR_Model) | |
lwpr_init_model(model, 1, 1, "Tutorial") | |
lwpr_set_init_D(model, 20, 0) | |
model$update_D = 1 | |
model$diag_only = TRUE | |
model$penalty = 0.0001 | |
lwpr_set_init_alpha(model, 40) | |
for (i in 1:20) { | |
idx = sample(Ntr,Ntr) | |
for (j in idx) { | |
yp=0 | |
lwpr_update(model, Xtr[j], Ytr[j], yp, NULL) | |
} | |
} | |
Ntest = 500; | |
Xtest = seq(0,10,length.out=Ntest) | |
Ytest = numeric(Ntest) | |
Conf = numeric(Ntest) | |
for (k in 1:Ntest) { | |
yp = 0 | |
conf = 0 | |
lwpr_predict(model, Xtest[k], 0.0, yp, conf, NULL); | |
Ytest[k] = yp | |
Conf[k] = conf | |
} | |
plot(Ytr ~ Xtr, pch=19, cex=0.5, col='red') | |
lines(Xtest , Ytest, col="blue") | |
lines(Xtest , Ytest + Conf, col="cyan") | |
lines(Xtest , Ytest - Conf, col="cyan") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment