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

Update on LFDA using new lfda package on CRAN #219

Merged
merged 9 commits into from
Aug 14, 2015
Merged

Update on LFDA using new lfda package on CRAN #219

merged 9 commits into from
Aug 14, 2015

Conversation

terrytangyuan
Copy link
Contributor

No description provided.

@zachmayer
Copy link
Collaborator

It looks like you have an error in your tests.

@terrytangyuan
Copy link
Contributor Author

Yeah I wasn't able to figure out why. Let me know if you found anything I missed.

@topepo
Copy link
Owner

topepo commented Aug 13, 2015

Since you now have these functions in your own R package, I'm not sure why you are still committing to this package.

@zachmayer
Copy link
Collaborator

@topepo He needs to revise the lfda model in caret to use his new package

@@ -53,5 +53,6 @@ Suggests:
superpc,
Cubist,
testthat (>= 0.9.1),
rARPACK
rARPACK,
lfda
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this import. We don't want caret to depend on modeling packages.

@zachmayer
Copy link
Collaborator

@terrytangyuan Please delete the lfda.R and lfda.default.Rd files too.

@zachmayer
Copy link
Collaborator

@topepo This PR should remove all lfda-related code, as it's migrated to @terrytangyuan's new package on CRAN.

@zachmayer
Copy link
Collaborator

@terrytangyuan You can also add a lfda.R file to the models section so it can be run in base caret. Inside that file you can specify that the model depends on the new lfda package.

@terrytangyuan
Copy link
Contributor Author

@zachmayer Just double check: so I should remove all dependencies and code related to lfda and then write a new file to the models section, correct?

@zachmayer
Copy link
Collaborator

Correct. That's the preferred way to add new models to caret: no code or dependencies live inside the package, just a custom model definition that tells caret which model to load.

@terrytangyuan
Copy link
Contributor Author

@zachmayer I tried adding S3method(predict, lfda) to NAMESPACE but predict on lfda didn't work (if you uncomment the lines related to predict in testthat\test_models_lfda.R). I'll need to add predict method to lfda package later but any workaround with this so predict can be used in caret now? I believe I missed something important.

@zachmayer
Copy link
Collaborator

On this PR, edit the .travis.yml file. Find this line:
{yaml}

  • ./travis-tool.sh install_github jimhester/covr

And add this below it
```{yaml}```
  - ./travis-tool.sh install_github terrytangyuan/lfda

This will install lfda from github. Then you'll need to add predict.lfda to your github package. Make sure to tag it with:

@export
@method predict lfda

Here's what the full roxygen2 block for lfda might look like:

@title predict.lfda
@description make predictions for new data from an lfda object
@param object An lfda object
@param newdata Data to use for predictions
@param ... ignored

@@ -1,4 +1,5 @@
library(caret)
library(lfda)
library(testthat)

