---
title: "house price"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r}

train<-read.csv("../input/house-prices-advanced-regression-techniques/train.csv")
test<-read.csv("../input/house-prices-advanced-regression-techniques/test.csv")
install.packages("AICcmodavg")
library(tidyverse)
library(dplyr)
library(caret)
library(ggplot2)
library(AICcmodavg)
library(gridExtra)
```

```{r}
#The number of NA's in raw data is going to be demostrated for each variable.
data_na<-apply(train, 2, function(x) sum(is.na(x))) %>% sort 
data_na

```

```{r}
train<-subset(train,select=-c(GarageYrBlt))
test<-subset(test,select=-c(GarageYrBlt))
```


PoolQC NA's

```{r}
train$PoolQC<-factor(train$PoolQC)
levels <- levels(train$PoolQC)
levels[length(levels) + 1] <- "Np"
train$PoolQC <- factor(train$PoolQC, levels = levels)
train$PoolQC[is.na(train$PoolQC)] <- "Np"

test$PoolQC<-factor(test$PoolQC)
levels <- levels(test$PoolQC)
levels[length(levels) + 1] <- "Np"
test$PoolQC <- factor(test$PoolQC, levels = levels)
test$PoolQC[is.na(test$PoolQC)] <- "Np"
```

MiscFeature NA's

```{r}

train$MiscFeature<-factor(train$MiscFeature)
levels <- levels(train$MiscFeature)
levels[length(levels) + 1] <- "None"
train$MiscFeature <- factor(train$MiscFeature, levels = levels)
train$MiscFeature[is.na(train$MiscFeature)] <- "None"

test$MiscFeature<-factor(test$MiscFeature)
levels <- levels(test$MiscFeature)
levels[length(levels) + 1] <- "None"
test$MiscFeature <- factor(test$MiscFeature, levels = levels)
test$MiscFeature[is.na(test$MiscFeature)] <- "None"
```

Alley NA's
```{r}

train$Alley<-factor(train$Alley)
levels <- levels(train$Alley)
levels[length(levels) + 1] <- "Naa"
train$Alley <- factor(train$Alley, levels = levels)
train$Alley[is.na(train$Alley)] <- "Naa"

test$Alley<-factor(test$Alley)
levels <- levels(test$Alley)
levels[length(levels) + 1] <- "Naa"
test$Alley <- factor(test$Alley, levels = levels)
test$Alley[is.na(test$Alley)] <- "Naa"
```


Fence NA's
```{r}

train$Fence<-factor(train$Fence)
levels <- levels(train$Fence)
levels[length(levels) + 1] <- "Nofence"
train$Fence <- factor(train$Fence, levels = levels)
train$Fence[is.na(train$Fence)] <- "Nofence"

test$Fence<-factor(test$Fence)
levels <- levels(test$Fence)
levels[length(levels) + 1] <- "Nofence"
test$Fence <- factor(test$Fence, levels = levels)
test$Fence[is.na(test$Fence)] <- "Nofence"
```

FireplaceQu NA's
```{r}

train$FireplaceQu<-factor(train$FireplaceQu)
levels <- levels(train$FireplaceQu)
levels[length(levels) + 1] <- "NoFireplace"
train$FireplaceQu <- factor(train$FireplaceQu, levels = levels)
train$FireplaceQu[is.na(train$FireplaceQu)] <- "NoFireplace"

test$FireplaceQu<-factor(test$FireplaceQu)
levels <- levels(test$FireplaceQu)
levels[length(levels) + 1] <- "NoFireplace"
test$FireplaceQu <- factor(test$FireplaceQu, levels = levels)
test$FireplaceQu[is.na(test$FireplaceQu)] <- "NoFireplace"

```

GarageCond NA's
```{r}

train$GarageCond<-factor(train$GarageCond)
levels <- levels(train$GarageCond)
levels[length(levels) + 1] <- "NoGarage"
train$GarageCond <- factor(train$GarageCond, levels = levels)
train$GarageCond[is.na(train$GarageCond)] <- "NoGarage"

