options(repos = "https://cran.rstudio.com/")

FIRST RESEARCH QUESTION: is there significant difference in podcast listening time between genders?

h0: Males spend the same time listening to podcasts as do women and others together.

source: AJAYAKUMAR, M. (2023).Spotify User Behavior Dataset: A Comprehensive Spotify Dataset for User Analysis. https://www.kaggle.com/datasets/meeraajayakumar/spotify-user-behavior-dataset

i included the following variables:
Age
Gender
Pod_lis_frequency (=podcast listenning frequency)
Preffered_pod_format (preffered podcast format)

#function to read excel files
library(readxl)
#importing data we want to analyze
podcast <- read_excel("~/Desktop/r program/podcast.xlsx")
print(podcast)
## # A tibble: 520 × 4
##    Age   Gender pod_lis_frequency    preffered_pod_format
##    <chr> <chr>  <chr>                <chr>               
##  1 20-35 Female Daily                Interview           
##  2 12-20 Male   Several times a week Interview           
##  3 35-60 Others Once a week          Interview           
##  4 20-35 Female Never                None                
##  5 20-35 Female Rarely               Story telling       
##  6 20-35 Male   Never                None                
##  7 20-35 Female Never                None                
##  8 20-35 Female Several times a week Conversational      
##  9 20-35 Female Rarely               Story telling       
## 10 20-35 Female Never                None                
## # ℹ 510 more rows
install.packages("dplyr")
## 
## The downloaded binary packages are in
##  /var/folders/r0/xxd_08qx47b8qgmwf_xccjb00000gn/T//RtmpauVNGd/downloaded_packages
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
#we changed male, female, and others variables to numeric variables and combined them in two categories
podcast <- podcast %>%
  mutate(gender_code = recode(Gender, "Male" = 0, "Female" = 1, "Others" = 1))
#we changed listening frequencies to numeric values
podcast <- podcast %>%
  mutate(listening_code = recode(pod_lis_frequency,
                                    "Never" = 0,
                                    "Rarely" = 1,
                                    "Several times a week" = 2,
                                    "Once a week" = 3,
                                    "Daily" = 4))
print(podcast)
## # A tibble: 520 × 6
##    Age   Gender pod_lis_frequency    preffered_pod_format gender_code
##    <chr> <chr>  <chr>                <chr>                      <dbl>
##  1 20-35 Female Daily                Interview                      1
##  2 12-20 Male   Several times a week Interview                      0
##  3 35-60 Others Once a week          Interview                      1
##  4 20-35 Female Never                None                           1
##  5 20-35 Female Rarely               Story telling                  1
##  6 20-35 Male   Never                None                           0
##  7 20-35 Female Never                None                           1
##  8 20-35 Female Several times a week Conversational                 1
##  9 20-35 Female Rarely               Story telling                  1
## 10 20-35 Female Never                None                           1
## # ℹ 510 more rows
## # ℹ 1 more variable: listening_code <dbl>
#descriptive statistics
summary(podcast)
##      Age               Gender          pod_lis_frequency  preffered_pod_format
##  Length:520         Length:520         Length:520         Length:520          
##  Class :character   Class :character   Class :character   Class :character    
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character    
##                                                                               
##                                                                               
##                                                                               
##   gender_code     listening_code 
##  Min.   :0.0000   Min.   :0.000  
##  1st Qu.:1.0000   1st Qu.:0.750  
##  Median :1.0000   Median :1.000  
##  Mean   :0.7808   Mean   :1.365  
##  3rd Qu.:1.0000   3rd Qu.:2.000  
##  Max.   :1.0000   Max.   :4.000
#chi square test
result <- chisq.test(podcast$gender_code, podcast$listening_code)
## Warning in chisq.test(podcast$gender_code, podcast$listening_code): Chi-squared
## approximation may be incorrect
print(result)
## 
##  Pearson's Chi-squared test
## 
## data:  podcast$gender_code and podcast$listening_code
## X-squared = 22.976, df = 4, p-value = 0.000128
library(dplyr)
Male <- podcast %>%
  filter(Gender == "Male") %>%
  select(listening_code)
Female_Others <- podcast %>%
  filter(Gender %in% c("Female", "Others")) %>%
  select(listening_code)
result_mannwhitney <- wilcox.test(Male$listening_code, Female_Others$listening_code)
print(result_mannwhitney)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  Male$listening_code and Female_Others$listening_code
## W = 17893, p-value = 0.0001113
## alternative hypothesis: true location shift is not equal to 0

A p-value of 0.0001113 obtained from the Mann-Whitney U test suggests strong evidence against the null hypothesis.this small p-value indicates that there is a significant difference in podcast listening times between these gender categories. based on this results we reject the null hypothesis.

