library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)

set.seed(2018)

#make random data to cluster
synth.data <- data.frame(x1 = c(rnorm(20, 3, 1.5), rnorm(20,0,1), rnorm(20,5,1)), 
                         x2 = c(rnorm(20, 0, 1), rnorm(20,4,1), rnorm(20,5,1)))

ndata <- nrow(synth.data) 
ndim <- ncol(synth.data)

#plot the data
synth.data %>% ggplot(aes(x = x1, y= x2)) + geom_point(shape = 1) + theme_bw()

#Euclidean distance
u_dist <- function(u, v){ 
  sqrt(sum((u - v) ** 2))
}

#we will make (k=3) k_means model.
k <- 3 
cents <- data.frame(cl = 1:k)
# pick three coordinate
cents <- cbind(cents, synth.data[sample(1:60, k),])
cents
##    cl         x1       x2
## 27  1 -1.0586618 6.597463
## 30  2 -0.6318248 4.275945
## 33  3 -0.2662727 5.474087
#make it as a function, and iterate until it does not change.
# cent_f <- function(cents){
#   #a: distance between cent1 and x,b: distance betweencent 2 and x, c: distance between cent3 and x.
#   a <- apply(synth.data, 1, function(x){u_dist(cents[1,2:3],x)})
#   b <- apply(synth.data, 1, function(x){u_dist(cents[2,2:3],x)})
#   c <- apply(synth.data, 1, function(x){u_dist(cents[3,2:3],x)})
#   
#   #each row contains the information of distance between data and centroid.
#   mat <- data.frame(dcent1 = a, dcent2 =b , dcent3 = c)
# 
#   mat_min <- apply(mat,1,which.min)
# 
#   #make a group using an index.
#   gr1 <- synth.data [mat_min == 1,]
#   gr2 <- synth.data [mat_min == 2,]
#   gr3 <- synth.data [mat_min == 3,]

# #  calculate mean in the gr 1,2,3 . and save it in the variable n_cents
# n_cents <- data.frame(cl = 1:3 , x1 = c(mean(gr1[,1]),mean(gr2[,1]),mean(gr3[,1])),x2 = c(mean(gr1[,2]),mean(gr2[,2]),mean(gr3[,2])))
# #compare all the elements in the matrix
# all(n_cents == cents)
# 
#if centroid does not change, then stop.
#   if ( all(n_cents == cents) ){ 
#     synth.data$cl <- mat_min
#     synth.data %>% 
#       ggplot(aes(x = x1, y= x2, col = cl)) + geom_point(shape = 1) + theme_bw() + geom_point(data = n_cents, shape = 4, col = 'red')
#   }
#   else{
#     #if it changes, then do it again wiht new centroid. (recursive function.)
#     cent_f(n_cents)
#   }
# }
# 
# 
# mat_min
# 
# cent_f(cents)
# mat_min
#this works at the R studio, but not when i kint this as html. so, just make another version with the same logic.


#to make mat min , we already know 4 times is optimal (from the previous one).
for (i in 1:4){
  a <- apply(synth.data, 1, function(x){u_dist(cents[1,2:3],x)})
  b <- apply(synth.data, 1, function(x){u_dist(cents[2,2:3],x)})
  c <- apply(synth.data, 1, function(x){u_dist(cents[3,2:3],x)})
  
  mat <- data.frame(dcent1 = a, dcent2 =b , dcent3 = c)
  mat_min <- apply(mat,1,which.min)
  
  gr1 <- synth.data [mat_min == 1,]
  gr2 <- synth.data [mat_min == 2,]
  gr3 <- synth.data [mat_min == 3,]
  cents <- data.frame(cl = 1:3 , x1 = c(mean(gr1[,1]),mean(gr2[,1]),mean(gr3[,1])),x2 = c(mean(gr1[,2]),mean(gr2[,2]),mean(gr3[,2])))
}
mat_min
##  [1] 2 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
## [36] 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
synth.data$cl <- mat_min
synth.data %>% 
  ggplot(aes(x = x1, y= x2, col = cl)) + geom_point(shape = 1) + theme_bw() + geom_point(data = cents, shape = 4, col = 'red' )

