Data Source: https://www.kaggle.com/c/house-prices-advanced-regression-techniques

#Download required packages
suppressWarnings(suppressMessages(library(RCurl)))
suppressWarnings(suppressMessages(library(dplyr)))
suppressWarnings(suppressMessages(library(XML)))
suppressWarnings(suppressMessages(library(Hmisc))) # correlation
suppressWarnings(suppressMessages(library(ggplot2)))
suppressWarnings(suppressMessages(library(Matrix)))

The Data structure

Load data and check the data structure

#Prepare raw data sets: read in csv or txt files into R 
housing_data <- read.csv(file="C:/Users/johnw/Dropbox/MSDA/DATA605/final/train.csv", header=TRUE, sep=",")

summary(housing_data)
##        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

Each house is independant from each other. Ground Living area is the most major concern to buyer of house. Sales price and Ground Living Area.

X = Ground living rea Y = Sales price

X = housing_data$GrLivArea
Y = housing_data$SalePrice

check skewness of X skewed at right

library(moments)

label_X = paste("The skewness of X: ", skewness(X)) #get skewness

hist(X, main=label_X)

The data is skewed to the right. https://www.r-bloggers.com/measures-of-skewness-and-kurtosis/

#let x be housing_data$GrLivArea
#histogram :check distribution of x and skewed to the right
hist(housing_data$GrLivArea)

X <- housing_data$GrLivArea

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 as shown below.

#first quantile of X and Y
x <- unname(quantile(X))[2]
y <- unname(quantile(Y))[2]

paste("The first quantile of X:", x)
## [1] "The first quantile of X: 1129.5"
paste("The first quantile of Y:", y)
## [1] "The first quantile of Y: 129975"
  1. P(X>x | Y>y): The probablity of X where ground living area is larger than first quantile given the probability of Y where sales price is larger than first quantitle.
# subset of housing data where Y > y
subset_Y_gt_y = subset( housing_data,  housing_data$SalePrice > y)

subset_X_gt_x_where_Y_gt_y = subset(subset_Y_gt_y, subset_Y_gt_y$GrLivArea > x)


pct_X_gt_x_where_Y_gt_y = nrow(subset_X_gt_x_where_Y_gt_y)/nrow(housing_data)

paste(" P(X>x | Y>y): ", pct_X_gt_x_where_Y_gt_y)
## [1] " P(X>x | Y>y):  0.653424657534247"
  1. P(X>x, Y>y): The probablity of X and Y where ground living area and sales price are larger than first quantile.
pct_X_gt_x_and_Y_gt_y =  nrow(subset(housing_data, (housing_data$SalePrice >y) & (housing_data$GrLivArea >x)))/nrow(housing_data)

paste("P(X>x, Y>y)",pct_X_gt_x_and_Y_gt_y
      )
## [1] "P(X>x, Y>y) 0.653424657534247"
  1. P(Xy): The probablity of X where ground living area is lesser than first quanile given the probability of Y where sales price is larger than first quantitle.
subset_X_lt_x_where_Y_gt_y = subset(subset_Y_gt_y, subset_Y_gt_y$GrLivArea <= x)

pct_X_lt_x_where_Y_gt_y =nrow(subset_X_lt_x_where_Y_gt_y)/nrow(housing_data) 
paste(" P(X<x | Y>y): ", pct_X_lt_x_where_Y_gt_y)
## [1] " P(X<x | Y>y):  0.0965753424657534"

For each case in the table:

P(Xy)

P(X>x, Y<=y)

P(X>x, Y>y)

pct_X_lte_x_and_Y_lte_y = nrow(subset(housing_data, housing_data$GrLivArea <= x & housing_data$SalePrice <=y)) / nrow(housing_data)

pct_X_lte_x_and_Y_gt_y = nrow(subset(housing_data, housing_data$GrLivArea <= x & housing_data$SalePrice >y)) / nrow(housing_data)

pct_X_gt_x_and_Y_lte_y = nrow(subset(housing_data, housing_data$GrLivArea > x & housing_data$SalePrice <=y)) / nrow(housing_data)

pct_X_gt_x_and_Y_gt_y = nrow(subset(housing_data, housing_data$GrLivArea > x & housing_data$SalePrice >y)) / nrow(housing_data)


#Creat and fill the table
cross_table <- matrix(
  c(pct_X_lte_x_and_Y_lte_y,pct_X_lte_x_and_Y_gt_y, (pct_X_lte_x_and_Y_lte_y+ pct_X_lte_x_and_Y_gt_y) ,     pct_X_gt_x_and_Y_lte_y,pct_X_gt_x_and_Y_gt_y,(pct_X_gt_x_and_Y_lte_y+pct_X_gt_x_and_Y_gt_y),
(pct_X_lte_x_and_Y_lte_y + pct_X_gt_x_and_Y_lte_y),(pct_X_lte_x_and_Y_gt_y + pct_X_gt_x_and_Y_gt_y), 
(pct_X_lte_x_and_Y_lte_y + pct_X_lte_x_and_Y_gt_y + pct_X_gt_x_and_Y_lte_y + pct_X_gt_x_and_Y_gt_y )
),ncol=3,byrow=TRUE)
colnames(cross_table) <- c("<=1st quartile",">1st quartile","Total")
rownames(cross_table) <- c("<= 1st q",">1st q","Total")
cross_table <- as.table(cross_table)
cross_table
##          <=1st quartile >1st quartile      Total
## <= 1st q     0.15342466    0.09657534 0.25000000
## >1st q       0.09657534    0.65342466 0.75000000
## Total        0.25000000    0.75000000 1.00000000

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.

P_A = nrow(subset(housing_data, housing_data$GrLivArea > x)) / nrow(housing_data)

P_B = nrow(subset(housing_data, housing_data$SalePrice > y)) / nrow(housing_data)

paste("P(A)P(B)", P_A*P_B)
## [1] "P(A)P(B) 0.5625"

P(AB) is 0.65 while to P(A)P(B) = 0.5625. P(AB) does not equal to P(A)P(B)

Chi-square test: Null hypothesis(H0): the X and Y variables are independent. Alternative hypothesis(H1): X and Y variables are dependent

housing_data_grLvingarea_salesPrice <- subset(housing_data, select=c("GrLivArea", "SalePrice"))
chisq.test(housing_data_grLvingarea_salesPrice)
## 
##  Pearson's Chi-squared test
## 
## data:  housing_data_grLvingarea_salesPrice
## X-squared = 200960, df = 1459, p-value < 2.2e-16

Since the p-value is very small, we can reject the H0. And X and Y variable are dependent.

Descriptive and Inferential Statistics

Provide univariate descriptive statistics and appropriate plots for the training data set. Provide a scatterplot of X and Y.

plot(X, Y, main="Housing Data", 
    xlab="GrLivArea", ylab="Sale Price", pch=19)

Derive a correlation matrix for any THREE quantitative variables in the dataset.

TotalBsmtSF: Total square feet of basement area GrLivArea: Above grade (ground) living area square feet SalePrice: Sale Price

https://www.r-bloggers.com/create-a-correlation-matrix-in-r/

housing_data_totalBasementSF_GrLivingArea_SalesPrice = subset(housing_data, select=c("TotalBsmtSF","GrLivArea", "SalePrice" ))

corr_matrix = cor(housing_data_totalBasementSF_GrLivingArea_SalesPrice)

round(corr_matrix,2)
##             TotalBsmtSF GrLivArea SalePrice
## TotalBsmtSF        1.00      0.45      0.61
## GrLivArea          0.45      1.00      0.71
## SalePrice          0.61      0.71      1.00

Test the hypotheses that the correlations between each pairwise set of variables is 0 and provide a 92% confidence interval.

https://www.statmethods.net/stats/correlations.html

#Hypotheses test in 92% confidence interval 
#H0: correlations between TotalBsmtSF and GrLivArea set of variables = 0. 
#H1: correlations between TotalBsmtSF and GrLivArea set of variables <> 0.
cor.test(~TotalBsmtSF+GrLivArea , data = housing_data_totalBasementSF_GrLivingArea_SalesPrice,conf.level=0.92 )
## 
##  Pearson's product-moment correlation
## 
## data:  TotalBsmtSF and GrLivArea
## t = 19.503, df = 1458, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 92 percent confidence interval:
##  0.4177447 0.4904754
## sample estimates:
##       cor 
## 0.4548682
#Hypotheses test in 92% confidence interval
#H0: correlations between TotalBsmtSF and SalePrice set of variables = 0. 
#H1: correlations between TotalBsmtSF and SalePrice set of variables <> 0.
cor.test(~TotalBsmtSF+SalePrice , data = housing_data_totalBasementSF_GrLivingArea_SalesPrice,conf.level=0.92 )
## 
##  Pearson's product-moment correlation
## 
## data:  TotalBsmtSF and SalePrice
## t = 29.671, df = 1458, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 92 percent confidence interval:
##  0.5841762 0.6413763
## sample estimates:
##       cor 
## 0.6135806
#Hypotheses test in 92% confidence interval
#H0: correlations between GrLivArea and SalePrice set of variables = 0. 
#H1: correlations between GrLivArea and SalePrice set of variables <> 0.
cor.test(~GrLivArea+SalePrice , data = housing_data_totalBasementSF_GrLivingArea_SalesPrice,conf.level=0.92 )
## 
##  Pearson's product-moment correlation
## 
## data:  GrLivArea and SalePrice
## t = 38.348, df = 1458, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 92 percent confidence interval:
##  0.6850407 0.7307245
## sample estimates:
##       cor 
## 0.7086245

