MSDS Spring 2018

DATA 605 Fundamentals of Computational Mathematics

Jiadi Li

Final Exam: House Prices - Advanced Regression Techniques

. Pick one of the quantitative independent variables from the training data set (train.csv) , and define that variable as X. Make sure this variable is skewed to the right!
. Pick the dependent variable and define it as Y.

#import the dataset
house_price <- read.csv("https://raw.githubusercontent.com/xiaoxiaogao-DD/store/master/train.csv")

#overview
head(house_price)
##   Id MSSubClass MSZoning LotFrontage LotArea Street Alley LotShape
## 1  1         60       RL          65    8450   Pave  <NA>      Reg
## 2  2         20       RL          80    9600   Pave  <NA>      Reg
## 3  3         60       RL          68   11250   Pave  <NA>      IR1
## 4  4         70       RL          60    9550   Pave  <NA>      IR1
## 5  5         60       RL          84   14260   Pave  <NA>      IR1
## 6  6         50       RL          85   14115   Pave  <NA>      IR1
##   LandContour Utilities LotConfig LandSlope Neighborhood Condition1
## 1         Lvl    AllPub    Inside       Gtl      CollgCr       Norm
## 2         Lvl    AllPub       FR2       Gtl      Veenker      Feedr
## 3         Lvl    AllPub    Inside       Gtl      CollgCr       Norm
## 4         Lvl    AllPub    Corner       Gtl      Crawfor       Norm
## 5         Lvl    AllPub       FR2       Gtl      NoRidge       Norm
## 6         Lvl    AllPub    Inside       Gtl      Mitchel       Norm
##   Condition2 BldgType HouseStyle OverallQual OverallCond YearBuilt
## 1       Norm     1Fam     2Story           7           5      2003
## 2       Norm     1Fam     1Story           6           8      1976
## 3       Norm     1Fam     2Story           7           5      2001
## 4       Norm     1Fam     2Story           7           5      1915
## 5       Norm     1Fam     2Story           8           5      2000
## 6       Norm     1Fam     1.5Fin           5           5      1993
##   YearRemodAdd RoofStyle RoofMatl Exterior1st Exterior2nd MasVnrType
## 1         2003     Gable  CompShg     VinylSd     VinylSd    BrkFace
## 2         1976     Gable  CompShg     MetalSd     MetalSd       None
## 3         2002     Gable  CompShg     VinylSd     VinylSd    BrkFace
## 4         1970     Gable  CompShg     Wd Sdng     Wd Shng       None
## 5         2000     Gable  CompShg     VinylSd     VinylSd    BrkFace
## 6         1995     Gable  CompShg     VinylSd     VinylSd       None
##   MasVnrArea ExterQual ExterCond Foundation BsmtQual BsmtCond BsmtExposure
## 1        196        Gd        TA      PConc       Gd       TA           No
## 2          0        TA        TA     CBlock       Gd       TA           Gd
## 3        162        Gd        TA      PConc       Gd       TA           Mn
## 4          0        TA        TA     BrkTil       TA       Gd           No
## 5        350        Gd        TA      PConc       Gd       TA           Av
## 6          0        TA        TA       Wood       Gd       TA           No
##   BsmtFinType1 BsmtFinSF1 BsmtFinType2 BsmtFinSF2 BsmtUnfSF TotalBsmtSF
## 1          GLQ        706          Unf          0       150         856
## 2          ALQ        978          Unf          0       284        1262
## 3          GLQ        486          Unf          0       434         920
## 4          ALQ        216          Unf          0       540         756
## 5          GLQ        655          Unf          0       490        1145
## 6          GLQ        732          Unf          0        64         796
##   Heating HeatingQC CentralAir Electrical X1stFlrSF X2ndFlrSF LowQualFinSF
## 1    GasA        Ex          Y      SBrkr       856       854            0
## 2    GasA        Ex          Y      SBrkr      1262         0            0
## 3    GasA        Ex          Y      SBrkr       920       866            0
## 4    GasA        Gd          Y      SBrkr       961       756            0
## 5    GasA        Ex          Y      SBrkr      1145      1053            0
## 6    GasA        Ex          Y      SBrkr       796       566            0
##   GrLivArea BsmtFullBath BsmtHalfBath FullBath HalfBath BedroomAbvGr
## 1      1710            1            0        2        1            3
## 2      1262            0            1        2        0            3
## 3      1786            1            0        2        1            3
## 4      1717            1            0        1        0            3
## 5      2198            1            0        2        1            4
## 6      1362            1            0        1        1            1
##   KitchenAbvGr KitchenQual TotRmsAbvGrd Functional Fireplaces FireplaceQu
## 1            1          Gd            8        Typ          0        <NA>
## 2            1          TA            6        Typ          1          TA
## 3            1          Gd            6        Typ          1          TA
## 4            1          Gd            7        Typ          1          Gd
## 5            1          Gd            9        Typ          1          TA
## 6            1          TA            5        Typ          0        <NA>
##   GarageType GarageYrBlt GarageFinish GarageCars GarageArea GarageQual
## 1     Attchd        2003          RFn          2        548         TA
## 2     Attchd        1976          RFn          2        460         TA
## 3     Attchd        2001          RFn          2        608         TA
## 4     Detchd        1998          Unf          3        642         TA
## 5     Attchd        2000          RFn          3        836         TA
## 6     Attchd        1993          Unf          2        480         TA
##   GarageCond PavedDrive WoodDeckSF OpenPorchSF EnclosedPorch X3SsnPorch
## 1         TA          Y          0          61             0          0
## 2         TA          Y        298           0             0          0
## 3         TA          Y          0          42             0          0
## 4         TA          Y          0          35           272          0
## 5         TA          Y        192          84             0          0
## 6         TA          Y         40          30             0        320
##   ScreenPorch PoolArea PoolQC Fence MiscFeature MiscVal MoSold YrSold
## 1           0        0   <NA>  <NA>        <NA>       0      2   2008
## 2           0        0   <NA>  <NA>        <NA>       0      5   2007
## 3           0        0   <NA>  <NA>        <NA>       0      9   2008
## 4           0        0   <NA>  <NA>        <NA>       0      2   2006
## 5           0        0   <NA>  <NA>        <NA>       0     12   2008
## 6           0        0   <NA> MnPrv        Shed     700     10   2009
##   SaleType SaleCondition SalePrice
## 1       WD        Normal    208500
## 2       WD        Normal    181500
## 3       WD        Normal    223500
## 4       WD       Abnorml    140000
## 5       WD        Normal    250000
## 6       WD        Normal    143000
dim(house_price)
## [1] 1460   81
#pick SalePrice as the dependent variable, Y
Y <- house_price$SalePrice
summary(Y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   34900  129975  163000  180921  214000  755000
hist(Y,breaks = 80,main = "Histogram: the property's sale price in dollars")

#pick TotalBsmtSF (Total square feet of basement area) as the quantitative independent variable, X
X <- house_price$TotalBsmtSF
summary(X)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0   795.8   991.5  1057.4  1298.2  6110.0
hist(X,breaks = 80,main = "Histogram: total square feet of basement area")

df <- data.frame(X,Y)



Probability
Calculate as a minimum the below probabilities a through c. Assume the small letter “x” is estimated as the 1st quartile of the X variable, and the small letter “y” is estimated as the 1st quartile of the Y variable. Interpret the meaning of all probabilities. In addition, make a table of counts.

quantile(Y)
##     0%    25%    50%    75%   100% 
##  34900 129975 163000 214000 755000
quantile(X)
##      0%     25%     50%     75%    100% 
##    0.00  795.75  991.50 1298.25 6110.00
  1. P(X>x | Y>y)
    Calculate the conditional probability of the basement area being greater than 795.75 \(ft^2\) given the sale price is greater than $129,975.00.
#prepare elements for calculating the probability 
Y1 <- quantile(Y,0.25)
X1 <- quantile(X,0.25)

Y1_count <- nrow(subset(df, Y > Y1))
Y1_X1_count <- nrow(subset(df,(X > X1 & Y > Y1)))
Y1_X1_count / Y1_count
## [1] 0.830137
  1. P(X>x, Y>y)
    Calculate the probability of the basement area being greater than 795.75 \(ft^2\) and sale price being greater than $129,975.00.
Y1_X1_count <- nrow(subset(df,(X > X1 & Y > Y1)))
Y1_X1_count / nrow(df)
## [1] 0.6226027
  1. P(X<x | Y<y)
    Calculate the conditional probability of the basement area being less than 795.75 \(ft^2\) given the sale price is less than $129,975.00.
Y1_count <- nrow(subset(df, Y < Y1))
Y1_X1_count <- nrow(subset(df,(X < X1 & Y < Y1)))
Y1_X1_count / Y1_count
## [1] 0.490411
#prepare elements for making a table of counts
X3 <- quantile(X,0.75)
Y2 <- quantile(Y,0.5)

a1 <- nrow(subset(df,(Y <= Y2 & X <= X3)))
a2 <- nrow(subset(df,(Y > Y2 & X <= X3)))
a3 <- a1 + a2 
b1 <- nrow(subset(df,(Y <= Y2 & X > X3)))
b2 <- nrow(subset(df,(Y > Y2 & X >X3)))
b3 <- b1 + b2
c1 <- a1 + b1
c2 <- a2 + b2
c3 <- c1 + c2

#table of counts: 
matrix(c(a1,a2,a3,b1,b2,b3,c1,c2,c3),nrow = 3)
##      [,1] [,2] [,3]
## [1,]  696   36  732
## [2,]  399  329  728
## [3,] 1095  365 1460

Does splitting the training data in this fashion make them independent? Let A be the new variable counting those observations above the 1st quartile for X, and let B be the new variable counting those observations above the 1st quartile for Y. Does P(AB)=P(A)P(B)? Check mathematically, and then evaluate by running a Chi Square test for association.

No, independence doesn’t change based on the size or selection of the whole dataset.

PA <- nrow(subset(df,X > X1))/nrow(df)
PB <- nrow(subset(df,Y > Y1))/nrow(df)
PAB <- nrow(subset(df,(X > X1 & Y > Y1)))/nrow(df)

PA*PB == PAB
## [1] FALSE

P(AB) \(\neq\) P(A)P(B).

Chi-square test for association:

chisq.test(matrix(c(a1,a2,b1,b2),ncol = 2))
## 
##  Pearson's Chi-squared test with Yates' continuity correction
## 
## data:  matrix(c(a1, a2, b1, b2), ncol = 2)
## X-squared = 313.61, df = 1, p-value < 2.2e-16



Descriptive and Inferential Statistics
Provide univariate descriptive statistics and appropriate plots for the training data set. Provide a scatterplot of X and Y. Derive a correlation matrix for any THREE quantitative variables in the dataset. Test the hypotheses that the correlations between each pairwise set of variables is 0 and provide a 92% confidence interval. Discuss the meaning of your analysis. Would you be worried about familywise error? Why or why not?

summary(house_price)
##        Id           MSSubClass       MSZoning     LotFrontage    
##  Min.   :   1.0   Min.   : 20.0   C (all):  10   Min.   : 21.00  
##  1st Qu.: 365.8   1st Qu.: 20.0   FV     :  65   1st Qu.: 59.00  
##  Median : 730.5   Median : 50.0   RH     :  16   Median : 69.00  
##  Mean   : 730.5   Mean   : 56.9   RL     :1151   Mean   : 70.05  
##  3rd Qu.:1095.2   3rd Qu.: 70.0   RM     : 218   3rd Qu.: 80.00  
##  Max.   :1460.0   Max.   :190.0                  Max.   :313.00  
##                                                  NA's   :259     
##     LotArea        Street      Alley      LotShape  LandContour
##  Min.   :  1300   Grvl:   6   Grvl:  50   IR1:484   Bnk:  63   
##  1st Qu.:  7554   Pave:1454   Pave:  41   IR2: 41   HLS:  50   
##  Median :  9478               NA's:1369   IR3: 10   Low:  36   
##  Mean   : 10517                           Reg:925   Lvl:1311   
##  3rd Qu.: 11602                                                
##  Max.   :215245                                                
##                                                                
##   Utilities      LotConfig    LandSlope   Neighborhood   Condition1  
##  AllPub:1459   Corner : 263   Gtl:1382   NAmes  :225   Norm   :1260  
##  NoSeWa:   1   CulDSac:  94   Mod:  65   CollgCr:150   Feedr  :  81  
##                FR2    :  47   Sev:  13   OldTown:113   Artery :  48  
##                FR3    :   4              Edwards:100   RRAn   :  26  
##                Inside :1052              Somerst: 86   PosN   :  19  
##                                          Gilbert: 79   RRAe   :  11  
##                                          (Other):707   (Other):  15  
##    Condition2     BldgType      HouseStyle   OverallQual    
##  Norm   :1445   1Fam  :1220   1Story :726   Min.   : 1.000  
##  Feedr  :   6   2fmCon:  31   2Story :445   1st Qu.: 5.000  
##  Artery :   2   Duplex:  52   1.5Fin :154   Median : 6.000  
##  PosN   :   2   Twnhs :  43   SLvl   : 65   Mean   : 6.099  
##  RRNn   :   2   TwnhsE: 114   SFoyer : 37   3rd Qu.: 7.000  
##  PosA   :   1                 1.5Unf : 14   Max.   :10.000  
##  (Other):   2                 (Other): 19                   
##   OverallCond      YearBuilt     YearRemodAdd    RoofStyle   
##  Min.   :1.000   Min.   :1872   Min.   :1950   Flat   :  13  
##  1st Qu.:5.000   1st Qu.:1954   1st Qu.:1967   Gable  :1141  
##  Median :5.000   Median :1973   Median :1994   Gambrel:  11  
##  Mean   :5.575   Mean   :1971   Mean   :1985   Hip    : 286  
##  3rd Qu.:6.000   3rd Qu.:2000   3rd Qu.:2004   Mansard:   7  
##  Max.   :9.000   Max.   :2010   Max.   :2010   Shed   :   2  
##                                                              
##     RoofMatl     Exterior1st   Exterior2nd    MasVnrType    MasVnrArea    
##  CompShg:1434   VinylSd:515   VinylSd:504   BrkCmn : 15   Min.   :   0.0  
##  Tar&Grv:  11   HdBoard:222   MetalSd:214   BrkFace:445   1st Qu.:   0.0  
##  WdShngl:   6   MetalSd:220   HdBoard:207   None   :864   Median :   0.0  
##  WdShake:   5   Wd Sdng:206   Wd Sdng:197   Stone  :128   Mean   : 103.7  
##  ClyTile:   1   Plywood:108   Plywood:142   NA's   :  8   3rd Qu.: 166.0  
##  Membran:   1   CemntBd: 61   CmentBd: 60                 Max.   :1600.0  
##  (Other):   2   (Other):128   (Other):136                 NA's   :8       
##  ExterQual ExterCond  Foundation  BsmtQual   BsmtCond    BsmtExposure
##  Ex: 52    Ex:   3   BrkTil:146   Ex  :121   Fa  :  45   Av  :221    
##  Fa: 14    Fa:  28   CBlock:634   Fa  : 35   Gd  :  65   Gd  :134    
##  Gd:488    Gd: 146   PConc :647   Gd  :618   Po  :   2   Mn  :114    
##  TA:906    Po:   1   Slab  : 24   TA  :649   TA  :1311   No  :953    
##            TA:1282   Stone :  6   NA's: 37   NA's:  37   NA's: 38    
##                      Wood  :  3                                      
##                                                                      
##  BsmtFinType1   BsmtFinSF1     BsmtFinType2   BsmtFinSF2     
##  ALQ :220     Min.   :   0.0   ALQ :  19    Min.   :   0.00  
##  BLQ :148     1st Qu.:   0.0   BLQ :  33    1st Qu.:   0.00  
##  GLQ :418     Median : 383.5   GLQ :  14    Median :   0.00  
##  LwQ : 74     Mean   : 443.6   LwQ :  46    Mean   :  46.55  
##  Rec :133     3rd Qu.: 712.2   Rec :  54    3rd Qu.:   0.00  
##  Unf :430     Max.   :5644.0   Unf :1256    Max.   :1474.00  
##  NA's: 37                      NA's:  38                     
##    BsmtUnfSF       TotalBsmtSF      Heating     HeatingQC CentralAir
##  Min.   :   0.0   Min.   :   0.0   Floor:   1   Ex:741    N:  95    
##  1st Qu.: 223.0   1st Qu.: 795.8   GasA :1428   Fa: 49    Y:1365    
##  Median : 477.5   Median : 991.5   GasW :  18   Gd:241              
##  Mean   : 567.2   Mean   :1057.4   Grav :   7   Po:  1              
##  3rd Qu.: 808.0   3rd Qu.:1298.2   OthW :   2   TA:428              
##  Max.   :2336.0   Max.   :6110.0   Wall :   4                       
##                                                                     
##  Electrical     X1stFlrSF      X2ndFlrSF     LowQualFinSF    
##  FuseA:  94   Min.   : 334   Min.   :   0   Min.   :  0.000  
##  FuseF:  27   1st Qu.: 882   1st Qu.:   0   1st Qu.:  0.000  
##  FuseP:   3   Median :1087   Median :   0   Median :  0.000  
##  Mix  :   1   Mean   :1163   Mean   : 347   Mean   :  5.845  
##  SBrkr:1334   3rd Qu.:1391   3rd Qu.: 728   3rd Qu.:  0.000  
##  NA's :   1   Max.   :4692   Max.   :2065   Max.   :572.000  
##                                                              
##    GrLivArea     BsmtFullBath     BsmtHalfBath        FullBath    
##  Min.   : 334   Min.   :0.0000   Min.   :0.00000   Min.   :0.000  
##  1st Qu.:1130   1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:1.000  
##  Median :1464   Median :0.0000   Median :0.00000   Median :2.000  
##  Mean   :1515   Mean   :0.4253   Mean   :0.05753   Mean   :1.565  
##  3rd Qu.:1777   3rd Qu.:1.0000   3rd Qu.:0.00000   3rd Qu.:2.000  
##  Max.   :5642   Max.   :3.0000   Max.   :2.00000   Max.   :3.000  
##                                                                   
##     HalfBath       BedroomAbvGr    KitchenAbvGr   KitchenQual
##  Min.   :0.0000   Min.   :0.000   Min.   :0.000   Ex:100     
##  1st Qu.:0.0000   1st Qu.:2.000   1st Qu.:1.000   Fa: 39     
##  Median :0.0000   Median :3.000   Median :1.000   Gd:586     
##  Mean   :0.3829   Mean   :2.866   Mean   :1.047   TA:735     
##  3rd Qu.:1.0000   3rd Qu.:3.000   3rd Qu.:1.000              
##  Max.   :2.0000   Max.   :8.000   Max.   :3.000              
##                                                              
##   TotRmsAbvGrd    Functional    Fireplaces    FireplaceQu   GarageType 
##  Min.   : 2.000   Maj1:  14   Min.   :0.000   Ex  : 24    2Types :  6  
##  1st Qu.: 5.000   Maj2:   5   1st Qu.:0.000   Fa  : 33    Attchd :870  
##  Median : 6.000   Min1:  31   Median :1.000   Gd  :380    Basment: 19  
##  Mean   : 6.518   Min2:  34   Mean   :0.613   Po  : 20    BuiltIn: 88  
##  3rd Qu.: 7.000   Mod :  15   3rd Qu.:1.000   TA  :313    CarPort:  9  
##  Max.   :14.000   Sev :   1   Max.   :3.000   NA's:690    Detchd :387  
##                   Typ :1360                               NA's   : 81  
##   GarageYrBlt   GarageFinish   GarageCars      GarageArea     GarageQual 
##  Min.   :1900   Fin :352     Min.   :0.000   Min.   :   0.0   Ex  :   3  
##  1st Qu.:1961   RFn :422     1st Qu.:1.000   1st Qu.: 334.5   Fa  :  48  
##  Median :1980   Unf :605     Median :2.000   Median : 480.0   Gd  :  14  
##  Mean   :1979   NA's: 81     Mean   :1.767   Mean   : 473.0   Po  :   3  
##  3rd Qu.:2002                3rd Qu.:2.000   3rd Qu.: 576.0   TA  :1311  
##  Max.   :2010                Max.   :4.000   Max.   :1418.0   NA's:  81  
##  NA's   :81                                                              
##  GarageCond  PavedDrive   WoodDeckSF      OpenPorchSF     EnclosedPorch   
##  Ex  :   2   N:  90     Min.   :  0.00   Min.   :  0.00   Min.   :  0.00  
##  Fa  :  35   P:  30     1st Qu.:  0.00   1st Qu.:  0.00   1st Qu.:  0.00  
##  Gd  :   9   Y:1340     Median :  0.00   Median : 25.00   Median :  0.00  
##  Po  :   7              Mean   : 94.24   Mean   : 46.66   Mean   : 21.95  
##  TA  :1326              3rd Qu.:168.00   3rd Qu.: 68.00   3rd Qu.:  0.00  
##  NA's:  81              Max.   :857.00   Max.   :547.00   Max.   :552.00  
##                                                                           
##    X3SsnPorch      ScreenPorch        PoolArea        PoolQC    
##  Min.   :  0.00   Min.   :  0.00   Min.   :  0.000   Ex  :   2  
##  1st Qu.:  0.00   1st Qu.:  0.00   1st Qu.:  0.000   Fa  :   2  
##  Median :  0.00   Median :  0.00   Median :  0.000   Gd  :   3  
##  Mean   :  3.41   Mean   : 15.06   Mean   :  2.759   NA's:1453  
##  3rd Qu.:  0.00   3rd Qu.:  0.00   3rd Qu.:  0.000              
##  Max.   :508.00   Max.   :480.00   Max.   :738.000              
##                                                                 
##    Fence      MiscFeature    MiscVal             MoSold      
##  GdPrv:  59   Gar2:   2   Min.   :    0.00   Min.   : 1.000  
##  GdWo :  54   Othr:   2   1st Qu.:    0.00   1st Qu.: 5.000  
##  MnPrv: 157   Shed:  49   Median :    0.00   Median : 6.000  
##  MnWw :  11   TenC:   1   Mean   :   43.49   Mean   : 6.322  
##  NA's :1179   NA's:1406   3rd Qu.:    0.00   3rd Qu.: 8.000  
##                           Max.   :15500.00   Max.   :12.000  
##                                                              
##      YrSold        SaleType    SaleCondition    SalePrice     
##  Min.   :2006   WD     :1267   Abnorml: 101   Min.   : 34900  
##  1st Qu.:2007   New    : 122   AdjLand:   4   1st Qu.:129975  
##  Median :2008   COD    :  43   Alloca :  12   Median :163000  
##  Mean   :2008   ConLD  :   9   Family :  20   Mean   :180921  
##  3rd Qu.:2009   ConLI  :   5   Normal :1198   3rd Qu.:214000  
##  Max.   :2010   ConLw  :   5   Partial: 125   Max.   :755000  
##                 (Other):   9
plot(X,Y)

sub_df <- data.frame(house_price$LotArea,house_price$GrLivArea,house_price$GarageArea)

cor(sub_df)
##                        house_price.LotArea house_price.GrLivArea
## house_price.LotArea              1.0000000             0.2631162
## house_price.GrLivArea            0.2631162             1.0000000
## house_price.GarageArea           0.1804028             0.4689975
##                        house_price.GarageArea
## house_price.LotArea                 0.1804028
## house_price.GrLivArea               0.4689975
## house_price.GarageArea              1.0000000
cor.test(house_price$LotArea,house_price$GrLivArea,method = "pearson",conf.level = 0.92)
## 
##  Pearson's product-moment correlation
## 
## data:  house_price$LotArea and house_price$GrLivArea
## t = 10.414, df = 1458, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 92 percent confidence interval:
##  0.2199359 0.3052674
## sample estimates:
##       cor 
## 0.2631162
cor.test(house_price$LotArea,house_price$GarageArea,method = "pearson",conf.level = 0.92)
## 
##  Pearson's product-moment correlation
## 
## data:  house_price$LotArea and house_price$GarageArea
## t = 7.0034, df = 1458, p-value = 3.803e-12
## alternative hypothesis: true correlation is not equal to 0
## 92 percent confidence interval:
##  0.1356921 0.2243801
## sample estimates:
##       cor 
## 0.1804028
cor.test(house_price$GarageArea,house_price$GrLivArea,method = "pearson",conf.level = 0.92)
## 
##  Pearson's product-moment correlation
## 
## data:  house_price$GarageArea and house_price$GrLivArea
## t = 20.276, df = 1458, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 92 percent confidence interval:
##  0.4324608 0.5039965
## sample estimates:
##       cor 
## 0.4689975

All three confidence intervals have p-values less than 0.5 which means that the null hypothesis could be accepted.
For familywise error, it’s the probability of making one or more false discoveries, or type I errors when performing multiple hypotheses tests. This problem can be avoid by ajusting the correlation test to a confident level of higher percentage.



Linear Algebra and Correlation
Invert your 3 x 3 correlation matrix from above. (This is known as the precision matrix and contains variance inflation factors on the diagonal.) Multiply the correlation matrix by the precision matrix, and then multiply the precision matrix by the correlation matrix. Conduct LU decomposition on the matrix.

cormatrix <- cor(sub_df)
cormatrix
##                        house_price.LotArea house_price.GrLivArea
## house_price.LotArea              1.0000000             0.2631162
## house_price.GrLivArea            0.2631162             1.0000000
## house_price.GarageArea           0.1804028             0.4689975
##                        house_price.GarageArea
## house_price.LotArea                 0.1804028
## house_price.GrLivArea               0.4689975
## house_price.GarageArea              1.0000000
precisionmatrix <- solve(cormatrix)
precisionmatrix
##                        house_price.LotArea house_price.GrLivArea
## house_price.LotArea             1.07920917            -0.2469705
## house_price.GrLivArea          -0.24697046             1.3385010
## house_price.GarageArea         -0.07886378            -0.5831994
##                        house_price.GarageArea
## house_price.LotArea               -0.07886378
## house_price.GrLivArea             -0.58319943
## house_price.GarageArea             1.28774631
#multiply the correlation matrix by the precision matrix
round(cormatrix %*% precisionmatrix)
##                        house_price.LotArea house_price.GrLivArea
## house_price.LotArea                      1                     0
## house_price.GrLivArea                    0                     1
## house_price.GarageArea                   0                     0
##                        house_price.GarageArea
## house_price.LotArea                         0
## house_price.GrLivArea                       0
## house_price.GarageArea                      1
#multiply the precision matrix by the correlation matrix
round(precisionmatrix %*% cormatrix)
##                        house_price.LotArea house_price.GrLivArea
## house_price.LotArea                      1                     0
## house_price.GrLivArea                    0                     1
## house_price.GarageArea                   0                     0
##                        house_price.GarageArea
## house_price.LotArea                         0
## house_price.GrLivArea                       0
## house_price.GarageArea                      1
library(Matrix)
## Warning: package 'Matrix' was built under R version 3.4.4
#LU decomposition on correlation matrix
expand(lu(cormatrix))$L
## 3 x 3 Matrix of class "dtrMatrix" (unitriangular)
##      [,1]      [,2]      [,3]     
## [1,] 1.0000000         .         .
## [2,] 0.2631162 1.0000000         .
## [3,] 0.1804028 0.4528838 1.0000000
expand(lu(cormatrix))$U
## 3 x 3 Matrix of class "dtrMatrix"
##      [,1]      [,2]      [,3]     
## [1,] 1.0000000 0.2631162 0.1804028
## [2,]         . 0.9307699 0.4215306
## [3,]         .         . 0.7765505
#LU decomposition on precision matrix
expand(lu(precisionmatrix))$L
## 3 x 3 Matrix of class "dtrMatrix" (unitriangular)
##      [,1]        [,2]        [,3]       
## [1,]  1.00000000           .           .
## [2,] -0.22884393  1.00000000           .
## [3,] -0.07307553 -0.46899748  1.00000000
expand(lu(precisionmatrix))$U
## 3 x 3 Matrix of class "dtrMatrix"
##      [,1]        [,2]        [,3]       
## [1,]  1.07920917 -0.24697046 -0.07886378
## [2,]           .  1.28198329 -0.60124693
## [3,]           .           .  1.00000000



Calculus-based Probability & Statistics Many times, it makes sense to fit a closed form distribution to data. For the first variable that you selected which is skewed to the right, shift it so that the minimum value is above zero as necessary. Then load the MASS package and run fitdistr to fit an exponential probability density function. (See https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/fitdistr.html ). Find the optimal value of ?? for this distribution, and then take 1000 samples from this exponential distribution using this value (e.g., rexp(1000, ??)). Plot a histogram and compare it with a histogram of your original variable. Using the exponential pdf, find the 5th and 95th percentiles using the cumulative distribution function (CDF). Also generate a 95% confidence interval from the empirical data, assuming normality. Finally, provide the empirical 5th percentile and 95th percentile of the data. Discuss.

library(MASS)
## Warning: package 'MASS' was built under R version 3.4.4
optimal_lambda <- fitdistr(house_price$TotalBsmtSF,"exponential")
optimal_lambda$estimate
##         rate 
## 0.0009456896
hist(rexp(1000,optimal_lambda$estimate),breaks = 200,main = "Fitted Exponential PDF",xlim = c(1,quantile(rexp(1000,optimal_lambda$estimate),0.99)))

hist(house_price$TotalBsmtSF,breaks = 400,main = "Observed Basement Area Size",xlim = c(1,quantile(house_price$TotalBsmtSF,0.99)))

#5th and 95th percentiles using CDF
qexp(0.05,rate = optimal_lambda$estimate,lower.tail = TRUE,log.p = FALSE)
## [1] 54.23904
qexp(0.95,rate = optimal_lambda$estimate,lower.tail = TRUE,log.p = FALSE)
## [1] 3167.776
#95% confidence interval from the empirical data (assuming normality)
Bsmt_mean <- mean(house_price$TotalBsmtSF)
Bsmt_sd <- sd(house_price$TotalBsmtSF)
qnorm(0.95,Bsmt_mean,Bsmt_sd)
## [1] 1779.035
#empirical 5th and 95th percentile of the data
quantile(house_price$TotalBsmtSF,c(0.05,0.95))
##     5%    95% 
##  519.3 1753.0

The exponential simulated model doesn’t seem to be a good enough model for this set of house data since the range covers doesn’t fit the one generated for the actual data and relatively large bias exists.



Modeling
Build some type of multiple regression model and submit your model to the competition board. Provide your complete model summary and results with analysis. Report your Kaggle.com user name and score.

#select all the quantitative variables and eliminate the ones with low correlations (<30%)
quantitative <- data.frame(house_price$OverallQual,house_price$YearBuilt,house_price$YearRemodAdd,house_price$MasVnrArea,house_price$BsmtFinSF1,house_price$TotalBsmtSF,house_price$X1stFlrSF,house_price$X2ndFlrSF,house_price$GrLivArea,house_price$FullBath,house_price$TotRmsAbvGrd,house_price$Fireplaces,house_price$GarageCars,house_price$GarageArea,house_price$WoodDeckSF,house_price$OpenPorchSF,house_price$SalePrice) 

#to save space, the result of the following command is not displayed
#cor(quantitative)

#create a linear regression model
m1 <- lm(house_price.SalePrice ~.,data = quantitative)
summary(m1)
## 
## Call:
## lm(formula = house_price.SalePrice ~ ., data = quantitative)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -512233  -17548   -1737   14681  283280 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)              -1.094e+06  1.268e+05  -8.627  < 2e-16 ***
## house_price.OverallQual   1.856e+04  1.174e+03  15.807  < 2e-16 ***
## house_price.YearBuilt     1.638e+02  4.978e+01   3.290 0.001028 ** 
## house_price.YearRemodAdd  3.564e+02  6.208e+01   5.741 1.15e-08 ***
## house_price.MasVnrArea    2.881e+01  6.159e+00   4.678 3.17e-06 ***
## house_price.BsmtFinSF1    1.725e+01  2.596e+00   6.646 4.26e-11 ***
## house_price.TotalBsmtSF   1.165e+01  4.298e+00   2.711 0.006796 ** 
## house_price.X1stFlrSF     2.618e+01  2.082e+01   1.257 0.208871    
## house_price.X2ndFlrSF     1.753e+01  2.048e+01   0.856 0.392000    
## house_price.GrLivArea     2.135e+01  2.035e+01   1.049 0.294370    
## house_price.FullBath     -1.489e+03  2.630e+03  -0.566 0.571228    
## house_price.TotRmsAbvGrd  1.688e+03  1.089e+03   1.550 0.121402    
## house_price.Fireplaces    7.888e+03  1.783e+03   4.423 1.05e-05 ***
## house_price.GarageCars    1.011e+04  2.960e+03   3.414 0.000659 ***
## house_price.GarageArea    1.040e+01  1.005e+01   1.035 0.301006    
## house_price.WoodDeckSF    3.068e+01  8.129e+00   3.774 0.000167 ***
## house_price.OpenPorchSF   7.271e+00  1.572e+01   0.462 0.643861    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 36380 on 1435 degrees of freedom
##   (8 observations deleted due to missingness)
## Multiple R-squared:  0.7918, Adjusted R-squared:  0.7894 
## F-statistic:   341 on 16 and 1435 DF,  p-value: < 2.2e-16
#eliminate variables based on significant level
quantitative2 <- data.frame(house_price$OverallQual,house_price$YearRemodAdd,house_price$MasVnrArea,house_price$BsmtFinSF1,house_price$TotalBsmtSF,house_price$Fireplaces,house_price$GarageCars,house_price$WoodDeckSF,house_price$SalePrice)
colnames(quantitative2) <- c("OverallQual","YearRemodAdd","MasVnrArea","BsmtFinSF1","TotalBsmtSF","Fireplaces","GarageCars","WoodDeckSF","SalePrice")

#create a linear regression model
m2 <- lm(SalePrice ~.,data = quantitative2)
summary(m2)
## 
## Call:
## lm(formula = SalePrice ~ ., data = quantitative2)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -407840  -21443   -2760   16410  363961 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  -8.307e+05  1.210e+05  -6.867 9.70e-12 ***
## OverallQual   2.449e+04  1.183e+03  20.706  < 2e-16 ***
## YearRemodAdd  3.925e+02  6.256e+01   6.273 4.66e-10 ***
## MasVnrArea    4.651e+01  6.602e+00   7.045 2.85e-12 ***
## BsmtFinSF1    1.482e+01  2.752e+00   5.383 8.52e-08 ***
## TotalBsmtSF   2.504e+01  3.290e+00   7.611 4.89e-14 ***
## Fireplaces    1.551e+04  1.849e+03   8.389  < 2e-16 ***
## GarageCars    1.794e+04  1.820e+03   9.855  < 2e-16 ***
## WoodDeckSF    4.464e+01  8.848e+00   5.045 5.12e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 39960 on 1443 degrees of freedom
##   (8 observations deleted due to missingness)
## Multiple R-squared:  0.7474, Adjusted R-squared:  0.746 
## F-statistic: 533.6 on 8 and 1443 DF,  p-value: < 2.2e-16
hist(m2$residuals,breaks = 200)

qqnorm(m2$residuals)
qqline(m2$residuals)

This model, from the perspective of residuals, is not bad since the residuals are normally distributed, with all dependent variables being statistically significant.
The performance of the model will be evaluated by test data.

test_raw <- read.csv("https://raw.githubusercontent.com/xiaoxiaogao-DD/store/master/test.csv")
test_raw[complete.cases(test_raw),]
##  [1] Id            MSSubClass    MSZoning      LotFrontage   LotArea      
##  [6] Street        Alley         LotShape      LandContour   Utilities    
## [11] LotConfig     LandSlope     Neighborhood  Condition1    Condition2   
## [16] BldgType      HouseStyle    OverallQual   OverallCond   YearBuilt    
## [21] YearRemodAdd  RoofStyle     RoofMatl      Exterior1st   Exterior2nd  
## [26] MasVnrType    MasVnrArea    ExterQual     ExterCond     Foundation   
## [31] BsmtQual      BsmtCond      BsmtExposure  BsmtFinType1  BsmtFinSF1   
## [36] BsmtFinType2  BsmtFinSF2    BsmtUnfSF     TotalBsmtSF   Heating      
## [41] HeatingQC     CentralAir    Electrical    X1stFlrSF     X2ndFlrSF    
## [46] LowQualFinSF  GrLivArea     BsmtFullBath  BsmtHalfBath  FullBath     
## [51] HalfBath      BedroomAbvGr  KitchenAbvGr  KitchenQual   TotRmsAbvGrd 
## [56] Functional    Fireplaces    FireplaceQu   GarageType    GarageYrBlt  
## [61] GarageFinish  GarageCars    GarageArea    GarageQual    GarageCond   
## [66] PavedDrive    WoodDeckSF    OpenPorchSF   EnclosedPorch X3SsnPorch   
## [71] ScreenPorch   PoolArea      PoolQC        Fence         MiscFeature  
## [76] MiscVal       MoSold        YrSold        SaleType      SaleCondition
## <0 rows> (or 0-length row.names)
test_raw[is.na(test_raw)] <- 0
## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated

## Warning in `[<-.factor`(`*tmp*`, thisvar, value = 0): invalid factor level,
## NA generated
pred <- predict(m2,test_raw)

write.csv(as.data.frame(cbind(test_raw$Id,pred)),file = "kaggle_sub.csv",quote = FALSE,row.names = FALSE)

Kaggle submission screenshot: