0% found this document useful (0 votes)
5 views11 pages

Tutorial 03 Soln

The document presents a tutorial solution for a dataset on house selling prices, detailing the modeling of selling price based on house size and desirability. It includes steps for deriving correlation, scatter plots, fitting linear regression models, and interpreting coefficients. Additionally, it discusses the application of similar modeling techniques to other datasets, including college acceptance rates and HDB resale prices in Singapore.

Uploaded by

Leon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views11 pages

Tutorial 03 Soln

The document presents a tutorial solution for a dataset on house selling prices, detailing the modeling of selling price based on house size and desirability. It includes steps for deriving correlation, scatter plots, fitting linear regression models, and interpreting coefficients. Additionally, it discusses the application of similar modeling techniques to other datasets, including college acceptance rates and HDB resale prices in Singapore.

Uploaded by

Leon
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Tutorial 3 Solution

DSA1101

PART I: ON-SITE QUESTION

A dataset on house selling price was randomly collected 1 , house_selling_prices_FL.csv. It’s our interest
to model how y = selling price (dollar) is dependent on x = the size of the house (square feet). A simple
linear regression model (y regress on x) was fitted, called Model 1.
The given data has another variable, NW, which specifies if a house is in the part of the town considered
less desirable (NW = 0).

(a) Derive the correlation between x and y.


(b) Derive a scatter plot of y against x. Give your comments on the association of y and x.
√ √
(c) Derive R2 of Model 1. Verify that R2 = |cor(y, x)|. In which situation we can have R2 = cor(y, x)?
(d) Form a model (called Model 2) which has two regressors (x and NW). Report the coefficient of variable
NW in Model 2. Interpret it.
(e) Estimate and report the price of a house where its size is 4000 square feet and is located at the more
desirable part of the town.

Solution

Importing data set into R:

house = [Link]("~/Documents/Data/house_selling_prices_FL.csv")
names(house) # names of columns

## [1] "House" "Taxes" "Bedrooms" "Baths" "Quadrant" "NW" "price"


## [8] "size" "lot"

dim(house) # 100 observations and 9 columns

## [1] 100 9

house$NW = [Link](house$NW) # to declare that NW is categorical

attach(house)

1 Statistics: The Art and Science of Learning from Data, 4th, Agresti, Franklin, Klingenberg

1
(a) The correlation is

cor(price, size)

## [1] 0.7612621

(b) The scatter plot of the price against the size is plotted below.

plot(size, price, pch = 20)


350000
250000
price

150000
50000

1000 2000 3000 4000

size

Comments:
There is a clear (obvious) association shown.
The association is positive.
The association is quite linear.
The variability of y (the price) is quite stable when x (the size) changes.

2
(c) Fitting Model 1.

M1 = lm(price ~ size, data = house)


summary(M1)$[Link]

## [1] 0.57952

sqrt (summary(M1)$[Link] ) # same as cor(price, size) # 0.7612621

## [1] 0.7612621

#summary(M1)

√ √
From the code above, we can see that R2 = |cor(y, x)|: R2 = 0.5795, hence, 0.5795 = 0.761 = |cor(y, x)|.

When cor(y, x) > 0 then in a simple model y ∼ x, we always have R2 = cor(y, x).

(d) Form a model (called Model 2) which has two regressors (x and NW). Report the coefficient of variable
NW in Model 2. Interpret it.

Fit Model 2:

M2 = lm(price ~ size + NW, data = house)


summary(M2)

##
## Call:
## lm(formula = price ~ size + NW, data = house)
##
## Residuals:
## Min 1Q Median 3Q Max
## -83207 -22968 215 14135 109149
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -15257.514 11908.297 -1.281 0.203160
## size 77.985 6.209 12.560 < 2e-16 ***
## NW1 30569.087 7948.742 3.846 0.000215 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## Residual standard error: 34390 on 97 degrees of freedom
## Multiple R-squared: 0.6352, Adjusted R-squared: 0.6276
## F-statistic: 84.43 on 2 and 97 DF, p-value: < 2.2e-16

3
The fitted equation of Model 2:

ŷ = −15257.5 + 77.99x + 30569.1 × I(N W = 1).

The estimated coefficient of NW in Model 2 is 30569.1. This value means: for two houses of the same size
(fix x), the house in the more desirable part (NW = 1) is $30569.1 more than the one in the less desirable
part (NW = 0).

(e) Estimate and report the price of a house where its size is 4000 square feet and is located at the more
desirable part of the town.

predict(M2, newdata=[Link](size=4000, NW = "1"))

## 1
## 327252.1

The mean price of a house with size x = 4000 and NW = 1 is $327252.1.

PART II: OFF-SITE QUESTION

