Preparing the data and loading packages

# Load necessary libraries
library(ggplot2)
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
library(tidyr)
library(purrr)
library(broom)

# Load your data
data <- read.csv("C:/Users/daniv/Downloads/Taxi.csv")

Boxplot income and overtreatment and data cleaning

# Boxplot to visualize relationship
ggplot(data, aes(x = income, y = overtreatment)) +
  geom_boxplot() +
  labs(title = "Relationship between Overtreatment and Alleged Income",
       x = "Alleged Income",
       y = "Overtreatment Index")

# Check for missing values
summary(data)
##      triple          origin             income            rushhour        
##  Min.   :  1.00   Length:348         Length:348         Length:348        
##  1st Qu.: 29.75   Class :character   Class :character   Class :character  
##  Median : 58.50   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 58.50                                                           
##  3rd Qu.: 87.25                                                           
##  Max.   :116.00                                                           
##    dgender               dage       overtreatment    overcharge       
##  Length:348         Min.   :23.00   Min.   :1.000   Length:348        
##  Class :character   1st Qu.:40.00   1st Qu.:1.000   Class :character  
##  Mode  :character   Median :48.50   Median :1.012   Mode  :character  
##                     Mean   :46.94   Mean   :1.064                     
##                     3rd Qu.:55.00   3rd Qu.:1.048                     
##                     Max.   :75.00   Max.   :1.991
# Check data types
str(data)
## 'data.frame':    348 obs. of  8 variables:
##  $ triple       : int  1 1 1 2 2 2 3 3 3 4 ...
##  $ origin       : chr  "resident" "nonresident" "foreign" "resident" ...
##  $ income       : chr  "high" "low" "high" "high" ...
##  $ rushhour     : chr  "yes" "yes" "yes" "no" ...
##  $ dgender      : chr  "male" "male" "male" "male" ...
##  $ dage         : int  45 45 50 50 50 60 50 55 65 45 ...
##  $ overtreatment: num  1.11 1 1.09 1.16 1.13 ...
##  $ overcharge   : chr  "no" "no" "no" "no" ...
# Data cleaning
data <- na.omit(data)  # Remove rows with missing values
data$income <- as.factor(data$income)  # Convert to factor if not already

# Perform t-test
t_test_income <- t.test(overtreatment ~ income, data = data)
tidy(t_test_income)
## # A tibble: 1 × 10
##   estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high
##      <dbl>     <dbl>     <dbl>     <dbl>   <dbl>     <dbl>    <dbl>     <dbl>
## 1   0.0182      1.07      1.06      1.37   0.172      346. -0.00797    0.0444
## # ℹ 2 more variables: method <chr>, alternative <chr>

Overtreatment and Alleged Income: Visual inspection shows a potential relationship between the overtreatment index and alleged income. We can see that most values both for high and low income are between 1.25 and 1.50 The t-test or ANOVA indicates whether this relationship is statistically significant. If p-value is significant, it suggests a link between overtreatment and alleged income.

Knowing these, the estimated difference in means between the two groups (estimate = 0.0182) is not statistically significant, as indicated by the p-value of 0.172. This suggests that there is insufficient evidence to reject the null hypothesis. The 95% confidence interval for the difference in means (-0.00797 to 0.0444) includes zero, reinforcing the lack of a significant effect. Therefore, we do not have compelling evidence to conclude that there is a meaningful difference between the two groups. The alternative hypothesis of a non-zero difference in means is not supported by the data.

Boxplot origin and overtreatment and Anova test

# Boxplot to visualize relationship
ggplot(data, aes(x = origin, y = overtreatment)) +
  geom_boxplot() +
  labs(title = "Relationship between Overtreatment and Alleged Origin",
       x = "Alleged Origin",
       y = "Overtreatment Index")

