#MODEL

library(ggcorrplot)

##For the following variables, they all impact sale price.
##  -year_built has a positive coefficient which makes sense, showing that as the year increases(gets newer), the sale price also tends to increase by $449
## -lot_area also has a positive coefficient showing an increase of 95 cents in sale price for every unit increase in area
## -gr_liv_area has a positive coefficient and the sale price increases by around $55 for every unit increase in area
## -total_bsmt_sf has a positive coefficient and as the square footage increase by a unit, the sale price increases by about $33
## -overall_qual_level is a categorical set of data which has a positive correlation also, as the quality level increases by 1 level then the sale price increases by around $20000

model <- lm(sale_price ~ year_built + lot_area + gr_liv_area + total_bsmt_sf + overall_qual_level, data = t_train)

summary(model)
## 
## Call:
## lm(formula = sale_price ~ year_built + lot_area + gr_liv_area + 
##     total_bsmt_sf + overall_qual_level, data = t_train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -182916  -18111   -1378   15219  256017 
## 
## Coefficients:
##                      Estimate Std. Error t value Pr(>|t|)    
## (Intercept)        -9.533e+05  4.723e+04  -20.18   <2e-16 ***
## year_built          4.449e+02  2.492e+01   17.86   <2e-16 ***
## lot_area            9.481e-01  7.930e-02   11.96   <2e-16 ***
## gr_liv_area         5.562e+01  1.538e+00   36.17   <2e-16 ***
## total_bsmt_sf       3.877e+01  1.737e+00   22.32   <2e-16 ***
## overall_qual_level  2.011e+04  6.653e+02   30.23   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 31490 on 2889 degrees of freedom
##   (35 observations deleted due to missingness)
## Multiple R-squared:  0.818,  Adjusted R-squared:  0.8177 
## F-statistic:  2598 on 5 and 2889 DF,  p-value: < 2.2e-16
t_train <- t_train %>% 
  mutate(predict = predict(model, newdata = t_new),
         residuals = sale_price - predict)

error <- sqrt(mean(t_train$residuals ^2 ))

t_numeric <- t_train %>% 
  select(where(is.numeric)) %>% 
  select(sale_price, year_built, lot_area, first_flr_sf, gr_liv_area, total_bsmt_sf, overall_qual_level)
pairs(t_numeric)

##the errors represent the distance between the actual sale price and the predicted sale price, the residuals range from -182916 to 256017 with a median of -1378. This means that there are some outliers in the data but the model tends to over/underestimate the sale price by 1378. 
#PREDICTIONS

hist(model$residuals)

predictions <- predict(model, newdata = t_test)

rmse <- sqrt(mean((t_test$sale_price - predictions)^ 2))
r_squared <- cor(t_test$sale_price, predictions)^2

##My R squared value for the test data set is 0.81421 which is relatively high, this means that roughly 81% of the dependent variable (sale price) is dependent upon the independent variable I chose. 
##My rmse value is 32222 which means that the predicted sale price is off by roughly that much compared to the actual value. Lower rmse values mean that a model is more accurate, but compared to the price that homes are sold for, this value is not too terribly bad. Even though it could be much better, once I got to this point everything I tried changing made this value worse.
#GRAPHS


ggplot(t_test, aes(x = total_area, y = sale_price, color = overall_qual_level)) +
  geom_point(alpha = 0.5) +
  scale_color_gradient(low = "blue", high = "red") +
  labs(title = "Relationship between Total Area, Overall Quality, and Sale Price",
       x = "Total Area",
       y = "Sale Price",
       color = "Overall Quality") +
  theme_minimal()

ggplot(t_test, aes(x = overall_qual_level, y = sale_price, fill = factor(overall_qual_level))) +
  geom_boxplot() +
  labs(title = "Distribution of Sale Price by Overall Quality",
       x = "Overall Quality",
       y = "Sale Price") +
  scale_fill_brewer(palette = "Set3") +
  theme_minimal()