1. Read the data from the file [Link]. Consider a simple linear regression of percentage of appli-
cants accepted (Acceptance) on the median combined math and verbal SAT score of students (SAT),
called Model M1.

(a) Write your own function in R, name the function as simple, to derive the intercept β0 and the slope
β1 of Model M1.

Hint: Use the formula of the estimated coefficients, β̂1 and β̂0 , given in slide 31/52 of Topic 3.

Answer
For a simple model (only one regressor that is quantitative), the two coefficients β0 and β1 of the model are
estimated by
P  P 
n n
Pn yi xi
i=1 i=1
i=1 yi xi − n
β̂1 = P 2 ; β̂0 = ȳ − β̂1 x̄.
n
Pn xi
i=1
i=1 x2i − n

Hence, if we let x, y to denote the vectors that are regressor and response, respectively, then function simple
will be formed as

simple <- function(x , y) {


beta_1 <- (sum(x*y)- mean (y)* sum (x ))/( sum(xˆ2)- mean(x)* sum(x))
beta_0 <- mean(y)- beta_1* mean(x)
return(c( beta_0 , beta_1)) }

4
In order to import data set [Link], we open this .txt file and could see that it has header, and the
values are separated by a tab, not by comma nor space. Hence, we use

dat= [Link]("~/Documents/Data/[Link]",header =TRUE,sep= "\t")


names(dat)

## [1] "School" "School_Type" "SAT" "Acceptance" "DPerStudent"


## [6] "Top.10p" "PerPhD" "GradPer"

head(dat)

## School School_Type SAT Acceptance DPerStudent Top.10p PerPhD GradPer


## 1 Amherst Lib Arts 1315 22 26636 85 81 93
## 2 Swarthmore Lib Arts 1310 24 27487 78 93 88
## 3 Williams Lib Arts 1336 28 23772 86 90 93
## 4 Bowdoin Lib Arts 1300 24 25703 78 95 90
## 5 Wellesley Lib Arts 1250 49 27879 76 91 86
## 6 Pomona Lib Arts 1320 33 26668 79 98 80

Applying function simple() onto the two columns SAT and Acceptance for (x, y), we have output as

simple(x = dat$SAT, y = dat$Acceptance )

## [1] 202.2677440 -0.1300894

Intercept of the equation is 202.2677 and the slope is -0.13.

(b) Use function lm() in R to derive the coefficients of Model M1. Compare with your answer in part (a).

Answer

lm(Acceptance ~ SAT , data =dat )

##
## Call:
## lm(formula = Acceptance ~ SAT, data = dat)
##
## Coefficients:
## (Intercept) SAT
## 202.2677 -0.1301

Both functions simple() and lm() give the same set of coefficients. This helps to verify if function simple()
was correctly defined or not.

5
2. Consider a dataset about HDB resale flats in Singapore give in hdbresale_reg.csv. Consider a simple
model (Model M2) where the resale price is the response and the floor area in square meters is the
only regressor.

(a) Use function simple you formed in the question above to find the coefficients of Model M2.

Answer

hdb = [Link]("~/Documents/Data/hdbresale_reg.csv")
names(hdb)

## [1] "X" "month" "town"


## [4] "flat_type" "block" "street_name"
## [7] "storey_range" "floor_area_sqm" "flat_model"
## [10] "lease_commence_date" "resale_price"

####################
simple(x = hdb$floor_area_sqm, y = hdb$resale_price)

## [1] 115145.730 3117.212

(b) Use function lm() in R to derive the coefficients of Model M2.

Answer

lm(resale_price ~ floor_area_sqm, data = hdb)

##
## Call:
## lm(formula = resale_price ~ floor_area_sqm, data = hdb)
##
## Coefficients:
## (Intercept) floor_area_sqm
## 115146 3117

Both functions simple() and lm() give the same set of coefficients. However, the output from lm() might
be rounded (automatically) by R to 0 decimal places.

6
3. Consider data set given in the file hdbresale_reg.csv on Canvas, which has the information of 6055
HDB resale flats in Singapore. We would want to form a linear model that helps to predict the resale
price of HDB flats, based on the floor area in square meters and the type of the flats.

(a) Consider the resale price, plot a histogram of it and give your comments. Is it suitable to fit a linear
model for this response variable? Explain.
(b) Consider the resale price, plot a histogram of log_e of it and give your comments. Is it more suitable
to fit a linear model for this response variable than the original resale price?
(c) Derive a scatter plot of the log_e of the resale price against the floor area in square meters. Give your
comments.

(d) Fit a linear model where the log of the resale price be the response. Write down the fitted equation.
(e) Report the coefficient of the floor area in square meters and interpret it.
(f) Predict the sale resale price of a 4-room HDB flat that is of 100 square meters.

