hw 3

2024-02-04

Dataset ‘movies’

data(movies)
head(movies)
# A tibble: 6 × 24
  title      year length budget rating votes    r1    r2    r3    r4    r5    r6
  <chr>     <int>  <int>  <int>  <dbl> <int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 $          1971    121     NA    6.4   348   4.5   4.5   4.5   4.5  14.5  24.5
2 $1000 a …  1939     71     NA    6      20   0    14.5   4.5  24.5  14.5  14.5
3 $21 a Da…  1941      7     NA    8.2     5   0     0     0     0     0    24.5
4 $40,000    1996     70     NA    8.2     6  14.5   0     0     0     0     0  
5 $50,000 …  1975     71     NA    3.4    17  24.5   4.5   0    14.5  14.5   4.5
6 $pent      2000     91     NA    4.3    45   4.5   4.5   4.5  14.5  14.5  14.5
# ℹ 12 more variables: r7 <dbl>, r8 <dbl>, r9 <dbl>, r10 <dbl>, mpaa <chr>,
#   Action <int>, Animation <int>, Comedy <int>, Drama <int>,
#   Documentary <int>, Romance <int>, Short <int>

Dataset ‘movies’; Year vs. Rating

codes for plotly

data(movies) Romance_data <- filter(movies,Romance== 1) mod = lm(rating ~ year, data = Romance_data) x = Romance_data\(year; y = Romance_data\)rating

xax <- list( title = “Year”, titlefont = list(family = “Modern Computer Roman”))

yax <- list( title = “Rating”, titlefont = list(family = “Modern Computer Roman”), range = c(0, 10) )

fig <- plot_ly(x = x, y = y, type = “scatter”, mode = “markers”, name = “data”, width = 800, height = 430 ) %>% add_lines(x=x, y=fitted(mod), name = “fitted”) %>% layout(xaxis = xax, yaxis = yax)

config( fig ) ###

Dataset ‘movies’

## get the action data 
data(movies)
Action_data <- filter(movies,Action== 1)
## define g
g <- ggplot(data = Action_data, aes(x= Action_data$year, y = Action_data$rating)) + geom_point() 
g
Warning: Use of `Action_data$year` is discouraged.
ℹ Use `year` instead.
Warning: Use of `Action_data$rating` is discouraged.
ℹ Use `rating` instead.

Dataset ‘movies’

## get the action data 
data(movies)
Action_data <- filter(movies,Action== 1)
## define g
g <- ggplot(data = Action_data, aes(x= Action_data$year, y = Action_data$budget)) + geom_point(color = "red") 
g
Warning: Use of `Action_data$year` is discouraged.
ℹ Use `year` instead.
Warning: Use of `Action_data$budget` is discouraged.
ℹ Use `budget` instead.
Warning: Removed 3850 rows containing missing values (`geom_point()`).

Dataset ‘movies’

data(movies)

## get each genre info
Action_data <- filter(movies, Action == 1 & year==2020)
Animation_data <- filter(movies,Animation == 1 & year==2020)
Comedy_data <- filter(movies,Comedy == 1 & year==2020)
Drama_data <- filter(movies,Drama == 1 & year==2020)
Documentary_data <- filter(movies,Documentary == 1 & year==2020)
Romance_data <- filter(movies,Romance == 1 & year==2020)
Short_data <- filter(movies,Short == 1 & year==2020)
## get average ratings
ave_rating_Action <- mean(Action_data$rating, na.rm = T)
ave_rating_Animation <- mean(Animation_data$rating, na.rm = T)
ave_rating_Comedy <- mean(Comedy_data$rating, na.rm = T)
ave_rating_Drama <- mean(Drama_data$rating, na.rm = T)
ave_rating_Documentary <- mean(Documentary_data$rating, na.rm = T)
ave_rating_Romance <- mean(Romance_data$rating, na.rm = T)
ave_rating_Short <- mean(Short_data$rating, na.rm = T)
## build a new data set 
df_2020 <- data.frame(
  class = c("Action", "Animation", "Comedy", "Drama", "Documentary", "Romance", "Short"),
  average_rates = c(ave_rating_Action, ave_rating_Animation, ave_rating_Comedy, ave_rating_Drama, ave_rating_Documentary, ave_rating_Romance, ave_rating_Short)
)
## barplot of genres in ggplot
ggplot(data = df_2020, aes(x=class, y= average_rates, fill= class)) +
  geom_bar(stat = "identity", width = 0.7, fill= "steelblue")+
  theme_minimal()
Warning: Removed 7 rows containing missing values (`position_stack()`).

Dataset “movies”; Average Ratings by Genres: Mean function

model:\(\text{Mean} = (\text{M1} +\text{M2} +\text{...}+\text{Mn})/n\)

Discrete Differentiation

model:\(\text{F'(x)} = (\text{F(x+h)} -\text{F(x-h)}) /2h\)