Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a formula interface with elm #4

Merged
merged 8 commits into from Oct 15, 2021
Merged

Conversation

edwindj
Copy link
Contributor

@edwindj edwindj commented Oct 14, 2021

First of all, thanks for this package!

FWIW, I have added a formula interface with the function elm, which makes working with your package a bit smoother.
(tests are included). Feel free to adapt and include it in your package

Best,

Edwin de Jonge

elm

elm is a wrapper for elm_train, transforms a data.frame with formula into the necessary input for elm_train.
Furthermore, the returned object has some nice printing properties:

library(elmNNRcpp)
elm(Species ~ ., data = iris, nhid = 20, actfun="sig")
## Extreme learning model, elm (classification):
## 
## call:  elm(Species ~ ., data = iris, nhid = 20, actfun = "sig") 
## hidden units       : 20 
## activation function: sig 
## accuracy           : 0.9733333 
## 
## confusion matrix :
##             predicted
## observed     setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         48         2
##   virginica       0          2        48

classification

Using predict on a elm object calls elm_predict with some extra's. For classification the type option
can be used returning either class, probabilities or raw

mod_elm <- elm(Species ~ ., data = iris, nhid = 20, actfun="sig")
# predict classes
predict(mod_elm, newdata = iris[1:3,-5])
## [1] setosa setosa setosa
## Levels: setosa versicolor virginica
# predict probabilities
predict(mod_elm, newdata = iris[1:3,-5], type="prob")
##         setosa versicolor   virginica
## [1,] 0.9775201 0.02021516 0.002264738
## [2,] 0.9519374 0.02871766 0.019344988
## [3,] 0.9620877 0.02854287 0.009369432
# predict elm output
predict(mod_elm, newdata = iris[1:3,-5], type="raw")
##        setosa  versicolor   virginica
## [1,] 1.018682 -0.02106638 0.002360102
## [2,] 1.009991 -0.03046901 0.020524740
## [3,] 1.020310 -0.03027020 0.009936443
data("ionosphere")
elm(class ~ ., data = ionosphere, nhid=20, actfun="relu")
## Extreme learning model, elm (classification):
## 
## call:  elm(class ~ ., data = ionosphere, nhid = 20, actfun = "relu") 
## hidden units       : 20 
## activation function: relu 
## accuracy           : 0.8603989 
## 
## confusion matrix :
##         predicted
## observed   b   g
##        b  89  37
##        g  12 213

regression

In regression the mse is returned.

data("Boston")
elm(medv ~ ., data = Boston, nhid = 40, actfun="relu")
## Extreme learning model, elm (regression):
## 
## call:  elm(medv ~ ., data = Boston, nhid = 40, actfun = "relu") 
## hidden units       : 40 
## activation function: relu 
## mse                : 20.20491

@mlampros
Copy link
Owner

@edwindj thank you for this PR.

@mlampros
Copy link
Owner

mlampros commented Oct 14, 2021

@edwindj I receive the following ERROR, WARNING and NOTEs (see the attached 00check.log file)
I checked the package locally because the Github Action was disabled. Can you have a look? I just enabled the Github Action, so if you make any commits the checks will be run

00check.log

(you can ignore the WARNING about the Insufficient package version)

@edwindj
Copy link
Contributor Author

edwindj commented Oct 15, 2021

(teaching today, so won't be able able to look into it, before this evening)

@edwindj
Copy link
Contributor Author

edwindj commented Oct 15, 2021

Looking at the github actions, it seems to be fixed 😀

@mlampros
Copy link
Owner

thank you very much @edwindj for this pull request. Give me a few days to update the CRAN version.
I'd like to add you also in the DESCRIPTION file as a contributor, is it ok by you?
I can use you existing Details

Edwin de Jonge, <edwindjonge@gmail.com>, ORCID: 0000-0002-6580-4718

Let me know. thanks again.

@mlampros mlampros merged commit f44f415 into mlampros:master Oct 15, 2021
@edwindj
Copy link
Contributor Author

edwindj commented Oct 15, 2021

You re welcome!
Ok, if you add me as a contributor, thanks!

Best,

Edwin

@mlampros
Copy link
Owner

@edwindj,

I intended today to submit the updated version to CRAN and I ran an R CMD check --as-cran on the cloned repository, however I receive the following error on Linux which comes from the examples (the tests seem ok),

* checking examples ... ERROR
Running examples inelmNNRcpp-Ex.Rfailed
The error most likely occurred in:

> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: elm
> ### Title: Fit an extreme learning model
> ### Aliases: elm
> 
> ### ** Examples
> 
> elm(Species ~ ., data = iris, nhid = 20, actfun="sig")
Extreme learning model, elm (classification):

call:  elm(Species ~ ., data = iris, nhid = 20, actfun = "sig") 
hidden units       : 20 
activation function: sig 
accuracy           : 0.9733333 

confusion matrix :
            predicted
observed     setosa versicolor virginica
  setosa         50          0         0
  versicolor      0         48         2
  virginica       0          2        48
> 
> mod_elm <- elm(Species ~ ., data = iris, nhid = 20, actfun="sig")
> 
> # predict classes
> predict(mod_elm, newdata = iris[1:3,-5])
Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "elm"
Calls: predict
Execution halted

can you reproduce this error locally using the cloned repository?

@edwindj
Copy link
Contributor Author

edwindj commented Oct 24, 2021 via email

@mlampros
Copy link
Owner

@edwindj,

initially I thought the error comes from the git clone https://github.com/mlampros/elmNNRcpp command but it doesn't make any difference if I use git clone https://github.com/mlampros/elmNNRcpp.git

It seems that the error comes from the auto-generated .Rd files and especially of the Examples section. Once you clone the repository can you please delete all .Rd files of the 'man' directory in the elmNNRcpp package?

Then once deleted, the way I build and check a package that includes Rcpp code every time before I submit to CRAN is the following:

devtools::document(pkg = '/your_default_dir/elmNNRcpp')     # creates the updated .Rd files based on the .R files of the 'R' directory
remove_so_and_o_files = sapply(list.files('/your_default_dir/elmNNRcpp/src', pattern = ".o$|.so$", full.names = T), function(x) file.remove(x))
setwd('/your_default_dir/elmNNRcpp')
tools::package_native_routine_registration_skeleton(".", con = "/your_default_dir/elmNNRcpp/src/init.c", character_only = FALSE)
Rcpp::compileAttributes(verbose = T)
setwd('/your_default_dir')
system("R CMD build elmNNRcpp")
system("R CMD check --as-cran elmNNRcpp_1.0.3.tar.gz")
system("R CMD INSTALL elmNNRcpp_1.0.3.tar.gz")

I think that way you might reproduce the error compared to a simple R CMD build and check. The paths might need to be modified in the previous code snippet because they are adjusted for a unix OS

@mlampros
Copy link
Owner

moreover can you add

@importFrom stats model.frame model.matrix terms

in the elm() function

@importFrom stats model.frame model.matrix

in the predict.elm() function

and update the DESCRIPTION file by adding the 'stats' package?

Imports: Rcpp (>= 0.12.17), stats

@mlampros
Copy link
Owner

highly probable this error comes from the devtools::document() and the update of the NAMESPACE on Linux OS (I have a similar issue currently in another PR). Which OS do you use?

@mlampros
Copy link
Owner

I submitted the updated version to CRAN (1.0.4). This was actually a problem with the R version I used. After updating to 4.1.1 the issue was resolved.

@edwindj
Copy link
Contributor Author

edwindj commented Jan 28, 2022

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants