Dữ liệu

data <- read.csv("train.csv", stringsAsFactors=FALSE)

str(data)
## 'data.frame':    1460 obs. of  81 variables:
##  $ Id           : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ MSSubClass   : int  60 20 60 70 60 50 20 60 50 190 ...
##  $ MSZoning     : chr  "RL" "RL" "RL" "RL" ...
##  $ LotFrontage  : int  65 80 68 60 84 85 75 NA 51 50 ...
##  $ LotArea      : int  8450 9600 11250 9550 14260 14115 10084 10382 6120 7420 ...
##  $ Street       : chr  "Pave" "Pave" "Pave" "Pave" ...
##  $ Alley        : chr  NA NA NA NA ...
##  $ LotShape     : chr  "Reg" "Reg" "IR1" "IR1" ...
##  $ LandContour  : chr  "Lvl" "Lvl" "Lvl" "Lvl" ...
##  $ Utilities    : chr  "AllPub" "AllPub" "AllPub" "AllPub" ...
##  $ LotConfig    : chr  "Inside" "FR2" "Inside" "Corner" ...
##  $ LandSlope    : chr  "Gtl" "Gtl" "Gtl" "Gtl" ...
##  $ Neighborhood : chr  "CollgCr" "Veenker" "CollgCr" "Crawfor" ...
##  $ Condition1   : chr  "Norm" "Feedr" "Norm" "Norm" ...
##  $ Condition2   : chr  "Norm" "Norm" "Norm" "Norm" ...
##  $ BldgType     : chr  "1Fam" "1Fam" "1Fam" "1Fam" ...
##  $ HouseStyle   : chr  "2Story" "1Story" "2Story" "2Story" ...
##  $ OverallQual  : int  7 6 7 7 8 5 8 7 7 5 ...
##  $ OverallCond  : int  5 8 5 5 5 5 5 6 5 6 ...
##  $ YearBuilt    : int  2003 1976 2001 1915 2000 1993 2004 1973 1931 1939 ...
##  $ YearRemodAdd : int  2003 1976 2002 1970 2000 1995 2005 1973 1950 1950 ...
##  $ RoofStyle    : chr  "Gable" "Gable" "Gable" "Gable" ...
##  $ RoofMatl     : chr  "CompShg" "CompShg" "CompShg" "CompShg" ...
##  $ Exterior1st  : chr  "VinylSd" "MetalSd" "VinylSd" "Wd Sdng" ...
##  $ Exterior2nd  : chr  "VinylSd" "MetalSd" "VinylSd" "Wd Shng" ...
##  $ MasVnrType   : chr  "BrkFace" "None" "BrkFace" "None" ...
##  $ MasVnrArea   : int  196 0 162 0 350 0 186 240 0 0 ...
##  $ ExterQual    : chr  "Gd" "TA" "Gd" "TA" ...
##  $ ExterCond    : chr  "TA" "TA" "TA" "TA" ...
##  $ Foundation   : chr  "PConc" "CBlock" "PConc" "BrkTil" ...
##  $ BsmtQual     : chr  "Gd" "Gd" "Gd" "TA" ...
##  $ BsmtCond     : chr  "TA" "TA" "TA" "Gd" ...
##  $ BsmtExposure : chr  "No" "Gd" "Mn" "No" ...
##  $ BsmtFinType1 : chr  "GLQ" "ALQ" "GLQ" "ALQ" ...
##  $ BsmtFinSF1   : int  706 978 486 216 655 732 1369 859 0 851 ...
##  $ BsmtFinType2 : chr  "Unf" "Unf" "Unf" "Unf" ...
##  $ BsmtFinSF2   : int  0 0 0 0 0 0 0 32 0 0 ...
##  $ BsmtUnfSF    : int  150 284 434 540 490 64 317 216 952 140 ...
##  $ TotalBsmtSF  : int  856 1262 920 756 1145 796 1686 1107 952 991 ...
##  $ Heating      : chr  "GasA" "GasA" "GasA" "GasA" ...
##  $ HeatingQC    : chr  "Ex" "Ex" "Ex" "Gd" ...
##  $ CentralAir   : chr  "Y" "Y" "Y" "Y" ...
##  $ Electrical   : chr  "SBrkr" "SBrkr" "SBrkr" "SBrkr" ...
##  $ X1stFlrSF    : int  856 1262 920 961 1145 796 1694 1107 1022 1077 ...
##  $ X2ndFlrSF    : int  854 0 866 756 1053 566 0 983 752 0 ...
##  $ LowQualFinSF : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ GrLivArea    : int  1710 1262 1786 1717 2198 1362 1694 2090 1774 1077 ...
##  $ BsmtFullBath : int  1 0 1 1 1 1 1 1 0 1 ...
##  $ BsmtHalfBath : int  0 1 0 0 0 0 0 0 0 0 ...
##  $ FullBath     : int  2 2 2 1 2 1 2 2 2 1 ...
##  $ HalfBath     : int  1 0 1 0 1 1 0 1 0 0 ...
##  $ BedroomAbvGr : int  3 3 3 3 4 1 3 3 2 2 ...
##  $ KitchenAbvGr : int  1 1 1 1 1 1 1 1 2 2 ...
##  $ KitchenQual  : chr  "Gd" "TA" "Gd" "Gd" ...
##  $ TotRmsAbvGrd : int  8 6 6 7 9 5 7 7 8 5 ...
##  $ Functional   : chr  "Typ" "Typ" "Typ" "Typ" ...
##  $ Fireplaces   : int  0 1 1 1 1 0 1 2 2 2 ...
##  $ FireplaceQu  : chr  NA "TA" "TA" "Gd" ...
##  $ GarageType   : chr  "Attchd" "Attchd" "Attchd" "Detchd" ...
##  $ GarageYrBlt  : int  2003 1976 2001 1998 2000 1993 2004 1973 1931 1939 ...
##  $ GarageFinish : chr  "RFn" "RFn" "RFn" "Unf" ...
##  $ GarageCars   : int  2 2 2 3 3 2 2 2 2 1 ...
##  $ GarageArea   : int  548 460 608 642 836 480 636 484 468 205 ...
##  $ GarageQual   : chr  "TA" "TA" "TA" "TA" ...
##  $ GarageCond   : chr  "TA" "TA" "TA" "TA" ...
##  $ PavedDrive   : chr  "Y" "Y" "Y" "Y" ...
##  $ WoodDeckSF   : int  0 298 0 0 192 40 255 235 90 0 ...
##  $ OpenPorchSF  : int  61 0 42 35 84 30 57 204 0 4 ...
##  $ EnclosedPorch: int  0 0 0 272 0 0 0 228 205 0 ...
##  $ X3SsnPorch   : int  0 0 0 0 0 320 0 0 0 0 ...
##  $ ScreenPorch  : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ PoolArea     : int  0 0 0 0 0 0 0 0 0 0 ...
##  $ PoolQC       : chr  NA NA NA NA ...
##  $ Fence        : chr  NA NA NA NA ...
##  $ MiscFeature  : chr  NA NA NA NA ...
##  $ MiscVal      : int  0 0 0 0 0 700 0 350 0 0 ...
##  $ MoSold       : int  2 5 9 2 12 10 8 11 4 1 ...
##  $ YrSold       : int  2008 2007 2008 2006 2008 2009 2007 2009 2008 2008 ...
##  $ SaleType     : chr  "WD" "WD" "WD" "WD" ...
##  $ SaleCondition: chr  "Normal" "Normal" "Normal" "Abnorml" ...
##  $ SalePrice    : int  208500 181500 223500 140000 250000 143000 307000 200000 129900 118000 ...

Tách dữ liệu thành 2 bộ dữ liệu train để huấn luyện mô hình tuyến tính và test để thử nghiệm lại mô hình sau đó. Lấy 3/4 số dữ liệu để huấn luyện và dành lại 1/4 số dữ liệu cho việc thử nghiệm.

rows <- nrow(data) #đếm số dòng của dữ liệu
# 3/4 số lượng dữ liệu
bound <- round(rows*3/4) #số dòng nhân 3/4

train <- data[ 1           : bound, ]  #từ dòng 1 đến dòng bound
test  <- data[ (bound + 1) : rows , ] #dòng từ bound đến còn lại

Tương quan

Các chỉ số về số phòng chịu ảnh hưởng tởi diện tích sinh hoạt nên có thể được bỏ qua khi so sánh. Ta thấy giá trị của căn nhà phụ thuộc nhiều vào năm xây dựng, diện tích sinh hoạt, diện tích gara, diện tích tầng hầm.

train %>% select(SalePrice,
                 OverallQual,
                 OverallCond,
                 YearBuilt,
                 YearRemodAdd,
                 LotArea,
                 TotalBsmtSF,
                 GrLivArea,
                 GarageArea,
                 BedroomAbvGr,
                 KitchenAbvGr,
                 TotRmsAbvGrd,
                 BsmtFullBath,
                 BsmtHalfBath,
                 FullBath,
                 HalfBath,
                 Fireplaces) %>% 
  cor() %>% 
  corrplot(type="lower",  sig.level = 0.01, insig = "blank")

Biểu đồ scatterplot thể hiện rõ sự tương quan này. Những căn nhà mới hơn có giá bán cao hơn. Những căn nhà được xây sau cũng có xu hướng sở hữu hiện tích tầng hầm và garage rộng hơn.

pairs(~SalePrice+YearBuilt+TotalBsmtSF+GrLivArea+GarageArea,data=train, main="Scatterplot")

scatterplot(SalePrice ~ YearBuilt, data=train,  xlab="Nam xay dung", ylab="Gia tri", grid=FALSE)

scatterplot(SalePrice ~ TotalBsmtSF, data=train,  xlab="Dien tich tang ham", ylab="Gia tri", grid=FALSE)

scatterplot(SalePrice ~ GrLivArea, data=train,  xlab="Dien tich sinh hoat", ylab="Gia tri", grid=FALSE)

scatterplot(SalePrice ~ GarageArea, data=train,  xlab="Dien tich garage", ylab="Gia tri", grid=FALSE)

Mô hình tuyến tính

lm_model <- lm(SalePrice ~ YearBuilt+TotalBsmtSF+GrLivArea+GarageArea, data=train)
summary(lm_model)
## 
## Call:
## lm(formula = SalePrice ~ YearBuilt + TotalBsmtSF + GrLivArea + 
##     GarageArea, data = train)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -388240  -18945   -2430   17538  257321 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.269e+06  9.117e+04 -13.920   <2e-16 ***
## YearBuilt    6.358e+02  4.711e+01  13.497   <2e-16 ***
## TotalBsmtSF  4.843e+01  3.404e+00  14.229   <2e-16 ***
## GrLivArea    7.643e+01  2.755e+00  27.737   <2e-16 ***
## GarageArea   6.400e+01  7.410e+00   8.637   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39540 on 1090 degrees of freedom
## Multiple R-squared:  0.755,  Adjusted R-squared:  0.7541 
## F-statistic: 839.8 on 4 and 1090 DF,  p-value: < 2.2e-16

Thử dự báo trên dữ liệu test và kiểm tra kết quả với RMSE theo cách đánh giá của Kaggle.

prediction <- predict(lm_model, test)
log_prediction <- log(prediction)
log_SalePrice <- log(test$SalePrice)
rmse(log_SalePrice, log_prediction)
## [1] 0.2394648