test$GarageCond<-factor(test$GarageCond)
levels <- levels(test$GarageCond)
levels[length(levels) + 1] <- "NoGarage"
test$GarageCond <- factor(test$GarageCond, levels = levels)
test$GarageCond[is.na(test$GarageCond)] <- "NoGarage"
```


GarageQual NA's
```{r}

train$GarageQual<-factor(train$GarageQual)
levels <- levels(train$GarageQual)
levels[length(levels) + 1] <- "NoGarage"
train$GarageQual <- factor(train$GarageQual, levels = levels)
train$GarageQual[is.na(train$GarageQual)] <- "NoGarage"

test$GarageQual<-factor(test$GarageQual)
levels <- levels(test$GarageQual)
levels[length(levels) + 1] <- "NoGarage"
test$GarageQual <- factor(test$GarageQual, levels = levels)
test$GarageQual[is.na(test$GarageQual)] <- "NoGarage"
```


GarageFinish NA's
```{r}

train$GarageFinish<-factor(train$GarageFinish)
levels <- levels(train$GarageFinish)
levels[length(levels) + 1] <- "NoGarage"
train$GarageFinish <- factor(train$GarageFinish, levels = levels)
train$GarageFinish[is.na(train$GarageFinish)] <- "NoGarage"

test$GarageFinish<-factor(test$GarageFinish)
levels <- levels(test$GarageFinish)
levels[length(levels) + 1] <- "NoGarage"
test$GarageFinish <- factor(test$GarageFinish, levels = levels)
test$GarageFinish[is.na(test$GarageFinish)] <- "NoGarage"
```

GarageType NA's
```{r}

train$GarageType<-factor(train$GarageType)
levels <- levels(train$GarageType)
levels[length(levels) + 1] <- "NoGarage"
train$GarageType <- factor(train$GarageType, levels = levels)
train$GarageType[is.na(train$GarageType)] <- "NoGarage"

test$GarageType<-factor(test$GarageType)
levels <- levels(test$GarageType)
levels[length(levels) + 1] <- "NoGarage"
test$GarageType <- factor(test$GarageType, levels = levels)
test$GarageType[is.na(test$GarageType)] <- "NoGarage"

```

BsmtFinType2 NA's
```{r}

train$BsmtFinType2<-factor(train$BsmtFinType2)
levels <- levels(train$BsmtFinType2)
levels[length(levels) + 1] <- "NoBasement"
train$BsmtFinType2 <- factor(train$BsmtFinType2, levels = levels)
train$BsmtFinType2[is.na(train$BsmtFinType2)] <- "NoBasement"

test$BsmtFinType2<-factor(test$BsmtFinType2)
levels <- levels(test$BsmtFinType2)
levels[length(levels) + 1] <- "NoBasement"
test$BsmtFinType2 <- factor(test$BsmtFinType2, levels = levels)
test$BsmtFinType2[is.na(test$BsmtFinType2)] <- "NoBasement"

```


BsmtExposure NA's
```{r}

train$BsmtExposure<-factor(train$BsmtExposure)
levels <- levels(train$BsmtExposure)
levels[length(levels) + 1] <- "NoBasement"
train$BsmtExposure <- factor(train$BsmtExposure, levels = levels)
train$BsmtExposure[is.na(train$BsmtExposure)] <- "NoBasement"

test$BsmtExposure<-factor(test$BsmtExposure)
levels <- levels(test$BsmtExposure)
levels[length(levels) + 1] <- "NoBasement"
test$BsmtExposure <- factor(test$BsmtExposure, levels = levels)
test$BsmtExposure[is.na(test$BsmtExposure)] <- "NoBasement"


```


BsmtFinType1 NA's

```{r}

train$BsmtFinType1<-factor(train$BsmtFinType1)
levels <- levels(train$BsmtFinType1)
levels[length(levels) + 1] <- "NoBasement"
train$BsmtFinType1 <- factor(train$BsmtFinType1, levels = levels)
train$BsmtFinType1[is.na(train$BsmtFinType1)] <- "NoBasement"

test$BsmtFinType1<-factor(test$BsmtFinType1)
levels <- levels(test$BsmtFinType1)
levels[length(levels) + 1] <- "NoBasement"
test$BsmtFinType1 <- factor(test$BsmtFinType1, levels = levels)
test$BsmtFinType1[is.na(test$BsmtFinType1)] <- "NoBasement"