test_that('test lfda model training and prediction', {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should move to your own package, as it JUST tests the lfda model.

@zachmayer
Copy link
Collaborator

Finally, tests for lfda and predict.lfda belong in your own package. Change your test here to be something like:

test_that('test lfda model training and prediction', {
  skip_on_cran()
  set.seed(1)
  tr_dat <- twoClassSim(200)
  te_dat <- twoClassSim(200)

  model <- train(
    Class~., tr_dat, 
    method = "lfda", 
    trControl = trainControl(method='cv', number='5')
    )
  expect_is(model, 'train')
  expect_is(model$finalModel, 'lfda')
  expect_is(model$results$Accuracy, 'numeric')
  expect_true(model$results$Accuracy >= .75)
})

library = c("lfda"),
type = "Classification",
grid = function(x, y, len = NULL) {
stop("grid is not available for lfda. ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is always going to cause your model to fail. Caret models ALWAYS need a grid. See here for a simple modelInfo you can copy: https://github.com/terrytangyuan/caret/blob/newlfda/models/files/lda.R

@zachmayer
Copy link
Collaborator

Also, take a quick read through here: http://topepo.github.io/caret/custom_models.html

@terrytangyuan
Copy link
Contributor Author

Thank you! These are very helpful. I'll read through and revise this. I've missed a lot of stuff.

@terrytangyuan
Copy link
Contributor Author

@zachmayer I kept getting this Model lfda is not in caret's built-in library. What should I change to include it?

@zachmayer
Copy link
Collaborator

You need to re-run the script to create the models data object: https://github.com/topepo/caret/blob/master/models/parseModels.R

@zachmayer zachmayer merged commit 7c12bc7 into topepo:master Aug 14, 2015
@zachmayer
Copy link
Collaborator

Ok, we'll see if this build passes on master and I'll make a PR to fix it.

@zachmayer
Copy link
Collaborator

For future reference, you need to keep your fork up-to-date with topepo/caret master. This will reduce the likelihood of merge conflicts on future PRs.

Basically, what happened was Max added a new model at the same time you did. This created a conflict.

The resolution of the conflict is to merge mx's branch into yours and re-create models.RData such that it includes both models (max's new model and your changed one)

@terrytangyuan terrytangyuan deleted the newlfda branch August 14, 2015 19:34
@terrytangyuan terrytangyuan restored the newlfda branch August 14, 2015 19:34
@terrytangyuan terrytangyuan deleted the newlfda branch August 14, 2015 19:34
@zachmayer
Copy link
Collaborator

For future reference, you can test your model as follows:

modelInfo <- list(label = "Local Fisher Discriminant Analysis",
                  library = c("lfda"),
                  type = "Classification",
                  grid = function(x, y, len = NULL, search = "grid") data.frame(r="none",metric="none", knn="none"),
                  parameters = data.frame(parameter = c("r", "metric", "knn"),
                                          class = c("numeric", "character", "numeric"),
                                          label = c("# Reduced Dimensions",
                                                    "Type of Transformation Metric",
                                                    "# of Nearest Neighbors")),
                  fit = function(x, y, param, ...) {
                    theDots <- list(...)

                    argList <- list(x = x, y = y, r = ifelse(is.null(param$r, 3, param$r)),
                                    metric = ifelse(is.null(param$metric), "plain", param$metric),
                                    knn = ifelse(is.null(param$knn, 5, param$knn)))
                    argList <- c(argList, theDots)

                    if(is.data.frame(x)) x <- as.matrix(x)

                    out <- do.call("lfda", argList)

                    out$call <- NULL
                    out
                  },
#                 predict = function(modelFit, newdata, submodels = NULL)
#                   predict(modelFit, newdata),
                  prob = NULL,
                  predictors = function(x, ...) {
                    # if dimensionality of original data is not reduced
                    if(dim(x$T)[1]==dim(x$T)[2]){
                      return(colnames(x$Z))
                    } else {
                      print("predictors are not available for lfda model with dimension reduction. ")
                      return(NULL)
                    }
                  },
                  tags = c("Metric Learning", "Local Metric Learning", "Dimension Reduction",
                           "Multimodality Preservance", "Fisher Discriminant Analysis",
                           "Classification", "Pre-processing")
                  )

  library(lfda)
  tr_dat <- twoClassSim(200)
  te_dat <- twoClassSim(200)

  lfda.model <- train(
    x=tr_dat[,-16],y=tr_dat[,16],
    method = modelInfo
  )

As you can see this fails. Digging deeper, we can see the fit function is failing:

modelInfo$fit(tr_dat[,-16], tr_dat[,16], modelInfo$grid(1))

I'm making a PR to fix this, but in the future you need to test your models and make sure they work before submitting them. You get a freebie this time since there were git issues to work out =D

Next time you should be able to do this on your own.

@zachmayer
Copy link
Collaborator

I can't actually get lfda to run on any input data: lfda(matrix(runif(100), ncol=10), runif(10), r=3):

5 stop("'x' must be an array of at least two dimensions") 
4 colSums(Xc^2) 
3 as.matrix(colSums(Xc^2)) 
2 t(as.matrix(colSums(Xc^2))) 
1 lfda(matrix(runif(100), ncol = 10), runif(10), r = 3) 

Can you send me an example of how to run lfda, e.g. on the iris dataset?

@terrytangyuan
Copy link
Contributor Author

I am not by my pc but you can check the tests in lfda package.
On Aug 14, 2015 4:00 PM, "Zach Mayer" notifications@github.com wrote:

I can't actually get lfda to run on any input data: lfda(matrix(runif(100),
ncol=10), runif(10), r=3):

5 stop("'x' must be an array of at least two dimensions") 4 colSums(Xc^2) 3 as.matrix(colSums(Xc^2)) 2 t(as.matrix(colSums(Xc^2))) 1 lfda(matrix(runif(100), ncol = 10), runif(10), r = 3)

Can you send me an example of how to run lfda, e.g. on the iris dataset?


Reply to this email directly or view it on GitHub
#219 (comment).

@zachmayer
Copy link
Collaborator

What data type do the arguments to lfda need to be?

@zachmayer
Copy link
Collaborator

Ok, go it figured out. PR coming tongiht.

@terrytangyuan
Copy link
Contributor Author

Thanks! And yep I got the lesson.

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.

3 participants