{"id":2535,"date":"2024-09-24T21:21:35","date_gmt":"2024-09-24T12:21:35","guid":{"rendered":"https:\/\/secondlife.lol\/?p=2535"},"modified":"2024-09-24T21:26:24","modified_gmt":"2024-09-24T12:26:24","slug":"r-anaalysys-example-movies-ratings-data","status":"publish","type":"post","link":"https:\/\/secondlife.lol\/en\/r-anaalysys-example-movies-ratings-data\/","title":{"rendered":"R Data Analysis Example: Learn hands-on analysis from movie ratings data"},"content":{"rendered":"<p class=\"wp-block-paragraph\">You've probably rated or written a review after watching your favorite movie. Your ratings and reviews aren't just an expression of opinion, they're data in and of themselves that can provide a wealth of insights. In this post <strong>Analyze movie rating data with an R data analysis example<\/strong>and how to handle data in the real world. <strong>Derive insights<\/strong>Let's take a look at it together. It's packed with important information, so stay tuned!<\/p>\n\n\n<style>.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{padding-top:var(--global-kb-spacing-sm, 1.5rem);padding-right:var(--global-kb-spacing-sm, 1.5rem);padding-bottom:var(--global-kb-spacing-sm, 1.5rem);padding-left:var(--global-kb-spacing-sm, 1.5rem);border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{padding-top:0px;padding-right:0px;padding-bottom:0px;padding-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title-wrap{color:var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{color:var(--global-palette2, #2B6CB0);font-size:28px;font-weight:regular;font-style:normal;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list{color:var(--global-palette1, #3182CE);line-height:2em;font-weight:regular;font-style:normal;margin-top:var(--global-kb-spacing-sm, 1.5rem);margin-right:0px;margin-bottom:0px;margin-left:0px;}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap .kb-table-of-content-list .kb-table-of-contents__entry:hover{color:var(--global-palette6, #718096);}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}}@media all and (max-width: 1024px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}@media all and (max-width: 767px){.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-content-wrap{border-top:3px solid var(--global-palette2, #2B6CB0);border-right:3px solid var(--global-palette2, #2B6CB0);border-bottom:3px solid var(--global-palette2, #2B6CB0);border-left:3px solid var(--global-palette2, #2B6CB0);}.kb-table-of-content-nav.kb-table-of-content-id83_5f28a6-34 .kb-table-of-contents-title{font-size:28px;}}<\/style>\n\n\n<h2 class=\"wp-block-heading\">The importance of analyzing movie rating data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Movie rating data isn't just about what you think of a movie after you've seen it - the movie industry uses it to analyze the market, predict which movies will be more popular, which genres will be loved, and more. These predictions can also help shape marketing strategies.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this R data analysis example, you'll learn how to use movie rating data from MovieLens to analyze the distribution of movie ratings, the average rating per genre, and the most popular movies. Along the way, you'll understand the entire analysis flow from data load to visualization.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. load and explore data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To analyze movie rating data, we first need to import the data, which we'll do using the <strong><a href=\"https:\/\/www.kaggle.com\/datasets\/abhikjha\/movielens-100k\/data\" target=\"_blank\" rel=\"noreferrer noopener\">MovieLens Dataset<\/a><\/strong>which contains data from millions of movie ratings, making it a good example of real-world data analysis.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Loading data from R<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>Importing the # MovieLens dataset\ninstall.packages(\"data.table\")\nlibrary(data.table)\n\n# Load data into the # local file path\nratings &lt;- fread(&quot;C:\/Users\/user\/Documents\/Project_R\/data\/ml-latest-small\/ratings.csv&quot;)\n\nExplore the # data\nstr(ratings)\nsummary(ratings)\nhead(ratings)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">After importing MovieLens data into R with the code above, you can see the structure of the data and the underlying statistics. The data is stored in <code>userId<\/code>, <code>movieId<\/code>, <code>rating<\/code>, <code>timestamp<\/code> and so on, and contains tens of millions of records in total.<\/p>\n\n\n<style>.kb-image2535_5c1626-5a .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image2535_5c1626-5a img.kb-img, .kb-image2535_5c1626-5a .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2535_5c1626-5a\"><figure class=\"aligncenter size-medium_large\"><img decoding=\"async\" width=\"768\" height=\"349\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-12-768x349.jpg\" alt=\"R \ub370\uc774\ud130\ubd84\uc11d \uc608\uc81c - \uc601\ud654 \ud3c4\uc810 \ub370\uc774\ud130 \ubd84\uc11d - \ub370\uc774\ud130 \ud0d0\uc0c9 \" class=\"kb-img wp-image-2536\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-12-768x349.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-12-300x136.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-12-600x273.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-12.jpg 915w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Data structure descriptions<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>userId<\/code>: User ID<\/li>\n\n\n\n<li><code>movieId<\/code>: Movie ID<\/li>\n\n\n\n<li><code>rating<\/code>: User Rating (1-5)<\/li>\n\n\n\n<li><code>timestamp<\/code>: Rated time<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">By exploring your data, you can learn which movies each user has rated and how they rated them, and you can build a movie recommendation system based on this information.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. visualize the distribution of movie ratings<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Now let's visualize the distribution of movie rating data. Rating data usually ranges from 1 to 5, and seeing which scores were given the most can provide interesting insights.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Visualize Rating Distributions in R<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Install and load ggplot2\ninstall.packages(\"ggplot2\")\nlibrary(ggplot2)\n\nVisualize the # rating distribution\nggplot(ratings, aes(x=rating)) +\n  geom_histogram(binwidth=0.5, fill=\"blue\", color=\"black\", alpha=0.7) +\n  labs(title=\"MovieLens rating distribution\", x=\"ratings\", y=\"frequency\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">When you run this code, it outputs a histogram showing the distribution of ratings, as shown below. As you can see from this R data analysis example, the ratings are often clustered in certain areas. For example, high ratings such as 4 or 5 are often given, and you can visually see that there are a lot of positive reviews.<\/p>\n\n\n<style>.kb-image2535_5d0009-76 .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image2535_5d0009-76 img.kb-img, .kb-image2535_5d0009-76 .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2535_5d0009-76\"><figure class=\"aligncenter size-medium_large\"><img decoding=\"async\" width=\"768\" height=\"541\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-13-768x541.jpg\" alt=\"R \ub370\uc774\ud130\ubd84\uc11d \uc608\uc81c - \uc601\ud654 \ud3c4\uc810 \ub370\uc774\ud130 \ubd84\uc11d - \ud3c9\uc810 \ubd84\ud3ec \ud788\uc2a4\ud1a0\uadf8\ub7a8\" class=\"kb-img wp-image-2538\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-13-768x541.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-13-300x211.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-13-600x423.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-13.jpg 1200w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Analyze average ratings by genre<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we'll analyze how the ratings vary based on the genre of the movie. By analyzing how the ratings vary by genre, we can see if certain genres are well-received by more people. To do this, we'll combine the movie's genre information with the rating data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Load the # movies dataset\nmovies &lt;- fread(&quot;C:\/Users\/user\/Documents\/Project_R\/data\/ml-latest-small\/movies.csv&quot;)\n\nCombine # movie data with ratings data\nmovie_ratings &lt;- merge(ratings, movies, by=&quot;movieId&quot;)\n\nCalculate the average rating per # genre\ngenre_avg_ratings &lt;- movie_ratings[, .(mean_rating=mean(rating)), by=genres]\n\nVisualize the top 20 genres on #\ntop_genres &lt;- genre_avg_ratings[order(-mean_rating)][1:20]\nggplot(top_genres, aes(x=reorder(genres, mean_rating), y=mean_rating)) +\n  geom_col(fill=&quot;green&quot;) +\n  coord_flip()\n  labs(title=&quot;Average rating by genre&quot;, x=&quot;genres&quot;, y=&quot;average rating&quot;)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this data analysis example, you can analyze whether a particular genre receives a high average rating. For example, a documentary or drama genre might have a high average rating, which suggests that there is a large audience that likes that genre.<\/p>\n\n\n<style>.kb-image2535_ec1325-c3 .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image2535_ec1325-c3 img.kb-img, .kb-image2535_ec1325-c3 .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2535_ec1325-c3\"><figure class=\"aligncenter size-medium_large\"><a href=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-14.jpg\" class=\"kb-advanced-image-link\"><img decoding=\"async\" width=\"768\" height=\"541\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-14-768x541.jpg\" alt=\"R \ub370\uc774\ud130\ubd84\uc11d \uc608\uc81c - \uc601\ud654 \ud3c4\uc810 \ub370\uc774\ud130 \ubd84\uc11d - \uc7a5\ub974\ubcc4 \ud3c9\uade0 \ud3c4\uc810\" class=\"kb-img wp-image-2539\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-14-768x541.jpg 768w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-14-300x211.jpg 300w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-14-600x423.jpg 600w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-14.jpg 1200w\" sizes=\"(max-width: 768px) 100vw, 768px\" \/><\/a><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Analyze popular movies<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, let's analyze the most popular movies in the ratings data: movies with high ratings are more likely to be of interest to audiences.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Count the number of ratings per movie\nmovie_popularity &lt;- movie_ratings[, .N, by=movieId][order(-N)][1:10]\n\n# Output popular movie titles and number of ratings\ntop_movies &lt;- merge(movie_popularity, movies, by=&quot;movieId&quot;)\nprint(top_movies[, .(title, N)])<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code will show you the top 10 most reviewed movies. This lets you know which movies resonated with audiences, which can be important information for planning your marketing strategy (my favorite movie, Braveheart, is #1!).<\/p>\n\n\n<style>.kb-image2535_9867e5-26 .kb-image-has-overlay:after{opacity:0.3;border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;}.kb-image2535_9867e5-26 img.kb-img, .kb-image2535_9867e5-26 .kb-img img{border-top-left-radius:5px;border-top-right-radius:5px;border-bottom-right-radius:5px;border-bottom-left-radius:5px;box-shadow:0px 0px 14px 0px rgba(0, 0, 0, 0.2);}<\/style>\n<div class=\"wp-block-kadence-image kb-image2535_9867e5-26\"><figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"413\" height=\"190\" src=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-15.jpg\" alt=\"R \ub370\uc774\ud130\ubd84\uc11d \uc608\uc81c - \uc601\ud654 \ud3c4\uc810 \ub370\uc774\ud130 \ubd84\uc11d - \uc778\uae30 \uc601\ud654 \uc81c\ubaa9 \ud3c4\uc810\" class=\"kb-img wp-image-2540\" srcset=\"https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-15.jpg 413w, https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/image-15-300x138.jpg 300w\" sizes=\"(max-width: 413px) 100vw, 413px\" \/><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Common mistakes in data analysis and how to fix them<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let's take a look at how to avoid common mistakes when analyzing data, especially in a real-world analysis like this one.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Lack of data preprocessing<\/strong>Before you analyze, be sure to check your data for missing or duplicate values, which can skew your results if they are not removed or handled appropriately.<\/li>\n\n\n\n<li><strong>Choosing the wrong visualization<\/strong>: When visualizing data, it's important to choose the right graph. For example, you should avoid the mistake of using histograms for categorical data.<\/li>\n\n\n\n<li><strong>Filtering excessive data<\/strong>It's important to filter only the data you need, but filtering too much data can cause you to miss important information.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q1: Where can I download MovieLens data?<\/strong><br>A: MovieLens data can be downloaded from the MovieLens official site. Different sizes of datasets are available, and we used the 'ml-latest-small' data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q2: What insights can I get from the ratings data?<\/strong><br>A: Rating data allows you to analyze the popularity of movies, genre preferences, user tastes, and more. This is an important resource for building a movie recommendation system.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Q3: What are the most useful R packages for analyzing ratings data?<\/strong><br>A: <code>ggplot2<\/code>is very useful for visualization, and <code>data.table<\/code>is effective.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Organize<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this post, you learned how to analyze movie ratings data as an example of R data analysis. Ratings data plays a big role in understanding user tastes and making important decisions in the movie industry. Learning how to effectively analyze and visualize this data will help you draw insights in a variety of fields.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take what you've learned and try your hand at other datasets!<\/p>","protected":false},"excerpt":{"rendered":"<p>Have you ever rated or reviewed your favorite movie after watching it?...<\/p>","protected":false},"author":3,"featured_media":2544,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_blocks_custom_css":"","_kad_blocks_head_custom_js":"","_kad_blocks_body_custom_js":"","_kad_blocks_footer_custom_js":"","_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[6],"tags":[242,243],"class_list":["post-2535","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-r-coding","tag-r-","tag-243"],"taxonomy_info":{"category":[{"value":6,"label":"\uc54c(R)"}],"post_tag":[{"value":242,"label":"R \ub370\uc774\ud130\ubd84\uc11d"},{"value":243,"label":"\uc601\ud654 \ud3c9\uc810 \ub370\uc774\ud130"}]},"featured_image_src_large":["https:\/\/secondlife.lol\/wp-content\/uploads\/2024\/09\/R-\ub370\uc774\ud130\ubd84\uc11d-\uc608\uc81c-\ud3ec\uc2a4\ud2b8-\uc378\ub124\uc77c-1-600x600.webp",600,600,true],"author_info":{"display_name":"TERE","author_link":"https:\/\/secondlife.lol\/en\/author\/tere\/"},"comment_info":0,"category_info":[{"term_id":6,"name":"\uc54c(R)","slug":"r-coding","term_group":0,"term_taxonomy_id":6,"taxonomy":"category","description":"","parent":20,"count":61,"filter":"raw","cat_ID":6,"category_count":61,"category_description":"","cat_name":"\uc54c(R)","category_nicename":"r-coding","category_parent":20}],"tag_info":[{"term_id":242,"name":"R \ub370\uc774\ud130\ubd84\uc11d","slug":"r-%eb%8d%b0%ec%9d%b4%ed%84%b0%eb%b6%84%ec%84%9d","term_group":0,"term_taxonomy_id":242,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"},{"term_id":243,"name":"\uc601\ud654 \ud3c9\uc810 \ub370\uc774\ud130","slug":"%ec%98%81%ed%99%94-%ed%8f%89%ec%a0%90-%eb%8d%b0%ec%9d%b4%ed%84%b0","term_group":0,"term_taxonomy_id":243,"taxonomy":"post_tag","description":"","parent":0,"count":1,"filter":"raw"}],"_links":{"self":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2535","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/comments?post=2535"}],"version-history":[{"count":3,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2535\/revisions"}],"predecessor-version":[{"id":2542,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/posts\/2535\/revisions\/2542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media\/2544"}],"wp:attachment":[{"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/media?parent=2535"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/categories?post=2535"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/secondlife.lol\/en\/wp-json\/wp\/v2\/tags?post=2535"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}