--- title: '44008 STATS 201/8 Assignment 3' output: pdf_document: toc: yes author: "your name" date: "2022-08-20" --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ## Question 1 ### Question of interest/goal of the study We are interested in the typical sale prices for 3-bedroom and 4-bedroom houses in Auckland. In particular, we want to see whether buying a 4- bedroom house increases the cost by more than 15% as compared to buying a 3-bedroom house. ### Read the data. ```{r} house.df=read.csv(file='house.csv', header = TRUE, stringsAsFactors = TRUE) ``` ### Inspect the data: ```{r} stripchart(Price~Bedrooms,main="Price by Number of Bedrooms",method="stack",pch=1,data=house.df) ``` ```{r} boxplot(Price~Bedrooms,main="Price by Number of Bedrooms",horizontal=TRUE,data=house.df) ``` ```{r} library(s20x) summaryStats(Price~Bedrooms, data=house.df) ``` ```{r} stripchart(log(Price)~Bedrooms,main="log(Price) by Number of Bedrooms",method="stack",pch=1,data=house.df) ``` ```{r} boxplot(log(Price)~Bedrooms,main="log(Price) by Number of Bedrooms",horizontal=TRUE,data=house.df) ``` ```{r} summaryStats(log(Price)~Bedrooms, data=house.df) ``` ### Comment on plots and summary statistics By inspecting the data, we can intuitively see that the average price and median price of a 4-bedroom apartment are more expensive than that of a 3-bedroom apartment, which is what we care about. For the logarithmic price distribution, we can see that the overall distribution of prices is more relevant, convenient for our further data analysis. ### 1.5 We will fit models to the log-transformed data and to the untransformed data. Why aren¡¯t we concerned about the lack of Normality for using either an untransformed or log model? First of all, we have enough data. During the modeling process, we pay more attention to the relationship between the two sets of data.Logarithmic transformation tends to be normal. After completing the modeling, we can use non-parametric testing to complete hypothesis testing. ### 1.6 Fit linear model on the untransformed data and check assumptions. Generate confidence intervals for the model.(DO NOT use the Welch test here.) ```{r,warning=FALSE} house.df$Price[which(house.df$Bedrooms=="3Rooms")]=1.15*house.df$Price[which(house.df$Bedrooms=="3Rooms")] shapiro.test(house.df$Price) bartlett.test(Price~Bedrooms, house.df) aov1=aov(Price~Bedrooms,house.df,alternative="greater") summary(aov1) model.tables(aov1,"means") predict(aov1,list(1820356,Bedrooms='3Rooms'),interval = "confidence") predict(aov1,list(1982567,Bedrooms='4Rooms'),interval = "confidence") ``` It can be seen that the two data distributions are significantly different, and it can be considered that the price of a 4-bedroom is more than 15% higher than that of a 3-bedroom ### 1.7 Interpret BOTH confidence intervals from above model (as if for an Executive Summary) The above two confidence intervals reflect the value interval of the mean value of the two types of houses under the condition of 95% confidence.This reflects the price intervals in which we normally buy a house ### 1.8 ```{r,warning=FALSE} shapiro.test(log(house.df$Price)) bartlett.test(log(Price)~Bedrooms,house.df ) aov2=aov(log(Price)~Bedrooms,house.df,alternative="greater") summary(aov2) model.tables(aov2,"means") predict(aov2,list(log(14.36),Bedrooms='3Rooms'),interval = "confidence") predict(aov2,list(log(14.42),Bedrooms='4Rooms'),interval = "confidence") ``` ### 1.9 Interpret BOTH confidence intervals from above model (as if for an Executive Summary). The above two confidence intervals are the value intervals of the logarithm of two different house prices under the condition of 95% confidence.This reflects the logarithm of the price intervals in which we normally buy a house ### 1.10 Write the equation for each of the two models fitted (as if for Methods and Assumption Checks). Methods:ANOVA Assumption:There was no significant difference in the distribution of prices for a 15% increase in the price of a three-bedroom and a four-bedroom equation:Y=AX+e where A=diag(I_{N1},I_{N2}),I_{N}means [1,1...1]^{T} ```{r} coefficients(aov1) coefficients(aov2) ``` ### 1.11 Does buying a four bedroom house increase the cost by more than 15% as compared to buying a three bedroom house?Justify your answer. We can compare the confidence intervals and find that the obvious confidence interval of the four-bedroom apartment is higher than the confidence interval of the three-bedroom apartment in the previous session, which means that the price of a four-bedroom apartment is 15% higher than that of a three-bedroom apartment. ### 1.12 To address all the questions the lecturer has, do you prefer to use the log-transformed model or not transformed model? Justify your choice with at least two reasons. log-transformed model.Because the data has better normality and homogeneity of variance after log transformation, which ensures the rationality of our model.The fitted model has better effect,have a stronger linear correlation. ## Question 2 ### 2.1 Question of interest/goal of the study We wish to see whether sale price of the house increases as the land area increase, and we also wish to see whether the effect of land area on sale price is the same for both 3- and 4-bedroom houses. ### 2.2 Read in and inspect the data: ```{r} land.df=read.csv("land.csv", header = TRUE, stringsAsFactors = TRUE) plot(Price~Land,data=land.df,ylab='Price ($)',xlab='Land Area (square metres)', main = "House Prices by Land Area", pch = ifelse(Bedrooms == '3Rooms', '3', '4'), col = ifelse(Bedrooms == '3Rooms', 'blue', 'red')) legend('topleft',c('3-Bedroom','4-Bedroom'),col=c("blue","red"),pch=c('3', '4')) ``` ```{r} plot(log(Price)~Land,data=land.df,ylab='log(House Price)',xlab='Land Area (square metres)', main = "log(Prices) by Land Area", pch = ifelse(Bedrooms == '3Rooms', '3', '4'), col = ifelse(Bedrooms == '3Rooms', 'blue', 'red')) legend('topleft',c('3-Bedroom','4-Bedroom'),col=c("blue","red"),pch=c('3', '4')) ``` ### 2.3 Comment on plots Through the plot, we can see that the sale price increases with the increase of the land area and the 4-bedroom increases linearly faster, which is what we are concerned about. ### 2.4What is the main reason we should be using log(Price) instead of Price for this analysis? Eliminate heteroscedasticity and facilitate our calculations ### 2.5 Fit an appropriate linear Model and Check Assumptions ```{r} aov3=aov(log(Price)~Land+Bedrooms, land.df) summary(aov3) coefficients(aov3) ``` Through the model coefficients, we find the linear coefficient is small. ### 2.6 Method and Assumption Checks Method:ANCOVA ```{r} cor.test(log(land.df$Price),land.df$Land,method = "pearson") ``` We can see that as land size has a very weak or non-linear correlation with house price ```{r} cor.test((log(land.df$Price[which(land.df$Bedrooms=="4Rooms")])) ,(land.df$Land[which(land.df$Bedrooms=="4Rooms")]),method = "pearson") ``` ```{r} cor.test((log(land.df$Price[which(land.df$Bedrooms=="3Rooms")])) ,(land.df$Land[which(land.df$Bedrooms=="3Rooms")]),method = "pearson") ``` By comparing the 4-bedroom and three-bedroom, it can be seen that the 4-bedroom is more affected by the land area ### 2.7 Executive Summary (Remember to answer ALL the questions asked.) Through our modeling and analysis, we can get the following conclusions, there is no obvious linear relationship between the land area and house price we care about. However, the 4-bedroom and 3-bedroom are not affected by the change in land area, and the 4-bedroom is more significantly affected