# ANOVA test to test for a statistically significant link
anova_origin <- aov(overtreatment ~ origin, data = data)
summary(anova_origin)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## origin        2  0.226 0.11284   7.584 0.000598 ***
## Residuals   345  5.133 0.01488                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Overtreatment and Alleged Origin: Visual inspection shows a potential relationship between the overtreatment index and alleged origin.The graph indicates that foreign origin has a higher overtreatment index, followed by non residents. The ANOVA test indicates whether this relationship is statistically significant. If p-value is significant, it suggests a link between overtreatment and alleged origin.

Taking this into account the analysis of variance (ANOVA) results indicates a statistically significant effect of the variable “origin” on the response variable. The F value of 7.584 with associated p-value of 0.000598 is below the conventional significance level of 0.05. Therefore, we reject the null hypothesis that the means of the groups are equal.In other words, there is evidence to suggest that the variable “origin” has a significant impact on the response variable. The differences in means among the groups are not likely due to random chance alone. The post-hoc tests or pairwise comparisons can be performed to identify which specific group(s) differ from each other. The residual sum of squares (Sum Sq) and mean squared error (Mean Sq) represent the variability within the groups and provide context for the magnitude of the effect. The majority of the variability in the response variable is explained by the variable “origin,” as evidenced by the larger Sum Sq for “origin” compared to the residual Sum Sq. In summary, the variable “origin” significantly influences the response variable, and further investigation or post-hoc tests may be conducted to explore specific group differences.

Boxplot income and overcharge and chi square test

# Barplot to visualize relationship
ggplot(data, aes(x = income, fill = overcharge)) +
  geom_bar(position = "fill") +
  labs(title = "Relationship between Overcharge and Alleged Income",
       x = "Alleged Income",
       y = "Proportion of Overcharge")

# Chi-square test to test for a statistically significant link
chi_square_income <- chisq.test(table(data$income, data$overcharge))
chi_square_income
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  table(data$income, data$overcharge)
## X-squared = 0, df = 1, p-value = 1

Overcharge and Alleged Income: Visual inspection shows a potential relationship between overcharge and alleged income.We can see that for both high and low incomes the overcharge tends to be around 0.125 The chi-square test indicates whether this relationship is statistically significant. If p-value is significant, it suggests a link between overcharge and alleged income.

The chi-squared test was conducted to examine the association between the variables “income” and “overcharge.” The test statistic, X-squared, is 0 with 1 degree of freedom, resulting in a p-value of 1. With a p-value of 1, there is no evidence to reject the null hypothesis that there is no association between “income” and “overcharge.” The test does not provide sufficient evidence to suggest a statistically significant relationship between these two variables. Therefore, we fail to reject the null hypothesis, and it can be concluded that, based on this analysis, there is no significant association between the alleged income level and the occurrence of overcharging.

Barplot and chi square test

# Barplot to visualize relationship
ggplot(data, aes(x = origin, fill = overcharge)) +
  geom_bar(position = "fill") +
  labs(title = "Relationship between Overcharge and Alleged Origin",
       x = "Alleged Origin",
       y = "Proportion of Overcharge")

# Chi-square test to test for a statistically significant link
chi_square_origin <- chisq.test(table(data$origin, data$overcharge))
chi_square_origin
## 
##  Pearson's Chi-squared test
## 
## data:  table(data$origin, data$overcharge)
## X-squared = 23.044, df = 2, p-value = 9.909e-06

Overcharge and Alleged Origin: Visual inspection shows a potential relationship between overcharge and alleged origin. Foreign has a little bit higher proportion of overcharge with almost 0.25. It is followed by non residents. The chi-square test indicates whether this relationship is statistically significant. If p-value is significant, it suggests a link between overcharge and alleged origin.

The chi-squared test was conducted to assess the association between the variables “origin” and “overcharge.” The test yielded a test statistic (X-squared) of 23.044 with 2 degrees of freedom, resulting in an extremely low p-value of 9.909e-06. With such a low p-value, there is strong evidence to reject the null hypothesis that there is no association between “origin” and “overcharge.” The test suggests a statistically significant relationship between these two variables. Therefore, based on this analysis, it can be concluded that the alleged origin of the passenger is significantly associated with the occurrence of overcharging by taxi drivers.