This document contains the final exam for the class Data605 from CUNY . The questions were pretty interesting. I love analysis on home especially that i am looking to find one and the training data from Kaggle gives a nice idea of analysis that you can perform in your local market to get some prediction on these prices. The classed during the spring were extremely useful and i have done my best to use these concepts as much as i can.
Techniques competition. https://www.kaggle.com/c/house-prices-advanced-regression-techniques .
library(ggplot2)
library(MASS)
library(reshape)
library(DT)
library(corrplot)
## corrplot 0.84 loaded
dfHomeData <- read.csv('https://raw.githubusercontent.com/mkunissery/data/master/train.csv')
Pick one of the quantitative independent variables from the training data set (train.csv) , and define that variable as X. Pick SalePrice as the dependent variable, and define it as Y for the next analysis.
#chosen variable
X<-dfHomeData$X1stFlrSF
Y<-dfHomeData$SalePrice
plot(X,Y, col="#7c5244", main="Scatterplot of 1st Flr Sqft. and Sale Price", xlab = "Overall Condition", ylab="Sale Price")
abline(lm(Y~X), col="red", lwd=3) # regression line (y~x)
hist(X, col="#7c4456", main="Histogram of 1st Flr Sqft", xlab = "1st Flr Sqft",border="white")
hist(Y, col="#445a7c", main="Histogram of Sale Price", xlab = "Sale Price")
summary(X)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 334 882 1087 1163 1391 4692
boxplot(X)
summary(Y)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 34900 129975 163000 180921 214000 755000
boxplot(Y)
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.
print("1st Quartile: 1st Floor Sqft.")
XQ1<-quantile(X, probs=0.25) #1st quartile of X variable
XQ1
YQ2<-quantile(Y, probs=0.25) #2nd quartile, or median, of Y variable
print("1st Quartile: Sale Price")
YQ2
n<-(nrow(dfHomeData))
print("Total Number of Homes")
n
overallcond<-as.numeric(dfHomeData$X1stFlrSF)
saleprice<-as.numeric(dfHomeData$SalePrice)
nYQ2<-nrow(subset(dfHomeData,saleprice>YQ2))
print("Total Number of Homes sold at the 1st quartile ")
nYQ2
## [1] "1st Quartile: 1st Floor Sqft."
## 25%
## 882
## [1] "1st Quartile: Sale Price"
## 25%
## 129975
## [1] "Total Number of Homes"
## [1] 1460
## [1] "Total Number of Homes sold at the 1st quartile "
## [1] 1095
p1<-nrow(subset(dfHomeData, overallcond > XQ1 & saleprice>YQ2))/nYQ2
p1
## [1] 0.8273973
p2<-nrow(subset(dfHomeData, overallcond > XQ1 & saleprice>YQ2))/n
p2
## [1] 0.6205479
p3<-nrow(subset(dfHomeData, overallcond < XQ1 & saleprice>YQ2))/nYQ2
p3
## [1] 0.1707763
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.
c1<-nrow(subset(dfHomeData, overallcond <=XQ1 & saleprice<=YQ2))/n
c2<-nrow(subset(dfHomeData, overallcond <=XQ1 & saleprice>YQ2))/n
c4<-nrow(subset(dfHomeData, overallcond >XQ1 & saleprice<=YQ2))/n
c5<-nrow(subset(dfHomeData, overallcond >XQ1 & saleprice>YQ2))/n
dfcounts<-matrix(round(c(c1,c2,c4,c5),3), ncol=2, nrow=2, byrow=TRUE)
colnames(dfcounts)<-c("<=1 quartile",">1 quartile")
rownames(dfcounts)<-c("<=2nd quartile",">2nd quartile")
print("Quartile Matrix Percent")
dfcounts<-as.table(dfcounts)
addmargins(dfcounts)
print (paste0("p(A)*p(B)=", round(c4*c5,5)))
## [1] "Quartile Matrix Percent"
## <=1 quartile >1 quartile Sum
## <=2nd quartile 0.123 0.129 0.252
## >2nd quartile 0.127 0.621 0.748
## Sum 0.250 0.750 1.000
## [1] "p(A)*p(B)=0.07906"
\[ p(AB)=p(X>x \& Y>y)= 0.07906\]
chisq.test(dfcounts, correct=TRUE)
## Warning in chisq.test(dfcounts, correct = TRUE): Chi-squared approximation
## may be incorrect
##
## Pearson's Chi-squared test with Yates' continuity correction
##
## data: dfcounts
## X-squared = 5.4928e-33, df = 1, p-value = 1
Descriptive and Inferential Statistics. Provide univariate descriptive statistics and appropriate plots for both variables. Provide a scatterplot of X and Y. Transform both variables simultaneously using Box-Cox transformations. You might have to research this.
summary(dfHomeData)
## 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
t.test(dfHomeData$X1stFlrSF, dfHomeData$SalePrice)
##
## Welch Two Sample t-test
##
## data: dfHomeData$X1stFlrSF and dfHomeData$SalePrice
## t = -86.459, df = 1459.1, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -183837.0 -175680.2
## sample estimates:
## mean of x mean of y
## 1162.627 180921.196
Using at least three untransformed variables, build a correlation matrix. Invert your correlation matrix. (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.
myvars<-data.frame(dfHomeData$X1stFlrSF, dfHomeData$YearBuilt,dfHomeData$SalePrice)
head(myvars)
## dfHomeData.X1stFlrSF dfHomeData.YearBuilt dfHomeData.SalePrice
## 1 856 2003 208500
## 2 1262 1976 181500
## 3 920 2001 223500
## 4 961 1915 140000
## 5 1145 2000 250000
## 6 796 1993 143000
cor(myvars)
## dfHomeData.X1stFlrSF dfHomeData.YearBuilt
## dfHomeData.X1stFlrSF 1.0000000 0.2819859
## dfHomeData.YearBuilt 0.2819859 1.0000000
## dfHomeData.SalePrice 0.6058522 0.5228973
## dfHomeData.SalePrice
## dfHomeData.X1stFlrSF 0.6058522
## dfHomeData.YearBuilt 0.5228973
## dfHomeData.SalePrice 1.0000000
cor.test(dfHomeData$X1stFlrSF + dfHomeData$YearBuilt, dfHomeData$SalePrice, conf.level = 0.99)
##
## Pearson's product-moment correlation
##
## data: dfHomeData$X1stFlrSF + dfHomeData$YearBuilt and dfHomeData$SalePrice
## t = 31.063, df = 1458, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 99 percent confidence interval:
## 0.5887229 0.6699614
## sample estimates:
## cor
## 0.6310694
t.test(dfHomeData$X1stFlrSF + dfHomeData$YearBuilt, dfHomeData$SalePrice, conf.level = 0.99)
##
## Welch Two Sample t-test
##
## data: dfHomeData$X1stFlrSF + dfHomeData$YearBuilt and dfHomeData$SalePrice
## t = -85.51, df = 1459.1, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 99 percent confidence interval:
## -183149.8 -172424.8
## sample estimates:
## mean of x mean of y
## 3133.895 180921.196
mymx<-as.matrix(cor(myvars))
#correlation matrix
mymx
## dfHomeData.X1stFlrSF dfHomeData.YearBuilt
## dfHomeData.X1stFlrSF 1.0000000 0.2819859
## dfHomeData.YearBuilt 0.2819859 1.0000000
## dfHomeData.SalePrice 0.6058522 0.5228973
## dfHomeData.SalePrice
## dfHomeData.X1stFlrSF 0.6058522
## dfHomeData.YearBuilt 0.5228973
## dfHomeData.SalePrice 1.0000000
#inverse of correlation matrix, precision matrix
ginvmymx<-ginv(mymx)
ginvmymx
## [,1] [,2] [,3]
## [1,] 1.58409531 0.07589894 -0.999415
## [2,] 0.07589894 1.37995056 -0.767556
## [3,] -0.99941495 -0.76755601 2.006851
#corr mat * precision mat
mymxginv<-mymx%*%ginvmymx
round(mymxginv,2)
## [,1] [,2] [,3]
## dfHomeData.X1stFlrSF 1 0 0
## dfHomeData.YearBuilt 0 1 0
## dfHomeData.SalePrice 0 0 1
#precision mat x corr mat
ginvmymx<-ginvmymx%*%mymx
round(ginvmymx,2)
## dfHomeData.X1stFlrSF dfHomeData.YearBuilt dfHomeData.SalePrice
## [1,] 1 0 0
## [2,] 0 1 0
## [3,] 0 0 1
With a 99 percent confidence level, the correlation between Overall condition plus Year Built and Sale Price is estimated to be between 0.47 and 0.57.
#Correlation matrix of all quantitative variables in dataframe
cormatrix<-cor(dfHomeData[,sapply(dfHomeData, is.numeric)])
cordf<-as.data.frame(cormatrix)
#kable(head(cordf[2:10]))
datatable(head(cordf[2:10]))
cordf[cordf == 1] <- NA #drop correlation of 1, diagonals
cordf[abs(cordf) < 0.1] <- NA # drop correlations of less than 0.1
cordf<-as.matrix(cordf)
#cordf
cordf2<- na.omit(melt(cordf))
kable(head(cordf2[order(-abs(cordf2$value)),]))
| X1 | X2 | value | |
|---|---|---|---|
| 1016 | GarageArea | GarageCars | 0.8824754 |
| 1053 | GarageCars | GarageArea | 0.8824754 |
| 632 | TotRmsAbvGrd | GrLivArea | 0.8254894 |
| 891 | GrLivArea | TotRmsAbvGrd | 0.8254894 |
| 470 | X1stFlrSF | TotalBsmtSF | 0.8195300 |
| 507 | TotalBsmtSF | X1stFlrSF | 0.8195300 |
#test of alternate corr approach
#myvars<-data.frame(dfHomeData$X1stFlrSF, dfHomeData$SalePrice)
#head(myvars)
topcors <- cordf2[ which(cordf2$X2=='SalePrice'),]
topcorsdf<-topcors[order(-abs(topcors$value)),]# sort by highest correlations
#topcorsdf
cors1<-data.frame(topcorsdf$X1,topcorsdf$X2,topcorsdf$value)
kable(cors1)
| topcorsdf.X1 | topcorsdf.X2 | topcorsdf.value |
|---|---|---|
| OverallQual | SalePrice | 0.7909816 |
| GrLivArea | SalePrice | 0.7086245 |
| GarageCars | SalePrice | 0.6404092 |
| GarageArea | SalePrice | 0.6234314 |
| TotalBsmtSF | SalePrice | 0.6135806 |
| X1stFlrSF | SalePrice | 0.6058522 |
| FullBath | SalePrice | 0.5606638 |
| TotRmsAbvGrd | SalePrice | 0.5337232 |
| YearBuilt | SalePrice | 0.5228973 |
| YearRemodAdd | SalePrice | 0.5071010 |
| Fireplaces | SalePrice | 0.4669288 |
| BsmtFinSF1 | SalePrice | 0.3864198 |
| WoodDeckSF | SalePrice | 0.3244134 |
| X2ndFlrSF | SalePrice | 0.3193338 |
| OpenPorchSF | SalePrice | 0.3158562 |
| HalfBath | SalePrice | 0.2841077 |
| LotArea | SalePrice | 0.2638434 |
| BsmtFullBath | SalePrice | 0.2271222 |
| BsmtUnfSF | SalePrice | 0.2144791 |
| BedroomAbvGr | SalePrice | 0.1682132 |
| KitchenAbvGr | SalePrice | -0.1359074 |
| EnclosedPorch | SalePrice | -0.1285780 |
| ScreenPorch | SalePrice | 0.1114466 |
par(mar=c(8,8,1,1))
barplot(topcorsdf$value, ylab="Correlation to Sale Price", ylim=c(0,1), col=colors()[c(23,89,12)] , las=2, names.arg=topcorsdf$X1)
Calculus-Based Probability & Statistics. Many times, it makes sense to fit a closed form distribution to data. For your non-transformed independent variable, location shift (if necessary) it so that the minimum value is above zero. Then load the MASS package and run fitdistr to fit a density function of your choice. (See https://stat.ethz.ch/R-manual/R-devel/library/MASS/html/fitdistr.html ). Find the optimal value of the parameters for this distribution, and then take 1000 samples from this distribution (e.g., rexp(1000, ???) for an exponential). Plot a histogram and compare it with a histogram of your non-transformed original variable.
Minimum value is above 0.
#check that min val is not 0
min(dfHomeData$X1stFlrSF)
## [1] 334
fit <- fitdistr(dfHomeData$X1stFlrSF, "exponential")
lambda <- fit$estimate
sampledf <- rexp(1000, lambda)
## Warning in rexp(1000, lambda): '.Random.seed' is not an integer vector but
## of type 'NULL', so ignored
lambda
## rate
## 0.0008601213
sampledf<-data.frame(as.numeric(sampledf))
colnames(sampledf)[1] <- "sample"
str(sampledf)
## 'data.frame': 1000 obs. of 1 variable:
## $ sample: num 2129 955 5182 2460 404 ...
head(sampledf)
## sample
## 1 2129.4791
## 2 954.6035
## 3 5182.0750
## 4 2460.2207
## 5 403.6031
## 6 776.7069
hist(sampledf$sample, col="#5e4372", main="Histogram of Exponential Distribution", xlab = "1st floor Sqft.", breaks=30)
Build some type of 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.
fit <- lm(dfHomeData$SalePrice ~ dfHomeData$X1stFlrSF + dfHomeData$TotalBsmtSF + dfHomeData$GarageCars + dfHomeData$OverallQual, data=dfHomeData)
summary(fit)
##
## Call:
## lm(formula = dfHomeData$SalePrice ~ dfHomeData$X1stFlrSF + dfHomeData$TotalBsmtSF +
## dfHomeData$GarageCars + dfHomeData$OverallQual, data = dfHomeData)
##
## Residuals:
## Min 1Q Median 3Q Max
## -360017 -23751 -1913 18259 356074
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.068e+05 5.316e+03 -20.092 < 2e-16 ***
## dfHomeData$X1stFlrSF 4.230e+01 5.053e+00 8.372 < 2e-16 ***
## dfHomeData$TotalBsmtSF 1.280e+01 4.595e+00 2.786 0.00541 **
## dfHomeData$GarageCars 2.093e+04 1.894e+03 11.049 < 2e-16 ***
## dfHomeData$OverallQual 3.083e+04 1.082e+03 28.486 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 42190 on 1455 degrees of freedom
## Multiple R-squared: 0.7187, Adjusted R-squared: 0.7179
## F-statistic: 929.2 on 4 and 1455 DF, p-value: < 2.2e-16
\[ SalePrice=42.3*X1stFlrSF + 12.8*TotalBsmtSF + 20942.8*GarageCars + 30830*OverallQual - 106800 \] # Scatter Plots
par(mfrow=c(2,2))
X1<-dfHomeData$X1stFlrSF
X2<-dfHomeData$TotalBsmtSF
X3<-dfHomeData$GarageCars
X4<-dfHomeData$OverallQual
Y<-dfHomeData$SalePrice
plot(X1,Y, col="#5e4372", main="1stFloorSqFt", ylab="Sale Price")
abline(lm(Y~X1), col="red", lwd=3) # regression line (y~x)
plot(X2,Y, col="#8aeae0", main="TotalBsmtSF", ylab="Sale Price")
abline(lm(Y~X2), col="red", lwd=3) # regression line (y~x)
plot(X3,Y, col="#60663b", main="No.OfCarsInGarage", ylab="Sale Price")
abline(lm(Y~X3), col="red", lwd=3) # regression line (y~x)
plot(X4,Y, col="#8096b7", main="Overall Quality", ylab="Sale Price")
abline(lm(Y~X4), col="red", lwd=3) # regression line (y~x)
Load test data set and create calculated column using equation for multiple linear regression. Select required columns and export to csv for contest entry.
dftest <- read.csv('https://raw.githubusercontent.com/mkunissery/data/master/modeldata.csv')
SalePrice=42.3*dfHomeData$X1stFlrSF + 12.8*dfHomeData$TotalBsmtSF + 20942.8*dfHomeData$GarageCars + 30830*dfHomeData$OverallQual - 106800
dftest<-dftest[,c("Id","X1stFlrSF","TotalBsmtSF","GarageCars","OverallQual")]
kable(head(dftest))
| Id | X1stFlrSF | TotalBsmtSF | GarageCars | OverallQual |
|---|---|---|---|---|
| 1461 | 896 | 882 | 1 | 5 |
| 1462 | 1329 | 1329 | 1 | 6 |
| 1463 | 928 | 928 | 2 | 5 |
| 1464 | 926 | 926 | 2 | 6 |
| 1465 | 1280 | 1280 | 2 | 8 |
| 1466 | 763 | 763 | 2 | 6 |
submission <- cbind(dftest$Id,SalePrice)
## Warning in cbind(dftest$Id, SalePrice): number of rows of result is not a
## multiple of vector length (arg 1)
colnames(submission)[1] <- "Id"
submission[submission<0] <- median(SalePrice)
submission<-as.data.frame(submission[1:1459,])
submission[is.na(submission)] <- 0
head(submission)
## Id SalePrice
## 1 1461 198061.2
## 2 1462 189601.8
## 3 1463 201587.6
## 4 1464 222165.5
## 5 1465 265757.9
## 6 1466 133095.2
kable(head(submission))
| Id | SalePrice |
|---|---|
| 1461 | 198061.2 |
| 1462 | 189601.8 |
| 1463 | 201587.6 |
| 1464 | 222165.5 |
| 1465 | 265757.9 |
| 1466 | 133095.2 |
write.csv(submission, file = 'C:/Temp/submission1.csv', quote=FALSE, row.names=FALSE)