library(readr)
dentalService <- read_csv("C:/Users/Dell/Downloads/dentalService.csv")
## Rows: 49 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): income, visits, Price
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
View(dentalService)

dentalService <- read_csv("C:/Users/Dell/Downloads/dentalService.csv")
## Rows: 49 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): income, visits, Price
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Create a linear model using the lm function
step1 <- lm(income ~ Price, data = dentalService)
step2 <- lm(visits ~ income, data = dentalService)
# display the model results
summary(step1)
## 
## Call:
## lm(formula = income ~ Price, data = dentalService)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -61.270 -24.526  -9.526  22.954  94.048 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -18.765     22.393  -0.838    0.406    
## Price          1.613      0.213   7.575 1.11e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39.9 on 47 degrees of freedom
## Multiple R-squared:  0.5497, Adjusted R-squared:  0.5401 
## F-statistic: 57.37 on 1 and 47 DF,  p-value: 1.107e-09
summary(step2)
## 
## Call:
## lm(formula = visits ~ income, data = dentalService)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -4.2204 -2.2273 -0.0218  1.8977  4.3310 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  5.31980    0.94058   5.656 8.89e-07 ***
## income       0.03938    0.00601   6.553 3.90e-08 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 2.45 on 47 degrees of freedom
## Multiple R-squared:  0.4774, Adjusted R-squared:  0.4663 
## F-statistic: 42.94 on 1 and 47 DF,  p-value: 3.899e-08
# Create a scatterplot of the two variables 

  ggplot(dentalService,aes(income, Price))+
  geom_point()+
  geom_smooth(method = lm, se=FALSE)+ #lm=linear model se=FALSE removes error bars
  stat_poly_eq(formula = x ~ y, parse = TRUE)+ #add R-Squared using the ggpmisc package
  labs(title="Income vs Price - Linear Regression",
       x="income",
       y="Price")
## `geom_smooth()` using formula = 'y ~ x'

  ggplot(dentalService,aes(visits , income))+
  geom_point()+
  geom_smooth(method = lm, se=FALSE)+ #lm=linear model se=FALSE removes error bars
  stat_poly_eq(formula = x ~ y, parse = TRUE)+ #add R-Squared using the ggpmisc package
  labs(title="Visits Vs Income - Linear Regression",
       x="visits",
       y="income")
## `geom_smooth()` using formula = 'y ~ x'

  # Predict the next two price points
new_data <- data.frame(Price = c(200, 50))
predicted_incomes <- predict(step1, newdata = new_data)

# Create a data frame with the new predictions
predictions_df <- data.frame(Price = c(200, 50), Income = predicted_incomes)

# Add new values to the scatter linear graph
ggplot(dentalService, aes(income, Price)) +
  geom_point() +
  geom_smooth(method = lm, se = FALSE) +
  stat_poly_eq(formula = y ~ x, parse = TRUE) +
  geom_point(data = predictions_df, aes(x = Income, y = Price, color = "Predicted")) +
  labs(
    title = "Income vs Price - Linear Regression with Predictions",
    x = "Income",
    y = "Price"
  )
## `geom_smooth()` using formula = 'y ~ x'