1.1 load the data in...

{r} install.packages("dplyr") install.packages("ggplot2") library(tidyverse) mel <- read.csv("data/Melbourne_housing_FULL.csv") mel

1.2 Subset the data such that the dataset only includes the suburbs "Hawthorn", "Brunswick", and "Craigieburn"

{r} #Base r idx = mel$Suburb %in% c("Brunswick","Craigieburn","Hawthorn") mel.sub = mel[idx,] mel.sub$Suburb = factor(mel.sub$Suburb, levels = c("Brunswick","Craigieburn","Hawthorn")) mel.sub

1.2 Then Determine the average price in each of these three suburbs.

{r} mel |> filter(Suburb %in% c("Brunswick","Craigieburn","Hawthorn")) |> group_by(Suburb) |> summarize(Mean_price = mean(Price, na.rm = T))

1.3 Fit a linear regression model and then extract and interpret its coefficients

```{r}

Since the Price is too large, we divide it by 1000 to make the model more accurate.

mel.sub$Price = mel.sub$Price/1000

plot the linear regression model

lm1 = lm(mel.sub$Price ~ mel.sub$BuildingArea) summary(lm1) #1.4a Fit a multiple linear regression model with "Suburb", Briefly describe how to interpret the regression coefficients returned by lm, create an appropriate visualisation{r} lm2 = lm(mel.sub$Price ~ mel.sub$BuildingArea + mel.sub$Suburb) summary(lm2)

Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 509.7567 62.4510 8.163 3.99e-15 ***

mel.sub$BuildingArea 4.4354 0.3469 12.786 < 2e-16 ***

mel.sub$SuburbCraigieburn -660.2773 72.9115 -9.056 < 2e-16 ***

mel.sub$SuburbHawthorn 400.7159 72.5435 5.524 5.87e-08 ***

Based on the coefficients, The model should be:

(Price in 000s) = 509.7567 + 4.4354(BuildingArea) + -660.2773(Craigieburn) + 400.7159(Hawthorn)

To create a proper visualization for the model, I used color as the 3rd dimension to represent Suburb.

ggplot(mel.sub, aes(x = BuildingArea, y = Price, color = Suburb)) + geompoint() + geomsmooth(method = "lm", se = FALSE) + theme_minimal() + labs(title = "Price vs Building Area by Suburb", x = "Building Area", y = "Price in 000s") ```

1.4b adding carspace

{r} lm3 = lm(mel.sub$Price ~ mel.sub$BuildingArea + mel.sub$Suburb + mel.sub$Car) summary(lm3) # based on the further distance from the median to 0, I conclude that the model has gone worse with a new predictor.