install.packages("readxl") # Install the package
library(readxl) # Load the package
data <- read_excel(file.choose()) # Select the file manually to load the data
data <- as.data.frame(data) # Ensure the data is in a data frame format
head(data) # Display the first few rows of the dataset
colnames(data) # Check column names to verify correctness
colnames(data) <- c("Tire", "Wet", "Noise", "Buy_Again", "Purchase") # Rename columns
model <- glm(Purchase ~ Wet + Noise, data = data, family = binomial(link = “logit”))
summary(model)
coefficients <- coef(model)
b0 <- coefficients[1] # Intercept
b1 <- coefficients[2] # Coefficient for Wet
b2 <- coefficients[3] # Coefficient for Noise
cat("b0 (Intercept):", b0, "\n")
cat("b1 (Wet):", b1, "\n")
cat("b2 (Noise):", b2, "\n")
summary(model)
p_values <- summary(model)$coefficients[, 4] # Extract the p-values from the model summary
significant <- p_values < 0.05 # Check if p-values are less than alpha = 0.05
names(p_values) <- c("Intercept", "Wet", "Noise") # Add labels for each p-value
cat("P-values:\n")
print(p_values)
cat("\nSignificance (alpha = 0.05):\n")
print(significant)
if (significant["Wet"] && significant["Noise"]) {
cat("\nBoth independent variables (Wet and Noise) are significant.\n")
} else {
cat("\nNot all independent variables are significant.\n")
}
null_deviance <- model$null.deviance # Null Deviance: Model with only the intercept
residual_deviance <- model$deviance # Residual Deviance: Model with the predictors (Wet and Noise)
mcfadden_r2 <- 1 - (residual_deviance / null_deviance)
cat("McFadden R-Squared:", round(mcfadden_r2, 2), "\n")
wet <- 8 # Wet performance rating
noise <- 8 # Noise performance rating
b0 <- coefficients[1] # Intercept
b1 <- coefficients[2] # Coefficient for Wet
b2 <- coefficients[3] # Coefficient for Noise
logit <- b0 + b1 * wet + b2 * noise # Logit formula
probability <- exp(logit) / (1 + exp(logit)) # Logit to probability formula
percentage <- round(probability * 100, 2) # Convert to percentage
cat("Estimated Probability (%):", percentage, "\n")
wet <- 7 # Wet performance rating
noise <- 7 # Noise performance rating
logit <- b0 + b1 * wet + b2 * noise # Logit formula
probability <- exp(logit) / (1 + exp(logit)) # Logit to probability formula
percentage <- round(probability * 100, 2) # Convert to percentage
cat("Estimated Probability (%):", percentage, "\n")