```


BsmtCond NA's
```{r}

train$BsmtCond<-factor(train$BsmtCond)
levels <- levels(train$BsmtCond)
levels[length(levels) + 1] <- "NoBasement"
train$BsmtCond <- factor(train$BsmtCond, levels = levels)
train$BsmtCond[is.na(train$BsmtCond)] <- "NoBasement"

test$BsmtCond<-factor(test$BsmtCond)
levels <- levels(test$BsmtCond)
levels[length(levels) + 1] <- "NoBasement"
test$BsmtCond <- factor(test$BsmtCond, levels = levels)
test$BsmtCond[is.na(test$BsmtCond)] <- "NoBasement"
```


BsmtQual NA's
```{r}

train$BsmtQual<-factor(train$BsmtQual)
levels <- levels(train$BsmtQual)
levels[length(levels) + 1] <- "NoBasement"
train$BsmtQual <- factor(train$BsmtQual, levels = levels)
train$BsmtQual[is.na(train$BsmtQual)] <- "NoBasement"

test$BsmtQual<-factor(test$BsmtQual)
levels <- levels(test$BsmtQual)
levels[length(levels) + 1] <- "NoBasement"
test$BsmtQual <- factor(test$BsmtQual, levels = levels)
test$BsmtQual[is.na(test$BsmtQual)] <- "NoBasement"

```


MasVnrArea NA's
```{r}


train$MasVnrArea[is.na(train$MasVnrArea)]<-0

test$MasVnrArea[is.na(test$MasVnrArea)]<-0
```


MasVnrType NA's
```{r}

train$MasVnrType<-factor(train$MasVnrType)
levels <- levels(train$MasVnrType)
train$MasVnrType <- factor(train$MasVnrType, levels = levels)
train$MasVnrType[is.na(train$MasVnrType)] <- "None"

test$MasVnrType<-factor(test$MasVnrType)
levels <- levels(test$MasVnrType)
test$MasVnrType <- factor(test$MasVnrType, levels = levels)
test$MasVnrType[is.na(test$MasVnrType)] <- "None"
```

Electrical NA's
```{r}

train$Electrical<-factor(train$Electrical)
levels <- levels(train$Electrical)
train$Electrical <- factor(train$Electrical, levels = levels)
train$Electrical[is.na(train$Electrical)] <- "SBrkr"

test$Electrical<-factor(test$Electrical)
levels <- levels(test$Electrical)
test$Electrical <- factor(test$Electrical, levels = levels)
test$Electrical[is.na(test$Electrical)] <- "SBrkr"
```


MSZoning NA's
```{r}

test$MSZoning<-factor(test$MSZoning)
levels <- levels(test$MSZoning)
test$MSZoning <- factor(test$MSZoning, levels = levels)
test$MSZoning[is.na(test$MSZoning)] <- names(which.max(table(test$MSZoning)))
```

Functional NA's
```{r}

test$Functional<-factor(test$Functional)
levels <- levels(test$Functional)
test$Functional <- factor(test$Functional, levels = levels)
test$Functional[is.na(test$Functional)] <- names(which.max(table(test$Functional)))
```

BsmtHalfBath NA's
```{r}

train$BsmtHalfBath[is.na(train$BsmtHalfBath)]<-0

test$BsmtHalfBath[is.na(test$BsmtHalfBath)]<-0
```


BsmtFullBath NA's
```{r}

train$BsmtFullBath[is.na(train$BsmtFullBath)]<-0

test$BsmtFullBath[is.na(test$BsmtFullBath)]<-0
```

Utilities NA's
```{r}

test$Utilities<-factor(test$Utilities)
levels <- levels(test$Utilities)
test$Utilities <- factor(test$Utilities, levels = levels)
test$Utilities[is.na(test$Utilities)] <- names(which.max(table(test$Utilities)))
```


```{r}
#SaleType NA's

test$SaleType<-factor(test$SaleType)
levels <- levels(test$SaleType)
test$SaleType <- factor(test$SaleType, levels = levels)
test$SaleType[is.na(test$SaleType)] <- names(which.max(table(test$SaleType)))
```

GarageArea NA's
```{r}

train$GarageArea[is.na(train$GarageArea)]<-0

test$GarageArea[is.na(test$GarageArea)]<-0
```

GarageCars NA's
```{r}

train$GarageCars[is.na(train$GarageCars)]<-0

test$GarageCars[is.na(test$GarageCars)]<-0
```

KitchenQual NA's
```{r}

test$KitchenQual<-factor(test$KitchenQual)
levels <- levels(test$KitchenQual)
test$KitchenQual <- factor(test$KitchenQual, levels = levels)
test$KitchenQual[is.na(test$KitchenQual)] <- names(which.max(table(test$KitchenQual)))
```

TotalBsmtSF NA's
```{r}

train$TotalBsmtSF[is.na(train$TotalBsmtSF)]<-0

test$TotalBsmtSF[is.na(test$TotalBsmtSF)]<-0
```

BsmtUnfSF NA's
```{r}

train$BsmtUnfSF[is.na(train$BsmtUnfSF)]<-0

test$BsmtUnfSF[is.na(test$BsmtUnfSF)]<-0
```


BsmtFinSF2 NA's
```{r}

train$BsmtFinSF2[is.na(train$BsmtFinSF2)]<-0

test$BsmtFinSF2[is.na(test$BsmtFinSF2)]<-0

```

BsmtFinSF1 NA's
```{r}

train$BsmtFinSF1[is.na(train$BsmtFinSF1)]<-0

test$BsmtFinSF1[is.na(test$BsmtFinSF1)]<-0
```
Exterior2nd NA's
```{r}


test$Exterior2nd<-factor(test$Exterior2nd)
levels <- levels(test$Exterior2nd)
test$Exterior2nd <- factor(test$Exterior2nd, levels = levels)
test$Exterior2nd[is.na(test$Exterior2nd)] <- names(which.max(table(test$Exterior2nd)))
```

Exterior2nd NA's
```{r}


test$Exterior1st<-factor(test$Exterior1st)
levels <- levels(test$Exterior1st)
test$Exterior1st <- factor(test$Exterior1st, levels = levels)
test$Exterior1st[is.na(test$Exterior1st)] <- names(which.max(table(test$Exterior1st)))


```

LotFront NA's
```{r}


lotfront_train<-train[!is.na(train$LotFrontage),]

lotfront_train<-subset(lotfront_train,select=c(LotArea,LotConfig,LotShape,LotFrontage))

lotfront_train$LotArea<-sqrt(lotfront_train$LotArea)

lotfront_train_predict<-train[is.na(train$LotFrontage),]

lotfront_train_predict<-subset(lotfront_train_predict,select=c(LotArea,LotConfig,LotShape,LotFrontage))

lotfront_train_predict$LotArea<-sqrt(lotfront_train_predict$LotArea)


lotfront_test<-test[!is.na(test$LotFrontage),]

lotfront_test<-subset(lotfront_test,select=c(LotArea,LotConfig,LotShape,LotFrontage))

lotfront_test$LotArea<-sqrt(lotfront_test$LotArea)

lotfront_test_predict<-test[is.na(test$LotFrontage),]

lotfront_test_predict<-subset(lotfront_test_predict,select=c(LotArea,LotConfig,LotShape,LotFrontage))

lotfront_test_predict$LotArea<-sqrt(lotfront_test_predict$LotArea)

#Train LotFrontage Filling

ctrl <- trainControl(method = "cv", 
                     number = 5)

model_train <- train(LotFrontage ~., data = lotfront_train, method ='lm', preProcess=c("center","scale"), trControl = ctrl)


lotfrontage_train_prediction<-predict(model_train, newdata=lotfront_train_predict)


train[is.na(train$LotFrontage),c("LotFrontage")]<-lotfrontage_train_prediction
summary(model_train)

#Test LotFronmtage Filling

ctrl <- trainControl(method = "cv", 
                     number = 5)

model_train2 <- train(LotFrontage ~., data = lotfront_test, method ='lm', preProcess=c("center","scale"), trControl = ctrl)


lotfrontage_test_prediction<-predict(model_train2, newdata=lotfront_test_predict)


