1.2 lnitial data analysis Solution:

melbdata <- read.csv("Melbourne_housing_FULL.csv")

library(dplyr)
## 
## 载入程辑包:'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
melbdata.sub <- melbdata %>%
  filter(Suburb %in% c("Hawthorn", "Brunswick", "Craigieburn")) %>%
  mutate(Suburb = factor(Suburb, levels = c("Craigieburn", "Brunswick", "Hawthorn")))

1.3 Finding associationl Solution:

lm1 <- lm(data = melbdata.sub, Price/1000 ~ BuildingArea)

library(ggplot2)
library(dplyr)

library(ggfortify)

melbdata.sub_out <- melbdata %>%
  filter(Suburb %in% c("Hawthorn", "Brunswick", "Craigieburn")) %>%
  mutate(Suburb = factor(Suburb, levels = c("Craigieburn", "Brunswick","Hawthorn"))) %>%
  slice(-c(158,682))

1.4 Finding association Ⅰ Solution:

lm2 <- lm(Price ~ Suburb + BuildingArea, data = melbdata.sub)
summary(lm2)
## 
## Call:
## lm(formula = Price ~ Suburb + BuildingArea, data = melbdata.sub)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -4074050  -248461   -26674   166018  5479862 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -150520.6    78836.2  -1.909   0.0569 .  
## SuburbBrunswick  660277.3    72911.5   9.056   <2e-16 ***
## SuburbHawthorn  1060993.2    79615.5  13.326   <2e-16 ***
## BuildingArea       4435.4      346.9  12.786   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 609900 on 414 degrees of freedom
##   (因为不存在,709个观察量被删除了)
## Multiple R-squared:  0.425,  Adjusted R-squared:  0.4208 
## F-statistic:   102 on 3 and 414 DF,  p-value: < 2.2e-16
boxplot(Price ~ Suburb, data=melbdata.sub)

  1. SuburbBrunswick has a coefficient of 781107.7, which means that home values in SuburbBrunswick are on average $781107.7 higher when compared to other suburbs. SuburbHawthorn has a coefficient of 1144544.5, which means that home values in SuburbHawthorn are on average $1144544.5 higher when compared to other suburbs.

  2. Price=

melbdata.sub$Suburb=as.factor(melbdata.sub$Suburb)

my.colors <- rgb(c(1,0,0),c(0,1,0),c(0,0,1),alpha = 0.5)
plot(Price ~ BuildingArea, data = melbdata.sub_out,
     main = "House price of some suburbs against Building Area",
     xlab = "Building Area(in sqm)",
     ylab = "Price ($AUD)",
     col = my.colors[as.integer(melbdata.sub_out[["Suburb"]])],
     pch = 19)
legend("topright", legend = levels(melbdata.sub_out[["Suburb"]]),
       col=my.colors, pch = 19)

obs_sub <- subset(melbdata.sub_out, select = c("BuildingArea", "Suburb"))

buArea_sub <- with(obs_sub, split(BuildingArea, Suburb))


pred1 = data.frame(BuildingArea = buArea_sub[["Brunswick"]], Suburb = "Brunswick")
lines(pred1[["BuildingArea"]], predict(lm2, newdata = pred1),col='green')

pred2 = data.frame(BuildingArea = buArea_sub[["Craigieburn"]], Suburb = "Craigieburn")
lines(pred2[["BuildingArea"]], predict(lm2, newdata = pred2),col='red')

pred3 = data.frame(BuildingArea = buArea_sub[["Hawthorn"]], Suburb = "Hawthorn")
lines(pred3[["BuildingArea"]], predict(lm2, newdata = pred3),col='blue')

library(ggfortify)
autoplot(lm2, which=1:6, nrow=3, ncol=2)

lm3 <-lm(Price ~ Suburb + BuildingArea+ Car,data=melbdata.sub)
summary(lm3)
## 
## Call:
## lm(formula = Price ~ Suburb + BuildingArea + Car, data = melbdata.sub)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -3417281  -273352   -59191   252474  5001704 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     -447547.1    89421.2  -5.005 8.37e-07 ***
## SuburbBrunswick  781107.7    73776.4  10.588  < 2e-16 ***
## SuburbHawthorn  1144544.5    78263.7  14.624  < 2e-16 ***
## BuildingArea       3761.7      351.3  10.708  < 2e-16 ***
## Car              220740.5    34616.8   6.377 4.98e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 588500 on 402 degrees of freedom
##   (因为不存在,720个观察量被删除了)
## Multiple R-squared:  0.477,  Adjusted R-squared:  0.4718 
## F-statistic: 91.66 on 4 and 402 DF,  p-value: < 2.2e-16
autoplot(lm3, which=1:6, nrow=3, ncol=2)