# Basic recommendation engine using R

**Data Perspective**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

#### Types of Collaborative Filtering:

- User based Collaborative Filtering
- Item based Collaborative filtering

In this post will explain about User based Collaborative Filtering. This algorithm usually works by searching a large group of people and finding a smaller set with tastes similar to yours. It looks at other things they like and combines them to create a ranked list of suggestions.

**Implementing User Based Collaborative Filtering:**

This involves two steps:

- Calculating Similarity Function
- Recommend items to users based on user Similarity Score

#### Step1- Calculate Similarity Score for CHAN:

Creating Similarity score for people helps us to identify similar people. We use Cosine based Similarity function to calculate the similarity between the users. Know more about cosine similarity here. In R we have a cosine function readily available:

#### Step2- recommending Movies for CHAN:

The above explanation is written in the below R function:

rec_itm_for_user = function(userNo)

{ #calcualte column wise sum

col_sums= list()

rat_user = critics[userNo,2:7]

x=1

tot = list()

z=1

for(i in 1:ncol(rat_user)){

if(is.na(rat_user[1,i]))

{

col_sums[x] = sum(weight_mat[,i],na.rm=TRUE)

x=x+1

temp = as.data.frame(weight_mat[,i])

sum_temp=0

for(j in 1:nrow(temp))

{ if(!is.na(temp[j,1]))

{

sum_temp = sum_temp+user_sim[j,7]

}

}

tot[z] = sum_temp z=z+1

}

}

z=NULL

z=1

for(i in 1:ncol(rat_user)){

if(is.na(rat_user[1,i]))

{

rat_user[1,i] = col_sums[[z]]/tot[[z]] z=z+1

}

}

return(rat_user)

}

Calling the above function gives the below results:

Recommending movies for Chan will be in the order: Matrix (3.48), Titanic(2.81), Inception(2.35).

complete sourceCode is available on github

**leave a comment**for the author, please follow the link and comment on their blog:

**Data Perspective**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.