test[is.na(test$LotFrontage),c("LotFrontage")]<-lotfrontage_test_prediction


summary(model_train2)
```



```{r}
#Turn categoric variables to factor variables

categoric<- names(which(sapply(train, is.character)))
factor<- names(which(sapply(train, is.factor)))
train$MSSubClass<-factor(train$MSSubClass)
numeric<- names(which(sapply(train, is.numeric)))

train[sapply(train, is.character)] <- lapply(train[sapply(train, is.character)],as.factor)
categoric<- names(which(sapply(train, is.character)))
factor<- names(which(sapply(train, is.factor)))

test[sapply(test, is.character)] <- lapply(test[sapply(test, is.character)],as.factor)
test$MSSubClass<-factor(test$MSSubClass)

```


```{r}
library(corrplot)
# only using the first 1460 rows - training data

correlations <- cor(train[numeric])
# only want the columns that show strong correlations with SalePrice

correlations_SalePrice <- as.matrix(sort(correlations[,'SalePrice'], decreasing = TRUE))
correlated_numeric<- names(which(apply(correlations_SalePrice, 1, function(x) ((x > 0.2) | (x < -0.2)))))
par(mfrow=c(1,1))
corrplot(as.matrix(correlations[correlated_numeric,correlated_numeric]), type = 'upper', 
         method='color', addCoef.col = 'black', tl.cex = .7,cl.cex = .7, number.cex=.8)

```


```{r}
plot1<-ggplot(aes(y =SalePrice, x =BsmtCond),data=train) + geom_boxplot()+
  labs(title = "The Effect of General Cond. of the Basement to Price", y = "Price", x = "Condition of Basement")

plot2<-ggplot(aes(y =SalePrice, x =BsmtExposure),data=train) + geom_boxplot()+
  labs(title = "The Effect of walkout/garden level walls to Price", y = "Price", x = "Exposure Level")

plot3<-ggplot(aes(y =SalePrice, x =BsmtQual),data=train) + geom_boxplot()+
  labs(title = "The Effect of Height of Basement to Price", y = "Price", x = "Height of the Basement")

plot4<-ggplot(aes(y =SalePrice, x =KitchenQual),data=train) + geom_boxplot()+
  labs(title = "The Effect of Kitchen Quality to Price", y = "Price", x = "Kitchen Quality")

plot5<-ggplot(aes(y =SalePrice, x =GarageType),data=train) + geom_boxplot()+
  labs(title = "The Effect of Garage Location to Price", y = "Price", x = "Garage Location")

plot6<-ggplot(aes(y =SalePrice, x =SaleCondition),data=train) + geom_boxplot()+
  labs(title = "The Effect of Sale Condition to Price", y = "Price", x = "Sale Condition")

plot7<-ggplot(aes(y =SalePrice, x =LandSlope),data=train) + geom_boxplot()+
  labs(title = "The Effect of Land Slope to Price", y = "Price", x = "Land Slope")

plot8<-ggplot(aes(y =SalePrice, x =Condition1),data=train) + geom_boxplot()+
  labs(title = "The Effect of Condition1 to Price", y = "Price", x = "Condition1")

plot9<-ggplot(aes(y =SalePrice, x =BldgType),data=train) + geom_boxplot()+
  labs(title = "The Effect of BUilding Type to Price", y = "Price", x = "BldgType")

plot10<-ggplot(aes(y =SalePrice, x =LotShape),data=train) + geom_boxplot()+
  labs(title = "The Effect of Lotshape Type to Price", y = "Price", x = "Lotshape")

plot11<-ggplot(aes(y =SalePrice, x =Utilities),data=train) + geom_boxplot()+
  labs(title = "The Effect of Utilities Type to Price", y = "Price", x = "Utilities")

plot12<-ggplot(aes(y =SalePrice, x =LotConfig),data=train) + geom_boxplot()+
  labs(title = "The Effect of LotConfig Type to Price", y = "Price", x = "LotConfig")

plot13<-ggplot(aes(y =SalePrice, x =Neighborhood),data=train) + geom_boxplot()+
  labs(title = "The Effect of Neighborhood Type to Price", y = "Price", x = "Neighborhood")