based on the results the non-parametric test Wilcoxon rank sum test gave more reliable and better results than Chi-square. however both tests show a significant association between gender and podcast listening behaviors.

SECOND RESEARCH QUESTION: Can duration, number of daily guests and total guests, beer price, roasted chicken price and consumption determine beer consumption?

Source: Franceschini, L. (2023). Oktoberfest. https://www.kaggle.com/datasets/lucafrance/oktoberfest

variables that will be used in this case are the following: year (the data spans from 1985 until 2022)
guests total (millions)
guests daily (thousands)
beer price (1 litre)
beer consumption (hectolitres)
roasted chicken price
roasted chicken consumption

#this function enables r to read excel files
library(readxl)
#specific location of the file
oktoberfest <- read_excel("~/Desktop/r program/oktoberfest.xlsx")
#visualize all data
print(oktoberfest)
## # A tibble: 36 × 8
##     year duration guests_total guests_daily beer_price beer_consumption
##    <dbl>    <dbl>        <dbl>        <dbl>      <dbl>            <dbl>
##  1  1985       16          7.1          444       3.2             54541
##  2  1986       16          6.7          419       3.3             53807
##  3  1987       16          6.5          406       3.37            51842
##  4  1988       16          5.7          356       3.45            50951
##  5  1989       16          6.2          388       3.6             51241
##  6  1990       16          6.7          419       3.77            54300
##  7  1991       16          6.4          400       4.21            54686
##  8  1992       16          5.9          369       4.42            48888
##  9  1993       16          6.5          406       4.71            51933
## 10  1994       16          6.6          413       4.89            52108
## # ℹ 26 more rows
## # ℹ 2 more variables: roast_chicken_price <dbl>,
## #   roast_chicken_consumption <dbl>
#descriptive statistics
summary(oktoberfest)
##       year         duration      guests_total    guests_daily  
##  Min.   :1985   Min.   :16.00   Min.   :5.500   Min.   :329.0  
##  1st Qu.:1994   1st Qu.:16.00   1st Qu.:5.975   1st Qu.:369.0  
##  Median :2002   Median :16.00   Median :6.350   Median :394.0  
##  Mean   :2003   Mean   :16.31   Mean   :6.297   Mean   :386.7  
##  3rd Qu.:2011   3rd Qu.:16.00   3rd Qu.:6.500   3rd Qu.:406.0  
##  Max.   :2022   Max.   :18.00   Max.   :7.100   Max.   :444.0  
##    beer_price     beer_consumption roast_chicken_price
##  Min.   : 3.200   Min.   :48698    Min.   : 3.920     
##  1st Qu.: 4.845   1st Qu.:53511    1st Qu.: 5.317     
##  Median : 6.750   Median :61468    Median : 8.130     
##  Mean   : 7.053   Mean   :62477    Mean   : 7.769     
##  3rd Qu.: 9.170   3rd Qu.:71085    3rd Qu.: 9.890     
##  Max.   :13.450   Max.   :79225    Max.   :13.960     
##  roast_chicken_consumption
##  Min.   :313636           
##  1st Qu.:481139           
##  Median :515646           
##  Mean   :564746           
##  3rd Qu.:685466           
##  Max.   :807710
#selected variables for our research question
selected_variables <- oktoberfest %>%
  select(beer_consumption, duration, guests_daily, guests_total, beer_price, 
         roast_chicken_price, roast_chicken_consumption)
#this part shows correlation between the selected variables
correlation_matrix <- cor(selected_variables)
print(correlation_matrix)
##                           beer_consumption    duration guests_daily
## beer_consumption                1.00000000  0.37892134   -0.2602698
## duration                        0.37892134  1.00000000   -0.4549282
## guests_daily                   -0.26026979 -0.45492821    1.0000000
## guests_total                   -0.07011065  0.07247667    0.8547722
## beer_price                      0.88586183  0.34272165   -0.5037429
## roast_chicken_price             0.88113556  0.37365729   -0.5341261
## roast_chicken_consumption      -0.63046013 -0.26178850    0.5672097
##                           guests_total beer_price roast_chicken_price
## beer_consumption           -0.07011065  0.8858618           0.8811356
## duration                    0.07247667  0.3427217           0.3736573
## guests_daily                0.85477218 -0.5037429          -0.5341261
## guests_total                1.00000000 -0.3673196          -0.3823092
## beer_price                 -0.36731963  1.0000000           0.9852393
## roast_chicken_price        -0.38230916  0.9852393           1.0000000
## roast_chicken_consumption   0.48626128 -0.8327516          -0.8645604
##                           roast_chicken_consumption
## beer_consumption                         -0.6304601
## duration                                 -0.2617885
## guests_daily                              0.5672097
## guests_total                              0.4862613
## beer_price                               -0.8327516
## roast_chicken_price                      -0.8645604
## roast_chicken_consumption                 1.0000000