library(recommenderlab) 
## Loading required package: Matrix
## Loading required package: arules
## 
## Attaching package: 'arules'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
## Loading required package: proxy
## 
## Attaching package: 'proxy'
## The following object is masked from 'package:Matrix':
## 
##     as.matrix
## The following objects are masked from 'package:stats':
## 
##     as.dist, dist
## The following object is masked from 'package:base':
## 
##     as.matrix
## Loading required package: registry
  library(dplyr) 
  library(tidyr)
## 
## Attaching package: 'tidyr'
## The following object is masked from 'package:Matrix':
## 
##     expand
  library(tibble) 
  library(doBy)
  load('ratings.RData')
  #Pearson correlationfunction.
  pearsonCor <- function(x, y){ 
    x_mean <- mean(x, na.rm = T) 
    y_mean <- mean(y, na.rm = T) 
    idx <- !is.na(x) & !is.na(y) 
    if(sum(idx) == 0) return(NA) 
    x_new <- x[idx] 
    y_new <- y[idx] 
    sum((x_new- x_mean) * (y_new -y_mean)) / sqrt( sum( (x_new - x_mean)**2) * sum( (y_new-y_mean) **2) )
  }
  
  
  recommend <- function(u){
    sim <- apply(ratings, 1, function(x) {
      pearsonCor(u, x)
    })
    sim
    k = 2
    
    k_neighbors <- setdiff(which.maxn(sim, k+1), 5) 
    k_recommend <- apply(ratings[k_neighbors,], 2, function(x) { mean(x, na.rm = T)})
    k_recommend
    
    k_recommend_final <- k_recommend[is.na(u)]
    print(sort(k_recommend_final, decreasing = T))
  }
  
  #Apply it to all users
  apply(ratings,1, function(x){recommend(x)})
## Me Before You      Sherlock 
##             4             3 
## Sherlock  Titanic   Matrix 
##        5        2        1 
##  The Avengers Me Before You 
##             5             1 
##      Titanic The Avengers  Transformer 
##            5            3            2 
## The Avengers      Titanic 
##            5            2 
##       Titanic   Transformer Me Before You 
##             5             3             1
## $A
## Me Before You      Sherlock 
##             4             3 
## 
## $B
## Sherlock  Titanic   Matrix 
##        5        2        1 
## 
## $C
##  The Avengers Me Before You 
##             5             1 
## 
## $D
##      Titanic The Avengers  Transformer 
##            5            3            2 
## 
## $E
## The Avengers      Titanic 
##            5            2 
## 
## $F
##       Titanic   Transformer Me Before You 
##             5             3             1
  ratings.df <- read.csv('train_v2.csv')
  #delete user id, and move user to rowname.
  rating_matrix <- ratings.df %>% 
    select(-ID) %>% 
    spread(movie, rating) %>% 
    remove_rownames() %>% 
    column_to_rownames(var = 'user')
  
  #make it as continuous data type
  rating_rrm <- as(as(rating_matrix, 'matrix'), 'realRatingMatrix') 
  # row name : movie name, col name : users
  # I picked the movies which have more than 51 reviews,and users which rate over 100 reviews.
  rating_rrm <- rating_rrm[rowCounts(rating_rrm) > 50, colCounts(rating_rrm) > 100]
  
  
  #normalize the data(becuase there would be some poeple who would like to give it only biased score, 4,5 . or 1,2)
  rating_rrm_norm <- normalize(rating_rrm)
  image(rating_rrm[1:50, 1:50]) 

  image(rating_rrm_norm[1:50, 1:50])

  print(object.size(rating_matrix), units = "auto")
## 85.1 Mb
  print(object.size(rating_rrm), units = "auto")
## 7.5 Mb
  recommenderRegistry$get_entries(dataType = "realRatingMatrix")