Discuss the meaning of your analysis. Would you be worried about familywise error? Why or why not?

From all the p-values for correlation tests, they are all very small and we can reject the null hypothese.

I am not worry about the familywise error because these variables are all realted to each other.

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.

https://www.statmethods.net/advstats/matrix.html

#precision matrix = Invert corr matrix
paste("Precision Matrix")
## [1] "Precision Matrix"
round(solve(corr_matrix),4 )
##             TotalBsmtSF GrLivArea SalePrice
## TotalBsmtSF      1.6059   -0.0647   -0.9395
## GrLivArea       -0.0647    2.0112   -1.3855
## SalePrice       -0.9395   -1.3855    2.5582
paste("Multiply the correlation matrix by the precision matrix")
## [1] "Multiply the correlation matrix by the precision matrix"
round(corr_matrix %*% solve(corr_matrix) ,4)
##             TotalBsmtSF GrLivArea SalePrice
## TotalBsmtSF           1         0         0
## GrLivArea             0         1         0
## SalePrice             0         0         1
paste("Multiply the correlation matrix by the precision matrix, and then multiply the precision matrix by the correlation matrix")
## [1] "Multiply the correlation matrix by the precision matrix, and then multiply the precision matrix by the correlation matrix"
round(corr_matrix %*% solve(corr_matrix) %*% corr_matrix ,4)
##             TotalBsmtSF GrLivArea SalePrice
## TotalBsmtSF      1.0000    0.4549    0.6136
## GrLivArea        0.4549    1.0000    0.7086
## SalePrice        0.6136    0.7086    1.0000
paste("LU decomposition")
## [1] "LU decomposition"
lum <- lu(corr_matrix)
paste("Lower triangle")
## [1] "Lower triangle"
#as.data.frame(as.table(lum$L))

paste("Upper triangle")
## [1] "Upper triangle"
#as.data.frame(as.table(lum$U))

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.

#load MASS 
suppressWarnings(suppressMessages(library(MASS)))

#fitting an exponential probability density function
exp_x = fitdistr(X, "exponential") 

lamda = exp_x$estimate

sample_exp_x = rexp(1000,lamda)

hist(sample_exp_x)

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.

For most of the house buyer, beside location, the size of the house and number of bathroom are the considered the most.

Below are the multiple regression model with living area and bathrooms

lm_multi_regression <- lm(SalePrice~GrLivArea + TotalBsmtSF + FullBath + HalfBath, data = housing_data)

summary(lm_multi_regression)
## 
## Call:
## lm(formula = SalePrice ~ GrLivArea + TotalBsmtSF + FullBath + 
##     HalfBath, data = housing_data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -629227  -21306   -1461   18224  272570 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -27199.116   4412.919  -6.164 9.19e-10 ***
## GrLivArea       53.277      3.757  14.182  < 2e-16 ***
## TotalBsmtSF     71.975      3.353  21.465  < 2e-16 ***
## FullBath     27706.831   2974.310   9.315  < 2e-16 ***
## HalfBath     20660.030   2906.519   7.108 1.84e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 47830 on 1455 degrees of freedom
## Multiple R-squared:  0.6385, Adjusted R-squared:  0.6375 
## F-statistic: 642.4 on 4 and 1455 DF,  p-value: < 2.2e-16

All the p-values are very small, so that we know they have strong correlation for sales price.

 estimate_sales_price <- function ( g,b,fb,hb ) {
   if (is.na(b)) { b = 0}
   
   return ((-27199.116 + 53.277*g + 71.975*b + 27706.831*fb + 20660.030*hb))
   
 }

housing_data_test <- read.csv(file="C:/Users/johnw/Dropbox/MSDA/DATA605/final/test.csv", header=TRUE, sep=",")

housing_data_test$SalePrice <- housing_data_test$Id #use id as place holder

for(row in 1:nrow(housing_data_test)) {
  housing_data_test[row,]$SalePrice <-  estimate_sales_price( housing_data_test[row,]$GrLivArea, housing_data_test[row,]$TotalBsmtSF, housing_data_test[row,]$FullBath, housing_data_test[row,]$HalfBath)
}

predict_id_sales_price <- subset(housing_data_test, select=c("Id", "SalePrice"))

write.csv(predict_id_sales_price, "C:/Users/johnw/Dropbox/MSDA/DATA605/final/predict_saleprice.csv")

Kaggle submission.

Record ID of dataframe in the submission file is removed manually before uploading.

https://www.kaggle.com/c/house-prices-advanced-regression-techniques/leaderboard

Username: jwdata