plot14<-ggplot(aes(y =SalePrice, x =Condition2),data=train) + geom_boxplot()+
  labs(title = "The Effect of Condition2 to Price", y = "Price", x = "Condition2")

plot15<-ggplot(aes(y =SalePrice, x =HouseStyle),data=train) + geom_boxplot()+
  labs(title = "The Effect of HouseStyle to Price", y = "Price", x = "Housestyle")

plot16<-ggplot(aes(y =SalePrice, x =RoofStyle),data=train) + geom_boxplot()+
  labs(title = "The Effect of RoofStyle to Price", y = "Price", x = "Roofstyle")

plot17<-ggplot(aes(y =SalePrice, x =RoofMatl),data=train) + geom_boxplot()+
  labs(title = "The Effect of RoofMatl to Price", y = "Price", x = "RoofMatl")

plot18<-ggplot(aes(y =SalePrice, x =Heating),data=train) + geom_boxplot()+
  labs(title = "The Effect of Heating to Price", y = "Price", x = "Heating")

plot19<-ggplot(aes(y =SalePrice, x =HeatingQC),data=train) + geom_boxplot()+
  labs(title = "The Effect of HeatingQC to Price", y = "Price", x = "HeatingQC")

plot20<-ggplot(aes(y =SalePrice, x =PavedDrive),data=train) + geom_boxplot()+
  labs(title = "The Effect of PavedDrive to Price", y = "Price", x = "PavedDrive")

plot21<-ggplot(aes(y =SalePrice, x =MSSubClass),data=train) + geom_boxplot()+
  labs(title = "The Effect of MSSubClass to Price", y = "Price", x = "MSSubclass")

plot22<-ggplot(aes(y =SalePrice, x =ExterQual),data=train) + geom_boxplot()+
  labs(title = "The Effect of ExterQual to Price", y = "Price", x = "ExterQual")

plot23<-ggplot(aes(y =SalePrice, x =BsmtQual),data=train) + geom_boxplot()+
  labs(title = "The Effect of BsmtQual to Price", y = "Price", x = "BsmtQual")

plot24<-ggplot(aes(y =SalePrice, x =MSSubClass),data=train) + geom_boxplot()+
  labs(title = "The Effect of MSSubClass to Price", y = "Price", x = "MSSubClass")


```

```{r}
grid.arrange(plot1,plot2,plot3,plot4,plot5,plot6,nrow=3,ncol=2)

```

```{r}
grid.arrange(plot7,plot8,plot9,plot10,plot11,plot12,nrow=3,ncol=2)


```


```{r}
grid.arrange(plot13,plot14,plot15,plot16,plot17,plot18,nrow=3,ncol=2)


```


```{r}

grid.arrange(plot19,plot20,plot21,plot22,plot23,plot24,nrow=3,ncol=2)
```


```{r}
summary<-c()
  fval<-c()
  for(i in 1:length(factor)){
    summary[i]<-summary(aov(SalePrice~.,data=train[,c(factor[i],"SalePrice")]))
    fval[i]<-summary[i][[1]][1,4]}
    d<-cbind.data.frame(factor,fval)
    d%>%arrange(desc(fval))
    