in this part we see that all values range from 1 to -1. if the value is 0 it means that there is no correlation between variables.
positive values indicate positive correlation between variables, meaning that when one variable increases the other does as well.
negative values suggest negative correlation between variables, meaning that when one variable increases the other decreases. it is clear from the data that Duration, beer price, and roasted chicken price have positive correlation with beer consumption.
daily and total guests negatively correlate with beer consumption indicating that higher daily or total guest number means lower beer consumption.
Roasted chicken consumption is also negatively correlated with beer consumption implying that higher consumption of roasted chicken means lower consumption of beer.
strong correlation is clearly visible among beer price and roasted chicken price with beer consumption.

#multiple linear regression model
beer_consumption <- lm(beer_consumption ~ duration + guests_daily + guests_total + beer_price + 
              roast_chicken_price + roast_chicken_consumption, data = oktoberfest)
#summary of the calculation
summary(beer_consumption)
## 
## Call:
## lm(formula = beer_consumption ~ duration + guests_daily + guests_total + 
##     beer_price + roast_chicken_price + roast_chicken_consumption, 
##     data = oktoberfest)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8998.6 -1199.8   808.1  2254.6  4693.3 
## 
## Coefficients:
##                             Estimate Std. Error t value Pr(>|t|)  
## (Intercept)                2.928e+05  3.060e+05   0.957   0.3466  
## duration                  -1.972e+04  1.872e+04  -1.053   0.3008  
## guests_daily              -8.496e+02  8.199e+02  -1.036   0.3086  
## guests_total               5.877e+04  5.054e+04   1.163   0.2544  
## beer_price                 1.363e+03  1.379e+03   0.988   0.3312  
## roast_chicken_price        3.300e+03  1.543e+03   2.138   0.0411 *
## roast_chicken_consumption  2.554e-02  1.040e-02   2.455   0.0203 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3596 on 29 degrees of freedom
## Multiple R-squared:  0.897,  Adjusted R-squared:  0.8756 
## F-statistic: 42.08 on 6 and 29 DF,  p-value: 5.096e-13
#selected variables for the scatterplot
selected_vars <- oktoberfest[, c("beer_consumption", "duration", "guests_daily", "guests_total", "beer_price", "roast_chicken_price", "roast_chicken_consumption")]
#paired variables for the scatterplot
pairs(selected_vars)

#calculations of residuals and fitted values
residuals <- residuals(beer_consumption)
fitted_values <- fitted(beer_consumption)
#visualization of the residuals and fitted values combined in a plot
plot(fitted_values, residuals,
     xlab = "Fitted Values",
     ylab = "Residuals",
     main = "Residuals vs. Fitted Values Plot")
abline(h = 0, col = "red")

the points show no specific pattern so the linear model is appropriate.

#with this we create a histogram of residuals
hist(residuals, main = "Histogram of Residuals", xlab = "Residuals", ylab = "Frequency")

it is visible that the distribution is skewed, which indicates that the residuals are not symmetrically distributed around zero.

#Shapiro-Wilk test for normality
shapiro.test(residuals)
## 
##  Shapiro-Wilk normality test
## 
## data:  residuals
## W = 0.93819, p-value = 0.04464

Based on the Shapiro-Wilk test at a 0.05 significance level, the data tested does not follow a normal distribution (p-value = 0.04464).

#downloading new packages for a test
install.packages("olsrr")
## 
## The downloaded binary packages are in
##  /var/folders/r0/xxd_08qx47b8qgmwf_xccjb00000gn/T//RtmpauVNGd/downloaded_packages
library(olsrr)
## 
## Attaching package: 'olsrr'
## The following object is masked from 'package:datasets':
## 
##     rivers
#test for heteroskedasticity
ols_test_breusch_pagan(beer_consumption)
## 
##  Breusch Pagan Test for Heteroskedasticity
##  -----------------------------------------
##  Ho: the variance is constant            
##  Ha: the variance is not constant        
## 
##                     Data                     
##  --------------------------------------------
##  Response : beer_consumption 
##  Variables: fitted values of beer_consumption 
## 
##         Test Summary         
##  ----------------------------
##  DF            =    1 
##  Chi2          =    0.9094812 
##  Prob > Chi2   =    0.3402521

With a p-value of 0.3402521, if using a significance level of 0.05, we fail to reject the null hypothesis. therefore, the variance is not constant.

summary(beer_consumption)$r.squared
## [1] 0.8969626
summary(beer_consumption)$adj.r.squared
## [1] 0.8756445

the adjusted r-square of 0.876 indicates that approximately 87.6% of the variability in beer consumption can be attributed to the variables (duration, beer price, total and daily guests, beer consumption, roasted chicken price, and roasted chicken consumption) which were included in the model.