# Install required packages if not already installed
if (!require(readxl)) install.packages("readxl", dependencies = TRUE)
## Loading required package: readxl
if (!require(dplyr)) install.packages("dplyr", dependencies = TRUE)
## Loading required package: 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
# Load libraries
library(readxl)
library(dplyr)

# Read in the Excel file
data <- read_excel("regression analysis survey.xlsx", sheet = "MKTG 4000 Survey - Electric Veh")

# Clean and select relevant variables
data_clean <- data %>%
  select(likely_recommend_Tesla_1_10,
         brand_recognition,
         Tesla_brand_reputation,
         gasoline_or_EVs_lower_costs,
         willing_pay_lower_environmental_impact,
         agree_disagree_EV_positive_impact_environment,
         Importance_Price,
         Importance_Interior_Quality) %>%
  na.omit()

# Run linear regression
model <- lm(likely_recommend_Tesla_1_10 ~ brand_recognition +
              Tesla_brand_reputation +
              gasoline_or_EVs_lower_costs +
              willing_pay_lower_environmental_impact +
              agree_disagree_EV_positive_impact_environment +
              Importance_Price +
              Importance_Interior_Quality,
            data = data_clean)

# Display summary of regression
summary(model)
## 
## Call:
## lm(formula = likely_recommend_Tesla_1_10 ~ brand_recognition + 
##     Tesla_brand_reputation + gasoline_or_EVs_lower_costs + willing_pay_lower_environmental_impact + 
##     agree_disagree_EV_positive_impact_environment + Importance_Price + 
##     Importance_Interior_Quality, data = data_clean)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.1948 -1.0086  0.1072  1.1661  2.9076 
## 
## Coefficients:
##                                               Estimate Std. Error t value
## (Intercept)                                    7.40869    3.20840   2.309
## brand_recognition                             -0.10606    0.65073  -0.163
## Tesla_brand_reputation                         1.57818    0.55204   2.859
## gasoline_or_EVs_lower_costs                   -1.40387    0.49447  -2.839
## willing_pay_lower_environmental_impact        -0.09405    0.79109  -0.119
## agree_disagree_EV_positive_impact_environment  0.32727    0.63143   0.518
## Importance_Price                              -1.16993    1.22325  -0.956
## Importance_Interior_Quality                   -0.67271    0.91085  -0.739
##                                               Pr(>|t|)  
## (Intercept)                                     0.0338 *
## brand_recognition                               0.8724  
## Tesla_brand_reputation                          0.0109 *
## gasoline_or_EVs_lower_costs                     0.0113 *
## willing_pay_lower_environmental_impact          0.9068  
## agree_disagree_EV_positive_impact_environment   0.6109  
## Importance_Price                                0.3523  
## Importance_Interior_Quality                     0.4703  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.002 on 17 degrees of freedom
## Multiple R-squared:  0.6397, Adjusted R-squared:  0.4913 
## F-statistic: 4.312 on 7 and 17 DF,  p-value: 0.006518