Predict using userid in recommenderlab

678 Views Asked by At

I have a recommendation problem that is fairly simple: I would like build a hybrid recommender with recommenderlab in R where I recommend already liked movies in the MovieLense data set together with popular movies and some random movies.

library(recommenderlab)

data("MovieLense")

MovieLense100 <- MovieLense[rowCounts(MovieLense) > 100,]
train <- MovieLense100[1:100]
test <- MovieLense100[101:103]

## mix popular movies with a random recommendations for diversity and
## rerecommend some movies the user liked.
recom <- HybridRecommender(
  Recommender(train, method = "POPULAR"),
  Recommender(train, method = "RANDOM"),
  Recommender(train, method = "RERECOMMEND"),
  weights = c(.6, .1, .3)
)

When I have trained the model I would like to "predict" the movies for a given user using the userId. The Recommenderlab documentation suggests that this is possible as long as the userid is in the training data. However, when I try predicting using a userid in newdata I get the following error message:

as(predict(object = recom, newdata = 1), "list")
#> Error in object@predict(object@model, newdata, n = n, data = data, type = type, : If newdata is a user id then data needes to be the training dataset.

Fair enough, I try with a vector:

as(predict(recom, newdata = c(1,5)), "list")
#> Error in object@predict(object@model, newdata, n = n, data = data, type = type, : If newdata is a user id then data needes to be the training dataset.

Can't really figure out what this error message mean. Does anybody know?

1

There are 1 best solutions below

0
On BEST ANSWER

You just need to precise that you are recommending items to the user or users from the training dataset as followed :

> as(predict(recom, 1, data = train), "list")
$`1`
 [1] "Hearts and Minds (1996)"                     "Boys, Les (1997)"                            "Great Day in Harlem, A (1994)"              
 [4] "Dangerous Beauty (1998)"                     "Tough and Deadly (1995)"                     "Two or Three Things I Know About Her (1966)"
 [7] "Santa with Muscles (1996)"                   "Margaret's Museum (1995)"                    "Brassed Off (1996)"                         
[10] "Thousand Acres, A (1997)"                   

> as(predict(recom, 1:10, data = train), "list")
$`1`
 [1] "Dangerous Beauty (1998)"                     "Great Day in Harlem, A (1994)"               "Hearts and Minds (1996)"                    
 [4] "Boys, Les (1997)"                            "Santa with Muscles (1996)"                   "Two or Three Things I Know About Her (1966)"
 [7] "Tough and Deadly (1995)"                     "Saint of Fort Washington, The (1993)"        "Whole Wide World, The (1996)"               
[10] "Margaret's Museum (1995)"                   

$`5`
 [1] "Boys, Les (1997)"                            "Two or Three Things I Know About Her (1966)" "Santa with Muscles (1996)"                  
 [4] "Dangerous Beauty (1998)"                     "Great Day in Harlem, A (1994)"               "Whole Wide World, The (1996)"               
 [7] "Hearts and Minds (1996)"                     "Brassed Off (1996)"                          "Saint of Fort Washington, The (1993)"       
[10] "Celestial Clockwork (1994)"                 

$`6`
 [1] "Dangerous Beauty (1998)"                     "Two or Three Things I Know About Her (1966)" "Santa with Muscles (1996)"                  
 [4] "Tough and Deadly (1995)"                     "Brassed Off (1996)"                          "Crooklyn (1994)"                            
 [7] "Great Day in Harlem, A (1994)"               "Hearts and Minds (1996)"                     "Saint of Fort Washington, The (1993)"       
[10] "Boys, Les (1997)"                           

$`7`
 [1] "Two or Three Things I Know About Her (1966)" "Tough and Deadly (1995)"                     "Santa with Muscles (1996)"                  
 [4] "Dangerous Beauty (1998)"                     "Hearts and Minds (1996)"                     "Great Day in Harlem, A (1994)"              
 [7] "Boys, Les (1997)"                            "Brassed Off (1996)"                          "Margaret's Museum (1995)"                   
[10] "Crooklyn (1994)"                            

$`10`
 [1] "Dangerous Beauty (1998)"                     "Great Day in Harlem, A (1994)"               "Hearts and Minds (1996)"                    
 [4] "Tough and Deadly (1995)"                     "Two or Three Things I Know About Her (1966)" "Boys, Les (1997)"                           
 [7] "Santa with Muscles (1996)"                   "Brassed Off (1996)"                          "Margaret's Museum (1995)"                   
[10] "Crooklyn (1994)"                            

$`11`
 [1] "Two or Three Things I Know About Her (1966)" "Hearts and Minds (1996)"                     "Great Day in Harlem, A (1994)"              
 [4] "Tough and Deadly (1995)"                     "Brassed Off (1996)"                          "Dangerous Beauty (1998)"                    
 [7] "Boys, Les (1997)"                            "Santa with Muscles (1996)"                   "Thousand Acres, A (1997)"                   
[10] "Horse Whisperer, The (1998)"                

$`13`
 [1] "Santa with Muscles (1996)"        "Brassed Off (1996)"               "Tough and Deadly (1995)"          "Boys, Les (1997)"                
 [5] "Thousand Acres, A (1997)"         "Margaret's Museum (1995)"         "Total Eclipse (1995)"             "Love! Valour! Compassion! (1997)"
 [9] "Withnail and I (1987)"            "Prefontaine (1997)"              

$`15`
 [1] "Two or Three Things I Know About Her (1966)" "Boys, Les (1997)"                            "Great Day in Harlem, A (1994)"              
 [4] "Tough and Deadly (1995)"                     "Dangerous Beauty (1998)"                     "Saint of Fort Washington, The (1993)"       
 [7] "Santa with Muscles (1996)"                   "Hearts and Minds (1996)"                     "Celestial Clockwork (1994)"                 
[10] "Whole Wide World, The (1996)"               

$`16`
 [1] "Great Day in Harlem, A (1994)"               "Dangerous Beauty (1998)"                     "Hearts and Minds (1996)"                    
 [4] "Two or Three Things I Know About Her (1966)" "Boys, Les (1997)"                            "Santa with Muscles (1996)"                  
 [7] "Tough and Deadly (1995)"                     "Brassed Off (1996)"                          "Crooklyn (1994)"                            
[10] "Thousand Acres, A (1997)"                   

$`18`
 [1] "Great Day in Harlem, A (1994)"               "Two or Three Things I Know About Her (1966)" "Dangerous Beauty (1998)"                    
 [4] "Hearts and Minds (1996)"                     "Santa with Muscles (1996)"                   "Tough and Deadly (1995)"                    
 [7] "Boys, Les (1997)"                            "Whole Wide World, The (1996)"                "Margaret's Museum (1995)"                   
[10] "Saint of Fort Washington, The (1993)" 

Otherwise, the predict function we not know what user are you referring to.

You can also predict on test data as followed :

> as(predict(recom, test), "list")