Bright Smile Corporation provides dental services to their clients. They have been in business for a number of years, but recently they have experienced competition from other dental service providers who provide mobile dental services to their clients. Mobile services are all about providing basic dental services at clients’ homes. Bright Smile has a dilemma, as they are unsure if the mobile service is a good inversion and/or expansion.
Use the dataset on the “Bright Smile Corporation” tab of the datasheet to complete the following: 1. Show a graph (scatter) with linear progression. 2. Analyze the data and add a trendline. 3. Display the equation on the chart (data correlation). 4. Use TREND() or FORECAST() to predict the next two price points ($200 and $50). 5. Add both new values to the scatter linear graph.
# Load the mechanic database
bright_smile_data <- read.csv("C://Users//Gurvinder//Desktop//Data Analytics//Assignments//Predictive Analytics//Linear Regression//Task 2//Dentist Visits.csv")
# Perform linear regression for Income vs Price
model_income_price <- lm(Price ~ Income, data = bright_smile_data)
# Summary of the model
summary_model_income_price <- summary(model_income_price)
summary_model_income_price$coefficients # Display coefficients
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 52.1780328 7.04008017 7.411568 1.948061e-09
## Income 0.3407244 0.04498308 7.574502 1.106625e-09
library(ggplot2)
# Scatterplot with linear regression line for Income vs Price
ggplot(bright_smile_data, aes(x = Income, y = Price)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Price vs Income - Linear Regression",
x = "Income",
y = "Price")
# New Income values for prediction
new_income <- data.frame(Income = c(200, 50))
# Predict Price for the new Income values using the model
predicted_prices_income <- predict(model_income_price, newdata = new_income)
# Combine new Income values with predicted Prices
new_data_income <- cbind(new_income, Predicted_Price = predicted_prices_income)
# Scatterplot with linear regression line including predicted values
ggplot() +
geom_point(data = bright_smile_data, aes(x = Income, y = Price)) +
geom_smooth(data = bright_smile_data, aes(x = Income, y = Price), method = "lm", se = FALSE) +
geom_text(data = new_data_income, aes(x = Income, y = Predicted_Price, label = round(Predicted_Price, 2)), vjust = -0.5) +
geom_point(data = new_data_income, aes(x = Income, y = Predicted_Price), color = "orange") +
labs(title = "Price vs Income - Linear Regression with Predictions",
x = "Income",
y = "Price")
# Additional information: Display equation
equation <- as.character(signif(summary_model_income_price$coefficients[2, 1], digits = 4)) # Get the coefficient
equation <- paste("Price =", equation, "* Income +", signif(summary_model_income_price$coefficients[1, 1], digits = 4)) # Build the equation
equation # Display the equation
## [1] "Price = 0.3407 * Income + 52.18"
# Extracting required information
multiple_R_income <- summary_model_income_price$r.squared^0.5 # Multiple R
R_squared_income <- summary_model_income_price$r.squared # R-square
adjusted_R_squared_income <- summary_model_income_price$adj.r.squared # Adjusted R-square
standard_error_income <- summary_model_income_price$sigma # Standard error
observations_income <- summary_model_income_price$n # Observations
# Print the obtained information
cat("Multiple R:", multiple_R_income, "\n")
## Multiple R: 0.7414124
cat("R-squared:", R_squared_income, "\n")
## R-squared: 0.5496923
cat("Adjusted R-squared:", adjusted_R_squared_income, "\n")
## Adjusted R-squared: 0.5401113
cat("Standard error:", standard_error_income, "\n")
## Standard error: 18.33851
cat("Observations:", observations_income, "\n")
## Observations: 50 51
# Perform linear regression for Visits vs Price
model_visits_price <- lm(Price ~ Visits, data = bright_smile_data)
# Summary of the model
summary_model_visits_price <- summary(model_visits_price)
summary_model_visits_price$coefficients # Display coefficients
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 41.110485 9.9378598 4.136754 1.444747e-04
## Visits 5.485372 0.8619863 6.363642 7.562991e-08
library(ggplot2)
# Scatterplot with linear regression line for Visits vs Price
ggplot(bright_smile_data, aes(x = Visits, y = Price)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Price vs Visits - Linear Regression",
x = "Visits",
y = "Price")
# New Visits values for prediction
new_visits <- data.frame(Visits = c(20, 30))
# Predict Price for the new Visits values using the model
predicted_prices_visits <- predict(model_visits_price, newdata = new_visits)
# Combine new Visits values with predicted Prices
new_data_visits <- cbind(new_visits, Predicted_Price = predicted_prices_visits)
# Scatterplot with linear regression line including predicted values
ggplot() +
geom_point(data = bright_smile_data, aes(x = Visits, y = Price)) +
geom_smooth(data = bright_smile_data, aes(x = Visits, y = Price), method = "lm", se = FALSE) +
geom_text(data = new_data_visits, aes(x = Visits, y = Predicted_Price, label = round(Predicted_Price, 2)), vjust = -0.5) +
geom_point(data = new_data_visits, aes(x = Visits, y = Predicted_Price), color = "purple") +
labs(title = "Price vs Visits - Linear Regression with Predictions",
x = "Visits",
y = "Price")
# Additional information: Display equation and statistics
equation <- as.character(signif(summary_model_visits_price$coefficients[2, 1], digits = 4)) # Get the coefficient
equation <- paste("Price =", equation, "* Visits +", signif(summary_model_visits_price$coefficients[1, 1], digits = 4)) # Build the equation
equation # Display the equation
## [1] "Price = 5.485 * Visits + 41.11"
# Extracting required information
multiple_R_visits <- summary_model_visits_price$r.squared^0.5 # Multiple R
R_squared_visits <- summary_model_visits_price$r.squared # R-square
adjusted_R_squared_visits <- summary_model_visits_price$adj.r.squared # Adjusted R-square
standard_error_visits <- summary_model_visits_price$sigma # Standard error
observations_visits <- summary_model_visits_price$n # Observations
# Print the obtained information
cat("Multiple R:", multiple_R_visits, "\n")
## Multiple R: 0.6803177
cat("R-squared:", R_squared_visits, "\n")
## R-squared: 0.4628322
cat("Adjusted R-squared:", adjusted_R_squared_visits, "\n")
## Adjusted R-squared: 0.4514031
cat("Standard error:", standard_error_visits, "\n")
## Standard error: 20.02924
cat("Observations:", observations_visits, "\n")
## Observations: 50 51