library(tidyverse) # data manipulation
library(ggplot2) # data visualization
library(GGally) # Pairwise correlation plots
library(car) # Variance Inflation Factor (VIF) analysis
library(lmtest) # Hypothesis testing
library(sandwich) # Robust standard errors
library(readr) # Reading CSV files
head(movies)
# Check dataset structure
str(movies)
# Summary statistics
summary(movies)
# Check for missing values
sum([Link](movies))
# Check for missing values
sum([Link](movies))
# Histogram of Box Office Revenue
ggplot(movies, aes(x = BOX)) +
geom_histogram(bins = 20, fill = "blue", alpha = 0.7) +
labs(title = "Distribution of Box Office Revenue", x = "Box Office Revenue ($ Mil)", y =
"Frequency")
library(gridExtra) # Needed for [Link]()
# Create boxplots
boxplot1 <- ggplot(movies, aes(y = BOX)) +
geom_boxplot(fill = "blue", alpha = 0.6) +
labs(title = "Box Office Revenue", y = "Box Office Revenue ($ Mil)")
boxplot2 <- ggplot(movies, aes(y = BUDGET)) +
geom_boxplot(fill = "red", alpha = 0.6) +
labs(title = "Production Budget", y = "Production Budget ($ Mil)")
boxplot3 <- ggplot(movies, aes(y = STARPOWR)) +
geom_boxplot(fill = "green", alpha = 0.6) +
labs(title = "Star Power", y = "Star Power Score")
# Arrange plots in a single window (1 row, 3 columns)
[Link](boxplot1, boxplot2, boxplot3, nrow = 1)
# Define a function to remove outliers using the IQR method
remove_outliers <- function(df, column) {
Q1 <- quantile(df[[column]], 0.25, [Link] = TRUE) # First quartile (25th percentile)
Q3 <- quantile(df[[column]], 0.75, [Link] = TRUE) # Third quartile (75th percentile)
IQR_value <- Q3 - Q1 # Interquartile Range
# Define lower and upper bounds
lower_bound <- Q1 - 1.5 * IQR_value
upper_bound <- Q3 + 1.5 * IQR_value
# Filter out outliers
df <- df[df[[column]] >= lower_bound & df[[column]] <= upper_bound, ]
return(df)
}
# Apply the function to remove outliers from relevant columns
movies_cleaned <- movies # Make a copy of the dataset
movies_cleaned <- remove_outliers(movies_cleaned, "BOX")
movies_cleaned <- remove_outliers(movies_cleaned, "BUDGET")
movies_cleaned <- remove_outliers(movies_cleaned, "STARPOWR")
# Check the updated dataset structure
summary(movies_cleaned)
# Compare original vs. cleaned dataset sizes
cat("Original dataset size:", nrow(movies), "\n")
cat("Cleaned dataset size:", nrow(movies_cleaned), "\n")
# Re-plot boxplots to verify outlier removal
par(mfrow = c(1, 3))
boxplot(movies_cleaned$BOX, main = "Box Office (Cleaned)", col = "blue")
boxplot(movies_cleaned$BUDGET, main = "Budget (Cleaned)", col = "red")
boxplot(movies_cleaned$STARPOWR, main = "Star Power (Cleaned)", col = "green")
# Boxplot before cleaning
par(mfrow = c(2,2)) # Arrange plots in 2x2 layout
boxplot(movies$BOX, main = "BOX Before Cleaning", col = "red")
# Histogram before cleaning
hist(movies$BOX, main = "Histogram of BOX Before Cleaning", col = "red", breaks = 30,
xlab = "movies$BOX")
# Boxplot after cleaning
boxplot(movies_cleaned$BOX, main = "BOX After Cleaning", col = "green")
# Histogram after cleaning
hist(movies_cleaned$BOX, main = "Histogram of BOX After Cleaning", col = "green", breaks
= 30, xlab = "movies_cleaned$BOX")
# Reset layout
par(mfrow = c(1,1))
#Scatter plot anf Histogram
ggplot(data = [Link](movies_cleaned), aes(x = BOX)) +
geom_histogram(fill = "blue", bins = 30, alpha = 0.7) +
labs(title = "Distribution of Box Office Revenue After Cleaning",
x = "Box Office Revenue ($ Mil)", y = "Frequency") +
theme_minimal()
ggplot(movies_cleaned, aes(x = BUDGET, y = BOX)) +
geom_point() + # Scatter points
geom_smooth(method = "lm", col = "red", fill = "gray", se = TRUE) + # Linear regression
line with confidence interval
labs(title = "Budget vs Box Office Revenue",
x = "BUDGET",
y = "BOX") +
theme_minimal()
ggplot(movies, aes(x = STARPOWR, y = BOX)) +
geom_point(color = "blue") + # Scatter points in blue
geom_smooth(method = "lm", col = "red", fill = "gray", se = TRUE) + # Linear regression
line with confidence interval
labs(title = "Star Power vs Box Office Revenue",
x = "STARPOWR",
y = "BOX") +
theme_minimal()
movies <- movies %>%
mutate(log_BOX = log(BOX), log_BUDGET = log(BUDGET), log_STARPOWR =
log(STARPOWR + 1))
movies$log_BUDGET <- log(movies$BUDGET)
movies$log_BOX <- log(movies$BOX)
# Scatterplot with regression line
ggplot(movies, aes(x = log_BUDGET, y = log_BOX)) +
geom_point(color = "purple") + # Scatter points in purple
geom_smooth(method = "lm", col = "black", fill = "gray", se = TRUE) + # Linear regression
with confidence interval
labs(title = "Log Budget vs Log Box Office Revenue",
x = "log_BUDGET",
y = "log_BOX") +
theme_minimal()
# Pairwise correlations between key numerical variables
ggpairs(movies_cleaned[, c("BOX", "BUDGET", "STARPOWR", "ADDICT", "CMNGSOON",
"FANDANGO", "CNTWAIT3")])
model1 <- lm(BOX ~ BUDGET, data = movies)
summary(model1)
model2 <- lm(BOX ~ BUDGET + STARPOWR + SEQUEL, data = movies)
summary(model2)
model3 <- lm(BOX ~ BUDGET + STARPOWR + SEQUEL + ACTION + COMEDY +
ANIMATED + HORROR + ADDICT + CMNGSOON + FANDANGO, data = movies)
summary(model3)
model4 <- lm(BOX ~ BUDGET * STARPOWR + SEQUEL + ACTION + COMEDY +
ANIMATED + HORROR + ADDICT + CMNGSOON + FANDANGO + CNTWAIT3, data =
movies)
summary(model4)
model5 <- lm(log_BOX ~ log_BUDGET + log_STARPOWR + SEQUEL + ACTION +
COMEDY + ANIMATED + HORROR + ADDICT + CMNGSOON + FANDANGO, data =
movies)
summary(model5)
bptest(model1) # Breusch-Pagan test
bptest(model2) # Breusch-Pagan test
bptest(model3) # Breusch-Pagan test
bptest(model4) # Breusch-Pagan test
bptest(model5) # Breusch-Pagan test on log-log model
vif(model2)
vif(model3)
vif(model4)
vif(model5)
library(lmtest)
dwtest(model1) # Check for autocorrelation in model1
dwtest(model3) # Check for autocorrelation in model3
dwtest(model5) # Check for autocorrelation in log-log model
par(mfrow = c(2, 2))
plot(model3)
ols_model <- lm(BOX ~ BUDGET + STARPOWR + SEQUEL + ACTION + COMEDY +
ANIMATED + HORROR, data = movies)
summary(ols_model)
# Hausman test for endogeneity
hausman_test <- lm(ols_model$residuals ~ STARPOWR + SEQUEL + ACTION + COMEDY
+ ANIMATED + HORROR, data = movies)
summary(hausman_test)
# If endogeneity is confirmed, perform IV regression (2SLS)
iv_model <- ivreg(BOX ~ BUDGET + SEQUEL | STARPOWR + SEQUEL + ACTION +
COMEDY + ANIMATED + HORROR, data = movies)
summary(iv_model)
first_stage <- lm(BUDGET ~ STARPOWR + SEQUEL + ACTION + COMEDY + ANIMATED
+ HORROR, data = movies)
summary(first_stage)