(g) Report R2 of the model and interpret it.

Solution

Importing the data set into R:

hdb= [Link]("~/Documents/Data/hdbresale_reg.csv",header =TRUE)


names(hdb)

## [1] "X" "month" "town"


## [4] "flat_type" "block" "street_name"
## [7] "storey_range" "floor_area_sqm" "flat_model"
## [10] "lease_commence_date" "resale_price"

(a) Consider the resale price, plot a histogram of it and give your comments. Is it suitable to fit a linear
model for this response variable? Explain.

To answer, we need to check if the column “resale_price’ ’ satisfies: quantitative; symmetric; its variability
is stable when other quantitative regressor(s) change.

hist(hdb$resale_price, col = 2)

7
Histogram of hdb$resale_price
1500
1000
Frequency

500
0

2e+05 4e+05 6e+05 8e+05 1e+06

hdb$resale_price

Obviously, the histogram is right skewed. Hence, resale price is NOT suitable to be the response of a linear
model. For a right skewed variable, it is suggested to try with a transformation by taking log_e.

(b) Consider the resale price, plot a histogram of log_e of it and give your comments. Is it more suitable
to fit a linear model for this response variable than the original resale price?

hist(log(hdb$resale_price ), col = 2)

8
Histogram of log(hdb$resale_price)
1000
Frequency

600
200
0

12.5 13.0 13.5

log(hdb$resale_price)

The histogram of the log of the resale price is more symmetric, hence it is more suitable than the original
resale price as a response of a linear model. We may check to see if the variability of log(resale price) is
stable when x (floor area in sqm) changes or not by the scatter plot (in the question below).

(c) Derive a scatter plot of the log_e of the resale price against the floor area in square meters. Give your
comments.

hdb$[Link] = log(hdb$resale_price) # create a new column for the log(price)


attach(hdb)

plot([Link] ~ floor_area_sqm, col = 2)

9
13.6
13.2
[Link]

12.8
12.4

50 100 150

floor_area_sqm

The scatter plot shows: a very strong, positive and quite linear association between log(price) and the floor
area. The variability of the log(price) seems quite stable when the floor area changes.
Note: the variability of the log(price) is quite stable, may not be strongly stable.
From (b) and (c), it’s quite suitable to fit a linear model for log(price).

(d) Fit a linear model where the log of the resale price be the response. Write down the fitted equation.

M = lm([Link] ~ floor_area_sqm + flat_type, data = hdb)


summary(M)

##
## Call:
## lm(formula = [Link] ~ floor_area_sqm + flat_type, data = hdb)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.28208 -0.07054 -0.01515 0.04323 0.79797
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.235e+01 1.891e-02 653.013 < 2e-16 ***
## floor_area_sqm 3.712e-03 1.817e-04 20.429 < 2e-16 ***

10
## flat_type3 ROOM 1.190e-01 1.714e-02 6.944 4.2e-12 ***
## flat_type4 ROOM 2.093e-01 1.869e-02 11.196 < 2e-16 ***
## flat_type5 ROOM 2.762e-01 2.099e-02 13.160 < 2e-16 ***
## flat_typeEXECUTIVE 4.302e-01 2.527e-02 17.023 < 2e-16 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## Residual standard error: 0.1156 on 6049 degrees of freedom
## Multiple R-squared: 0.7116, Adjusted R-squared: 0.7114
## F-statistic: 2986 on 5 and 6049 DF, p-value: < 2.2e-16

The fitted equation is

[ = 12.35 + 0.003712 ∗ X+0.119 ∗ I(f lat type = 3ROOM )+


log price
0.2093 ∗ I(f lat type = 4ROOM )+
0.2762 ∗ I(f lat type = 5ROOM )+
0.4302 ∗ I(f lat type = Executive)

where X is the floor area in square meters.

(e) Report the coefficient of the floor area in square meters and interpret it.

The coefficient of it is 0.003712. It means that when comparing two flats of the same type, then the one
larger by 1 square meter will be larger in the log(price) by 0.003712.
Equivalently, that one is more expensive by e0.003712 = 1.003719 TIMES.

(f) Predict the resale price of a 4-room HDB flat that is of 100 square meters.

new = [Link](floor_area_sqm = 100, flat_type = "4 ROOM")


[Link] = predict(M, new)
exp([Link])

## 1
## 412807.6

The predicted for the average resale price is about $412,807.6.

(g) Report R2 of the model and interpret it.

summary(M)$[Link] #

## [1] 0.7116378

The value is 0.712. That means model M can explain 71.2% the variability of the response in the sample.

11

You might also like