2024-04-07

Introduction to Simple Linear Regression

  • Simple linear regression is a statistical method used to model the relationship between two variables: one independent variable and one dependent variable.

  • In this homework, I will perform simple linear regressions using the Housing.csv dataset.

Dataset housing

This dataset was grabbed from:

https://www.kaggle.com/datasets/sukhmandeepsinghbrar/housing-price-dataset?resource=download&select=Housing.csv

housing <- read.csv("Housing.csv", header = T)
head(housing)
          id            date   price bedrooms bathrooms sqft_living sqft_lot
1 7229300521 20141013T000000  231300        2      1.00        1180     5650
2 6414100192 20141209T000000  538000        3      2.25        2570     7242
3 5631500400 20150225T000000  180000        2      1.00         770    10000
4 2487200875 20141209T000000  604000        4      3.00        1960     5000
5 1954400510 20150218T000000  510000        3      2.00        1680     8080
6 7237550310 20140512T000000 1225000        4      4.50        5420   101930
  floors waterfront view condition grade sqft_above sqft_basement yr_built
1      1          0    0         3     7       1180             0     1955
2      2          0    0         3     7       2170           400     1951
3      1          0    0         3     6        770             0     1933
4      1          0    0         5     7       1050           910     1965
5      1          0    0         3     8       1680             0     1987
6      1          0    0         3    11       3890          1530     2001
  yr_renovated zipcode     lat     long sqft_living15 sqft_lot15
1            0   98178 47.5112 -122.257          1340       5650
2         1991   98125 47.7210 -122.319          1690       7639
3            0   98028 47.7379 -122.233          2720       8062
4            0   98136 47.5208 -122.393          1360       5000
5            0   98074 47.6168 -122.045          1800       7503
6            0   98053 47.6561 -122.005          4760     101930

GGPLOT Bedroom to Price

model: \(\text{Price} = \beta_0 + \beta_1 \cdot \text{Bedrooms} + \varepsilon\)
fitted: \(\text{Price} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Bedrooms}\)
\(\hat{\beta}_0 = b_0 - \text{estimate of } \beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of } \beta_1\)

GGPLOT Bedroom to Price Code

ggplot(data = housing, aes(x = bedrooms, y = price, color=price)) +
  geom_point() +
  labs(x = "Bedrooms", y = "Price") +
  ggtitle("Price vs Bedrooms") +
  geom_smooth(method = "lm",aes(group = 1), se=F)+
  scale_color_gradient(low = "blue", high = "red")

Plotly Square Footage of Living Room

model: \(\text{Price} = \beta_0 + \beta_1 \cdot \text{Living Space} + \varepsilon\)
fitted: \(\text{Price} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Living Space}\)
\(\hat{\beta}_0 = b_0 - \text{estimate of } \beta_0\);       \(\hat{\beta}_1 = b_1 - \text{estimate of } \beta_1\)

Plotly Square Footage of Living Room Code

plot_ly(x=housing$sqft_living , y=housing$price , type = "scatter",
  mode= "markers", name="data") %>% 
  add_lines(x=housing$sqft_living, y = fitted(lm_model), name="fitted") %>% 
  layout(xaxis=xax, yaxis=yax)

GGPLOT Bathrooms to Bedrooms

model: \(\text{Bathrooms} = \beta_0 + \beta_1 \cdot \text{Bedrooms} + \varepsilon\)
fitted: \(\text{Bathrooms} = \hat{\beta}_0 + \hat{\beta}_1 \cdot \text{Bedrooms}\)
\(\hat{\beta}_0 = b_0 - \text{estimate of } \beta_0\); \(\hat{\beta}_1 = b_1 - \text{estimate of } \beta_1\)

GGPLOT Scatterplot Bathrooms vs Bedrooms Code

ggplot(data = housing, aes(x = bedrooms, y = bathrooms, color=price)) +
  geom_point() +
  labs(x = "bedrooms", y = "bathrooms") +
  ggtitle("Bathrooms vs Bedrooms") +
  geom_smooth(method = "lm",aes(group = 1), se=F)+
  scale_color_gradient(low = "blue", high = "red")