## $ALS_realRatingMatrix
## Recommender method: ALS for realRatingMatrix
## Description: Recommender for explicit ratings based on latent factors, calculated by alternating least squares algorithm.
## Reference: Yunhong Zhou, Dennis Wilkinson, Robert Schreiber, Rong Pan (2008). Large-Scale Parallel Collaborative Filtering for the Netflix Prize, 4th Int'l Conf. Algorithmic Aspects in Information and Management, LNCS 5034.
## Parameters:
##   normalize lambda n_factors n_iterations min_item_nr seed
## 1      NULL    0.1        10           10           1 NULL
## 
## $ALS_implicit_realRatingMatrix
## Recommender method: ALS_implicit for realRatingMatrix
## Description: Recommender for implicit data based on latent factors, calculated by alternating least squares algorithm.
## Reference: Yifan Hu, Yehuda Koren, Chris Volinsky (2008). Collaborative Filtering for Implicit Feedback Datasets, ICDM '08 Proceedings of the 2008 Eighth IEEE International Conference on Data Mining, pages 263-272.
## Parameters:
##   lambda alpha n_factors n_iterations min_item_nr seed
## 1    0.1    10        10           10           1 NULL
## 
## $IBCF_realRatingMatrix
## Recommender method: IBCF for realRatingMatrix
## Description: Recommender based on item-based collaborative filtering.
## Reference: NA
## Parameters:
##    k   method normalize normalize_sim_matrix alpha na_as_zero
## 1 30 "Cosine"  "center"                FALSE   0.5      FALSE
## 
## $POPULAR_realRatingMatrix
## Recommender method: POPULAR for realRatingMatrix
## Description: Recommender based on item popularity.
## Reference: NA
## Parameters:
##   normalize
## 1  "center"
##                                                      aggregationRatings
## 1 new("standardGeneric", .Data = function (x, na.rm = FALSE, dims = 1, 
##                                                   aggregationPopularity
## 1 new("standardGeneric", .Data = function (x, na.rm = FALSE, dims = 1, 
## 
## $RANDOM_realRatingMatrix
## Recommender method: RANDOM for realRatingMatrix
## Description: Produce random recommendations (real ratings).
## Reference: NA
## Parameters: None
## 
## $RERECOMMEND_realRatingMatrix
## Recommender method: RERECOMMEND for realRatingMatrix
## Description: Re-recommends highly rated items (real ratings).
## Reference: NA
## Parameters:
##   randomize minRating
## 1         1        NA
## 
## $SVD_realRatingMatrix
## Recommender method: SVD for realRatingMatrix
## Description: Recommender based on SVD approximation with column-mean imputation.
## Reference: NA
## Parameters:
##    k maxiter normalize
## 1 10     100  "center"
## 
## $SVDF_realRatingMatrix
## Recommender method: SVDF for realRatingMatrix
## Description: Recommender based on Funk SVD with gradient descend.
## Reference: NA
## Parameters:
##    k gamma lambda min_epochs max_epochs min_improvement normalize verbose
## 1 10 0.015  0.001         50        200           1e-06  "center"   FALSE
## 
## $UBCF_realRatingMatrix
## Recommender method: UBCF for realRatingMatrix
## Description: Recommender based on user-based collaborative filtering.
## Reference: NA
## Parameters:
##     method nn sample normalize
## 1 "cosine" 25  FALSE  "center"
  #User based CF
  ubcf_model <- Recommender(rating_rrm, method = 'UBCF')
  #모델과 실제를 본다. 
  recom <- predict(ubcf_model, rating_rrm[1:2, ]) 
  recom <- predict(ubcf_model, rating_rrm[1:2, ], type = 'ratings') 
  recom
## 2 x 1790 rating matrix of class 'realRatingMatrix' with 3364 ratings.
  # divide into two sets, train and test
  e <- evaluationScheme(rating_rrm, method="split", train=0.8, given=10) 
  #r1: user based. r2: item based.
  r1 <- Recommender(getData(e, "train"), "UBCF") 
  r2 <- Recommender(getData(e, "train"), "IBCF")
  #which one is more accurate?
  p1 <- predict(r1, getData(e, "known"), type="ratings") 
  p2 <- predict(r2, getData(e, "known"), type="ratings")
  
  error <- rbind( calcPredictionAccuracy(p1, getData(e, "unknown")), 
                  calcPredictionAccuracy(p2, getData(e, "unknown"))
  )
  
  rownames(error) <- c("UBCF","IBCF")
  error
##          RMSE      MSE       MAE
## UBCF 1.064338 1.132815 0.8447415
## IBCF 1.758544 3.092475 1.3555584