library(inline)
library(RcppMLPACK)
library(Rcpp)
##
## Attaching package: 'Rcpp'
##
## The following object is masked from 'package:inline':
##
## registerPlugin
code <- '
arma::mat X = as<arma::mat>(design_matrix);
arma::vec Y = as<arma::vec>(response);
mlpack::regression::LogisticRegression<> lr(X,Y);
arma::vec parameters = lr.Parameters();
return List::create(_["parameters"] = parameters);
'
Logistic_Regression <- cxxfunction(signature(design_matrix="numeric", response ="numeric"),body=code,plugin="RcppMLPACK")
## In file included from file4d7eebdf53f.cpp:3:
## In file included from /Users/jared/lib/R/RcppMLPACK/include/RcppMLPACK.h:26:
## In file included from /Users/jared/lib/R/RcppMLPACK/include/mlpack/core/tree/cover_tree.hpp:26:
## In file included from /Users/jared/lib/R/RcppMLPACK/include/mlpack/core/tree/cover_tree/cover_tree.hpp:490:
## /Users/jared/lib/R/RcppMLPACK/include/mlpack/core/tree/cover_tree/cover_tree_impl.hpp:795:14: warning: unused variable 'originalSum' [-Wunused-variable]
## const long originalSum = nearSetSize + farSetSize + usedSetSize;
## ^
## 1 warning generated.
set.seed(1)
beta = runif(10,-2,2)
x=matrix(rnorm(1000*10,0,1),ncol=10)
y=rbinom(1000,size=1,prob=plogis(x%*%beta))
pars = Logistic_Regression(design_matrix=t(x),response=y)
## L-BFGS iteration 0; objective 693.147.
## L-BFGS iteration 1; objective 438.144.
## L-BFGS iteration 2; objective 345.09.
## L-BFGS iteration 3; objective 299.547.
## L-BFGS iteration 4; objective 280.902.
## L-BFGS iteration 5; objective 274.184.
## L-BFGS iteration 6; objective 272.983.
## L-BFGS iteration 7; objective 272.883.
## L-BFGS iteration 8; objective 272.876.
## L-BFGS iteration 9; objective 272.876.
## L-BFGS iteration 10; objective 272.876.
## L-BFGS iteration 11; objective 272.876.
## L-BFGS iteration 12; objective 272.876.
## L-BFGS iteration 13; objective 272.876.
## L-BFGS iteration 14; objective 272.876.
## L-BFGS iteration 15; objective 272.876.
## Line search failed. Stopping optimization.
## LogisticRegression::LogisticRegression(): final objective of trained model is 272.876.
glmc = coef(glm(y~x, family=binomial))
data.frame(MLPACK=pars$parameters, glm=glmc, true=c(0,beta))
## MLPACK glm true
## (Intercept) -0.01087 -0.01087 0.0000
## x1 -1.01975 -1.01975 -0.9380
## x2 -0.70388 -0.70388 -0.5115
## x3 0.18856 0.18856 0.2914
## x4 1.82055 1.82055 1.6328
## x5 -1.31840 -1.31840 -1.1933
## x6 1.85256 1.85256 1.5936
## x7 1.87586 1.87586 1.7787
## x8 0.60579 0.60579 0.6432
## x9 0.41441 0.41441 0.5165
## x10 -2.02928 -2.02928 -1.7529