```

# ANOVA of Categorical Data
```{r}
MSSubClass_aov<-summary(aov(SalePrice~MSSubClass,data=train))
MSSubClass_aov
PavedDrive_aov<-summary(aov(SalePrice~PavedDrive,data=train))
PavedDrive_aov
HeatingQC_aov<-summary(aov(SalePrice~HeatingQC,data=train))
HeatingQC_aov
Heating_aov<-summary(aov(SalePrice~Heating,data=train))
Heating_aov
RoofMatl_aov<-summary(aov(SalePrice~RoofMatl,data=train))
RoofMatl_aov
RoofStyle_aov<-summary(aov(SalePrice~RoofStyle,data=train))
RoofStyle_aov
HouseStyle_aov<-summary(aov(SalePrice~HouseStyle,data=train))
HouseStyle_aov
Condition2_aov<-summary(aov(SalePrice~Condition2,data=train))
Condition2_aov
Condition1_aov<-summary(aov(SalePrice~Condition1,data=train))
Condition1_aov
Neighborhood_aov<-summary(aov(SalePrice~Neighborhood,data=train))
Neighborhood_aov
LotConfig_aov<-summary(aov(SalePrice~LotConfig,data=train))
LotConfig_aov
Utilities_aov<-summary(aov(SalePrice~Utilities,data=train))
Utilities_aov
LotShape_aov<-summary(aov(SalePrice~LotShape,data=train))
LotShape_aov
LandSlope_aov<-summary(aov(SalePrice~LandSlope,data=train))
LandSlope_aov
SaleCondition_aov<-summary(aov(SalePrice~SaleCondition,data=train))
SaleCondition_aov
BsmtCond_aov<-summary(aov(SalePrice~BsmtCond,data=train))
BsmtCond_aov
BsmtQual_aov<-summary(aov(SalePrice~BsmtQual,data=train))
BsmtQual_aov
BsmtExposure_aov<-summary(aov(SalePrice~BsmtExposure,data=train))
BsmtExposure_aov
BldgType_aov<-summary(aov(SalePrice~BldgType,data=train))
BldgType_aov
GarageType_aov<-summary(aov(SalePrice~GarageType,data=train))
GarageType_aov
GarageCond_aov<-summary(aov(SalePrice~GarageCond,data=train))
GarageCond_aov
GarageFinish_aov<-summary(aov(SalePrice~GarageFinish,data=train))
GarageFinish_aov
Exterior1st_aov<-summary(aov(SalePrice~Exterior1st,data=train))
Exterior1st_aov
KitchenQual_aov<-summary(aov(SalePrice~KitchenQual,data=train))
KitchenQual_aov
PoolQC_aov<-summary(aov(SalePrice~PoolQC,data=train))
PoolQC_aov

```


```{r}
set.seed(77) 
partition <- caret::createDataPartition(y=train$SalePrice, p=.75, list=FALSE) 
train_v2 <- train[partition,]
test_v2 <- train[-partition,]


train_v2_num<-subset(train_v2,select=c(SalePrice,OverallQual,GrLivArea,GarageCars,GarageArea,TotalBsmtSF,
                                       FullBath,TotRmsAbvGrd,YearBuilt,YearRemodAdd,MasVnrArea,Fireplaces,
                                       BsmtFinSF1,WoodDeckSF,OpenPorchSF,HalfBath,LotArea,BsmtFullBath))

train_v2_num<-log(train_v2_num+1)

train_v2_cat<-subset(train_v2,select=c(ExterQual,KitchenQual,BsmtQual,GarageFinish,MasVnrType,
                                       CentralAir,HeatingQC,Neighborhood,
                                       SaleCondition,MSZoning,PavedDrive,LotShape))

train_v2_last<-cbind.data.frame(train_v2_num,train_v2_cat)




test_v2_num<-subset(test_v2,select=c(SalePrice,OverallQual,GrLivArea,GarageCars,GarageArea,TotalBsmtSF,
                                     FullBath,TotRmsAbvGrd,YearBuilt,YearRemodAdd,MasVnrArea,Fireplaces,
                                     BsmtFinSF1,WoodDeckSF,OpenPorchSF,HalfBath,LotArea,BsmtFullBath))

test_v2_num<-log(test_v2_num+1)

test_v2_cat<-subset(test_v2,select=c(ExterQual,KitchenQual,BsmtQual,GarageFinish,MasVnrType,
                                     CentralAir,HeatingQC,Neighborhood,
                                     SaleCondition,MSZoning,PavedDrive,LotShape))


test_v2_last<-cbind.data.frame(test_v2_num,test_v2_cat)

```


# Remove Influential Points

```{r}
model_lm<-lm(SalePrice ~.,data=train_v2_last)
cooksd<-cooks.distance(model_lm)
 
plot(cooksd,pch="*",cex=2,main="Influential Obs by Cooks distance")

abline(h=4*mean(cooksd,na.rm=T),col="red")

text(x=1:length(cooksd)+1,y=cooksd,labels=ifelse(cooksd>4*mean(cooksd,na.rm=T),names(cooksd),""),col="red")

influential<-as.numeric(names(cooksd)[(cooksd>4*mean(cooksd,na.rm=T))]) #influential observations

#train_v2_last[influential,]

train_v2_last_out<-train_v2_last[!(row.names(train_v2_last) %in% as.vector(na.omit(influential))),]
```


```{r}
ctrl <- trainControl(method = "cv", 
                     number = 5)

set.seed(77)

model_train <- train(SalePrice ~., data = train_v2_last_out, method ='lm', trControl = ctrl)

summary(model_train)

prediction<-predict(model_train,newdata=test_v2_last)

sqrt(mean((test_v2_last$SalePrice-prediction)^2))

mean(abs(test_v2_last$SalePrice-prediction)/test_v2_last$SalePrice)

sqrt(mean((exp(test_v2_last$SalePrice)-exp(prediction))^2))
```


#LOG Transformation of Categoric Variable

```{r}
train_num<-subset(train,select=c(SalePrice,OverallQual,GrLivArea,GarageCars,GarageArea,TotalBsmtSF,
                                 FullBath,TotRmsAbvGrd,YearBuilt,YearRemodAdd,MasVnrArea,Fireplaces,
                                 BsmtFinSF1,WoodDeckSF,OpenPorchSF,HalfBath,LotArea,BsmtFullBath))


train_num<-log(train_num+1)

train_cat<-subset(train,select=c(ExterQual,KitchenQual,BsmtQual,GarageFinish,MasVnrType,
                                 CentralAir,HeatingQC,Neighborhood,
                                 SaleCondition,MSZoning,PavedDrive,LotShape))


train_last<-cbind.data.frame(train_num,train_cat)




test_num<-subset(test,select=c(OverallQual,GrLivArea,GarageCars,GarageArea,TotalBsmtSF,
                               FullBath,TotRmsAbvGrd,YearBuilt,YearRemodAdd,MasVnrArea,Fireplaces,
                               BsmtFinSF1,WoodDeckSF,OpenPorchSF,HalfBath,LotArea,BsmtFullBath))
test_num<-log(test_num+1)

test_cat<-subset(test,select=c(ExterQual,KitchenQual,BsmtQual,GarageFinish,MasVnrType,
                               CentralAir,HeatingQC,GarageType,Neighborhood,BsmtFinType1,
                               SaleCondition,MSZoning,PavedDrive,LotShape))


test_last<-cbind.data.frame(test_num,test_cat)


```



# Remove Influential Points

```{r}
model_lm<-lm(SalePrice~.,data=train_last)
cooksd<-cooks.distance(model_lm)

plot(cooksd,pch="*",cex=2,main="Influential Obs by Cooks distance")

abline(h=4*mean(cooksd,na.rm=T),col="red")

text(x=1:length(cooksd)+1,y=cooksd,labels=ifelse(cooksd>4*mean(cooksd,na.rm=T),names(cooksd),""),col="red")

influential<-as.numeric(names(cooksd)[(cooksd>4*mean(cooksd,na.rm=T))]) #influential observations

#train_last[influential,]

train_last_out<-train_v2_last[!(row.names(train_v2_last) %in% as.vector(na.omit(influential))),]
```

```{r}
model_lm<-lm(SalePrice~.,data=train_last_out)
cooksd<-cooks.distance(model_lm)

plot(cooksd,pch="*",cex=2,main="Influential Obs by Cooks distance")

abline(h=4*mean(cooksd,na.rm=T),col="red")

text(x=1:length(cooksd)+1,y=cooksd,labels=ifelse(cooksd>4*mean(cooksd,na.rm=T),names(cooksd),""),col="red")

influential<-as.numeric(names(cooksd)[(cooksd>4*mean(cooksd,na.rm=T))]) #influential observations

#train_last_out[influential,]

train_last_out2<-train_v2_last_out[!(row.names(train_v2_last) %in% as.vector(na.omit(influential))),]

```

# Final Model

```{r}
model_train_final <- train(SalePrice ~., data = train_last_out, method ='lm', trControl = ctrl)
summary(model_train_final)

prediction<-predict(model_train_final,newdata=test_last)

expo_prediction<-exp(prediction)

```


```{r}
final_result<-cbind.data.frame(test$Id,expo_prediction)
colnames(final_result)<-c("Id","SalePrice")
```

