-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_peer.rmd
1908 lines (1258 loc) · 71.8 KB
/
Final_peer.rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Peer Assessment II"
output:
html_document:
pandoc_args: [
"--number-sections",
]
---
# Background
As a statistical consultant working for a real estate investment firm, your task is to develop a model to predict the selling price of a given home in Ames, Iowa. Your employer hopes to use this information to help assess whether the asking price of a house is higher or lower than the true value of the house. If the home is undervalued, it may be a good investment for the firm.
# Training Data and relevant packages
In order to better assess the quality of the model you will produce, the data have been randomly divided into three separate pieces: a training data set, a testing data set, and a validation data set. For now we will load the training data set, the others will be loaded and used later.
```{r load, message = FALSE}
load("ames_train.Rdata")
library(dplyr)
ames_train$Overall.Qual<-as.factor(ames_train$Overall.Qual)
ames_train$Overall.Cond<-as.factor(ames_train$Overall.Cond)
am<-select(ames_train,price,area,Lot.Area,Year.Built,Overall.Qual,Land.Slope,Sale.Condition,Overall.Cond)
am<-data.frame(am)
```
```{r xxx, echo=FALSE}
amSet1<-select(ames_train,price,Overall.Qual,Neighborhood,area,BsmtFin.SF.1,
Overall.Cond,Garage.Yr.Blt,Bldg.Type,Total.Bsmt.SF,Year.Built,
Land.Slope,Sale.Condition,Central.Air,Lot.Shape,Kitchen.Qual,
Garage.Cars,Fireplaces,Year.Remod.Add,MS.Zoning)
amLSet<-ames_train
amLSet<-data.frame(amLSet)
amLSet$logarea<-log(amLSet$area+1)
amLSet$logLot.Area<-log(amLSet$Lot.Area+1)
amLSet$logX2nd.Flr.SF<-log(amLSet$X2nd.Flr.SF+1)
amLSet$logBsmtFin.SF.1<-log(amLSet$BsmtFin.SF.1+1)
amSet2<-select(amLSet,price,Overall.Qual,Neighborhood,logarea,
Overall.Cond,Year.Built,logLot.Area,Bsmt.Full.Bath,
Garage.Type,Sale.Condition,logX2nd.Flr.SF,Bldg.Type,
Heating.QC,logBsmtFin.SF.1,Garage.Cars,MS.Zoning,
Kitchen.Qual,Heating,Central.Air,
Fireplaces)
LSet<-select(ames_train,price,Overall.Qual,Lot.Area,
Year.Built,Sale.Condition,area,MS.Zoning,Year.Remod.Add,Land.Slope,
Exter.Qual,Lot.Shape,Land.Contour,Lot.Config,Street,
Bsmt.Qual,Bsmt.Cond,Bsmt.Exposure,BsmtFin.Type.1,Bldg.Type,
BsmtFin.SF.1,BsmtFin.Type.2,BsmtFin.SF.2,Bsmt.Unf.SF,
Total.Bsmt.SF,Heating.QC,Central.Air,House.Style,
Electrical,X1st.Flr.SF,X2nd.Flr.SF,
Bsmt.Full.Bath,Bsmt.Half.Bath,Full.Bath,Half.Bath,
Bedroom.AbvGr,Kitchen.AbvGr,TotRms.AbvGrd,
Functional,Fireplaces,
Garage.Yr.Blt,Garage.Finish,Garage.Cars,Garage.Area,
Paved.Drive,Wood.Deck.SF,
Open.Porch.SF,Enclosed.Porch,X3Ssn.Porch,Screen.Porch,
Fence,Misc.Val,Mo.Sold,Yr.Sold,Sale.Type)
LSet<-data.frame(LSet)
LSet$logarea<-log(LSet$area+1)
LSet$logLot.Area<-log(LSet$Lot.Area+1)
LSet$logGarage.Area<-log(LSet$Garage.Area+1)
LSet$logX2nd.Flr.SF<-log(LSet$X2nd.Flr.SF+1)
LSet$logBsmtFin.SF.1<-log(LSet$BsmtFin.SF.1+1)
LSet$logBsmtFin.SF.2<-log(LSet$BsmtFin.SF.2+1)
LSet$logBsmt.Unf.SF<-log(LSet$Bsmt.Unf.SF+1)
LSet$logTotal.Bsmt.SF<-log(LSet$Total.Bsmt.SF+1)
LSet$logWood.Deck.SF<-log(LSet$Wood.Deck.SF+1)
LSet$logOpen.Porch.SF<-log(LSet$Open.Porch.SF+1)
LSet$logX1st.Flr.SF<-log(LSet$X1st.Flr.SF+1)
LSet<-LSet[-c(3,6,20,22,23,24,29,30,43,45,46)]
amSet3<-select(LSet,1,2,44,3,45,48,4,19,5,6,31,54,16,39,30,34,18,29,14,28,13)
```
Use the code block below to load any necessary packages
```{r packages, message = FALSE}
library(statsr)
library(dplyr)
library(BAS)
library(MASS)
library(ggplot2)
library(devtools)
getwd()
```
## Part 1 - Exploratory Data Analysis (EDA)
When you first get your data, it's very tempting to immediately begin fitting models and assessing how they perform. However, before you begin modeling, it's absolutely essential to explore the structure of the data and the relationships between the variables in the data set.
Do a detailed EDA of the ames_train data set, to learn about the structure of the data and the relationships between the variables in the data set (refer to Introduction to Probability and Data, Week 2, for a reminder about EDA if needed). Your EDA should involve creating and reviewing many plots/graphs and considering the patterns and relationships you see.
After you have explored completely, submit the three graphs/plots that you found most informative during your EDA process, and briefly explain what you learned from each (why you found each informative).
* * *
### Labeled Histogram and Data Description
The data was analyized and the following statistics were determined :
* Least expensive house is $12,789 (9.5 log units) and is the 428th row of the data table
* Most expensive house is $615,000 (13.3 log units) and is the 66th row of the data table
* There are more expensive houses and fewer cheaper houses (left skewed)
* The distribution is unimodal with a median price of $159467 (12.0 log units) indicated by the red line
* The mean house price is $181190 (12.1 log units) indicated by the purple line
* The largest number of houses are between $100,000 - $200,000 (11.7-12.2 log units) price range
The histogram, In Fig. I, depicts a disribution of the Ames Iowa houses by natural log of the price in US Dollars. There are 30 bins, each increasingly logarithymically, indicating the natural log of the price for each interval. The total span of the histogram is roughly $700,000 or(13.5 log units). The total count for each bin is located near the top of the bin column.
The blue shaded region for each bar in the histogram indicate the house was purchase under normal sale conditions. The salmon colored regions indicate other than normal sale conditions. The preponderance of the other than normal sale conditions occur for the higher priced houses.
```{r creategraphs,echo=FALSE}
ama<-ames_train
p<-ggplot(aes(x = log(price) ) , data = ama) +
geom_histogram(aes(fill=(Sale.Condition=='Normal') )) +
stat_bin( geom="text", aes(label=..count..) ,vjust = 0,hjust=0) +
xlab('Natural log of Price (in US Dollars)')+
geom_vline(data=ama,xintercept=median(log(ama$price)), color="red")+
geom_vline(data=ama,xintercept=mean(log(ama$price)), color="blue")+
ggtitle("Fig I. Natural Log of Price Distribution of Real Estate for Ames Iowa")
suppressMessages(plot(p))
#plot(p)
par(mfrow=c(1,1))
am<-ames_train
am<-as.data.frame(am)
hilo<-matrix(0,nrow=4,ncol=5,byrow=TRUE,dimnames=list(c('Least Expensive','Most Expensive','Median','Mean'),c('Price','LogPrice','Index','Count','Neighborhood')))
hilo<-data.frame(hilo)
hilo[1,'Price']<- min(am$price)
hilo[2,'Price']<- max(am$price)
hilo[3,'Price']<- median(am$price)
hilo[4,'Price']<- mean(am$price)
hilo[2,'LogPrice']<- round(log(max(am$price)),1)
hilo[1,'LogPrice']<- round(log(min(am$price)),1)
hilo[3,'LogPrice']<- round(log(median(am$price)),1)
hilo[4,'LogPrice']<- round(log(mean(am$price)),1)
hilo[1,'Count']<- nrow(am[am$price==min(am$price),])
hilo[2,'Count']<- nrow(am[am$price==max(am$price),])
hilo[1,'Index']<- which(am$price==min(am$price))
hilo[2,'Index']<- which(am$price==max(am$price))
hilo[1,'Neighborhood']<- as.character(am[which(am$price==min(am$price)),'Neighborhood'])
hilo[2,'Neighborhood']<- as.character(am[which(am$price==max(am$price)),'Neighborhood'])
# TABLE I
print('Table I: Highest, Lowest, Median and Mean Priced Houses in Ames Training Data');hilo
#PLOT
# CONSTRUCT A DATA FRAME CONTAINING A ROW FOR EACH NEIGHBORHOOD IN THE AMES DARA
# THE COLUMNS CONTAIN STATISTICS FOR EACH NEIGHBORHOOD (MEAN, SD, MEDIAN, MAX ,MIN)
neighstat <- matrix(0, nrow = length(unique(am$Neighborhood)), ncol = 6, byrow = TRUE,
dimnames = list(unique(am$Neighborhood),
c("Mean", "SD","Median","Max","Min","IQR")))
neighstat<-data.frame(neighstat)
#BUILD THE DATA FRAME
j=1
for(i in rownames(neighstat)){
neighstat[j,1]<-mean(filter(am,Neighborhood==i)$price)
neighstat[j,2]<-sd(filter(am,Neighborhood==i)$price)
neighstat[j,3]<-median(filter(am,Neighborhood==i)$price)
neighstat[j,4]<-max(filter(am,Neighborhood==i)$price)
neighstat[j,5]<-min(filter(am,Neighborhood==i)$price)
neighstat[j,6]<-IQR(filter(am,Neighborhood==i)$price)
j=j+1
}
# REVERSE ORDER THE DATA FRAMES BY MEDIAN, MEAN AND SD INTO SEPARATE DATA FRAMES
medianRN<-rownames(neighstat[order(-neighstat$Median),])
meanRN<-rownames(neighstat[order(-neighstat$Mean),])
sdRN<-rownames(neighstat[order(-neighstat$SD),])
iqrRN<-rownames(neighstat[order(-neighstat$IQR),])
RN<-rbind(medianRN,meanRN,sdRN,iqrRN)
#RN
medianPlot<-data.frame()
meanPlot<-data.frame()
sdPlot<-data.frame()
iqrPlot<-data.frame()
medianPlot<-ggplot(medianPlot)
meanPlot<-ggplot(meanPlot)
sdPlot<-ggplot(sdPlot)
iqrPlot<-ggplot(iqrPlot)
medianTitle<-"Fig, II Ames Real Estate, by Neighborhood, in Order of Highest Median Price"
meanTitle<-"Fig. III Ames Real Estate, by Neighborhood, in Order of Highest Average Price"
sdTitle<-"Fig. II Ames Real Estate, by Neighborhood, in Order of Most Heterogenous Prices"
iqrTitle<-"Fig. V Ames Real Estate, by Neighborhood, in Order Highest Price IQR"
Title<-rbind(medianTitle,meanTitle,sdTitle,iqrTitle)
#Title
for(k in 1:nrow(RN)){
j=1
for(i in RN[k,]){
x<-am$Neighborhood==i
am[grep("TRUE",x),'order']<-j
ifelse(j<10,am[grep("TRUE",x),'NeighborhoodLabel']<-paste0("0",j," ",i),am[grep("TRUE",x),'NeighborhoodLabel']<-paste0(j," ",i))
j=j+1
}
x<-am %>% group_by( order) %>%
ggplot( aes(x=as.factor(order), y=price/1000,fill = NeighborhoodLabel, col=I("black"))) + geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=4, size=5,color='black')+
stat_summary(fun.y=min, geom="point", shape=25, size=2) +
stat_summary(fun.y=sd, geom="point", shape=1, size=2,color='red') +
stat_summary(fun.y=max, geom="point", shape=24, size=2) +
coord_flip()+
xlab("Neigborhood")+
scale_y_discrete(name ="Price (in $1000)" ,
limits=c(0,100,200,300,400,500,600)) +
ggtitle(Title[k,1])
if(k==1){medianPlot<-x}
if(k==2){meanPlot<-x}
if(k==3){sdPlot<-x}
if(k==4){iqrPlot<-x}
}
options(warn=-1)
plot(sdPlot)
```
### Neighborhood Statistical Summary
The statistical summary is provided using individual boxplots for each Neighborhood in the Ames training data with respect to price. In order to better understand the statistical summary, a detailed explanation of Figure II is provided in the following section.
The boxplots are plotted sideways to reveal the information in a form much like a distribution. Each boxplot has units of price dollars (in thousands) with the following characteristics:
* Box width corresponds to the price Inter Quartile Range or IQR
* Box center bar is the median price value
* Left and right box whisker(sometimes outliers) corresponds to the min(inverted triangle) and max(upright triangle) price values, correspondingly,
* X in the box is the price mean
* The red o corresponds to the price standard deviation
* The NeighborhoodLabel maps the neighborhood name to its numerical value on the boxplot.
A red o represents the price standard deviation for each boxplot. By examination it is easy for you to see that the right-handedness of the red o decreases as you move your view upward from boxplot 1. The Neighboorhood associated with boxplot 1 is StoneBr and it has a maximum house price of $`r as.integer(neighstat['StoneBr','Max'])`, a minimum price of $`r as.integer(neighstat['StoneBr','Min'])` and it has a price standard deviation of $`r as.integer(max(neighstat$SD))`. Correspondingly, the Neighborhood associated with boxplot 27 is Blueste and it has a maximum house price of $`r as.integer(neighstat['Blueste','Max'])`, a minimum price of $`r as.integer(neighstat['Blueste','Min'])` it has a price standard deviation of $`r as.integer(min(neighstat$SD))`. Therfore the price standard deviation range or heterogenocity is from $10,000 to $123,000 when grouped by Neighborhood.
```{r graph2, echo=FALSE}
par(mfrow=c(1,1))
ami<-order(am$price)
oam<-am[ami,]
indx<-1:500
subS<-oam[indx,]
subS<-data.frame(subS)
subS2<-oam[-indx,]
subS2<-data.frame(subS2)
par(mfrow=c(1,1))
n.Overall.Qual = length(levels(subS2$Overall.Qual))
par(mar=c(5,4,4,10))
plot(log(price) ~ I(area),
data=subS, col=Overall.Qual,
pch=as.numeric(Overall.Qual)+15, main="Fig.III 500 Lowest Sale Prices showing Overall Quality",
xlab=" Living Space (in SF)",ylab="Natural Log of Price (in US Dollars)")
legend(x=,"right", legend=levels(ames_train$Overall.Qual),
col=1:n.Overall.Qual, pch=15+(1:n.Overall.Qual),
bty="n", xpd=TRUE, inset=c(-.5,0))
# n.Overall.Qual = length(levels(subS2$Overall.Qual))
# par(mar=c(5,4,4,10))
# plot(log(price) ~ I(area),
# data=subS2, col=Overall.Qual,
# pch=as.numeric(Overall.Qual)+15, main="Fig.III 500 Lowest Sale Prices showing Overall Quality",
# xlab=" Living Space (in SF)",ylab="Natural Log of Price (in US Dollars)")
# legend(x=,"right", legend=levels(ames_train$Overall.Qual),
# col=1:n.Overall.Qual, pch=15+(1:n.Overall.Qual),
# bty="n", xpd=TRUE, inset=c(-.5,0))
options(warn=0)
par(mfrow=c(1,1))
```
### 500 Lowest Priced Houses Scatter Plot
Figure III is a scatter plot of 500 lowest prices in the ames_train data set. The ordinate is the natural log of the selling price and the abscissa is the living space. The points are characterized by each house's overall Quality and fro this reason it is needed. I choose to use this plot since the under priced holmes may be more likely to be in the lower half.
* * *
## Part 2 - Development and assessment of an initial model, following a semi-guided process of analysis
### Section 2.1 An Initial Model
In building a model, it is often useful to start by creating a simple, intuitive initial model based on the results of the exploratory data analysis. (Note: The goal at this stage is **not** to identify the "best" possible model but rather to choose a reasonable and understandable starting point. Later you will expand and revise this model to create your final model.
Based on your EDA, select *at most* 10 predictor variables from “ames_train” and create a linear model for `price` (or a transformed version of price) using those variables. Provide the *R code* and the *summary output table* for your model, a *brief justification* for the variables you have chosen, and a *brief discussion* of the model results in context (focused on the variables that appear to be important predictors and how they relate to sales price).
#### Building the Initial Model
Using the results from the previous tests and exercises, I chose to predict log(price) using:
log(area),log(Lot.Area),Year.Built,Overall.Qual,Land.Slope,Sale.Condition and Overall.Cond as explanatory variables.
The Summary Table is provided below.
* * *
```{r fit_model, cache=TRUE}
lmt<-lm(log(price)~log(area)+log(Lot.Area)+Year.Built+Overall.Qual+Land.Slope+Sale.Condition+Overall.Cond,data=am)
summary(lmt)
```
#### Model Variable Explanation/Discussion
* log(area) - Size of house important to price prediction - Coefficient Estimate = ($`r summary(lmt)$coefficients[2,1]`) - log chosen to make square footage less right skewed
* log(Lot.Area) - Size of lot important to price prediction - - Coefficient Estimate = ($`r summary(lmt)$coefficients[3,1]`) log chosen to make square footage less right skewed
* Year.Built - Coefficient Estimate = ($`r summary(lmt)$coefficients[4,1]`) House age can be important for many reasons, some like older house and some like newer
* Overall.Qual - - Coefficient Estimate = ($`r summary(lmt)$coefficients[5:13,1]`) - price is directly dependent on this which can lead to undervalue
* Land.Slope - Coefficient Estimate = ($`r summary(lmt)$coefficients[14:15,1]`)- Some like flat lots, other like a hill for water drainage
* Sale.Condition - Coefficient Estimate = ($`r summary(lmt)$coefficients[16:20,1]`)- Emergency situations can reduce selling price
* Overall.Cond - - Coefficient Estimate = ($`r summary(lmt)$coefficients[21:28,1]`) House Condition can be a major cause of undervalued price
The Adjusted R Squared value is $`r summary(lmt)$adj.r.square` , which is a good starting point.
* * *
### Section 2.2 Model Selection
Now either using `BAS` another stepwise selection procedure choose the "best" model you can, using your initial model as your starting point. Try at least two different model selection methods and compare their results. Do they both arrive at the same model or do they disagree? What do you think this means?
* * *
#### Using Bayesian Adaptive Sampling (BAS) and Akaike Information Criterion (AIC)
The Bayesian adaptive sampling algorithm (BAS), samples models without replacement from the space of models. For problems that permit enumeration of all models, BAS is guaranteed to enumerate the model space in 2p iterations where p is the number of potential variables under consideration. For larger problems where sampling is required, we provide conditions under which BAS provides perfect samples without replacement. When the sampling probabilities in the algorithm are the marginal variable inclusion probabilities, BAS may be viewed as sampling models "near" the median probability model of Barbieri and Berger. As marginal inclusion probabilities are not known in advance, we discuss several strategies to estimate adaptively the marginal inclusion probabilities within BAS. We illustrate the performance of the algorithm using simulated and real data and show that BAS can outperform Markov chain Monte Carlo methods.[3]
The BAS model is constructed and analysed. The corrections are implemented and applied to a second model, lmtA.
The AIC model is applied to bothe models, lmt and lmtA.
```{r model_select,cache=TRUE}
model.bas <- bas.lm(log(price) ~log(area)+log(Lot.Area)+Bedroom.AbvGr+Overall.Qual+Land.Slope+ Sale.Condition+Overall.Cond, data = am, prior = "ZS-null", modelprior=uniform(),initprobs="eplogp")
plot(model.bas, ask=F)
```
#### BAS Graphical Summaries (4 Plots Shown Above)
* $\bf{Residuals}$ $\bf{and}$ $\bf{Fitted}$ $\bf{Values}$ - As rendered under Bayesian Model Averaging. Ideally, of our model assumptions hold, we will not see outliers or non-constant variance.[1]
* $\bf{Model}$ $\bf{Probabilities}$ - the cumulative probability of the models in the order that they are sampled. This plot indicates that the cumulative probability is leveling off as each additional model adds only a small increment to the cumulative probability, which earlier, there are larger jumps corresponding to sampling high probability models.[1]
* $\bf{Model}$ $\bf{Complexity}$ - the dimension of each model (the number of regression coefficients including the intercept) versus the log of the marginal likelihood of the model.[1]
* $\bf{Inclusion}$ $\bf{Probabilities}$ - the marginal posterior inclusion probabilities (pip) for each of the covariates, with marginal pips greater than 0.5 shown in red. The variables with pip > 0.5 correspond to what is known as the median probability model. Variables with high inclusion probabilities are generally important for explaining the data or prediction, but marginal inclusion probabilities may be small if there predictors are correlated, similar to how p-values may be large in the presence of mullticollinearity.[1]
```{r ZZ ,cache=TRUE}
model.bas
```
#### Marginal Posterior Inclusion Probabilities (Above)
```{r zza}
options(width = 80)
summary(model.bas)
```
#### BAS Top 5 Models (in terms of posterior probability)
Listed above with the zero-one indicators for variable inclusion. The other columns in the summary are the Bayes factor of each model to the highest probability model (hence its Bayes factor is 1), the posterior probabilities of the models, the ordinary $R^2$ of the models, the dimension of the models (number of coefficients including the intercept) and the log marginal likelihood under the selected prior distribution.[1]
```{r zzb}
image(model.bas, rotate=F)
```
#### Model Space Visualisation (Above)
This image has rows that correspond to each of the variables and intercept, with labels for the variables on the y-axis. The x-axis corresponds to the possible models. These are sorted by their posterior probability from best at the left to worst at the right with the rank on the top x-axis.
Each column represents one of the 20 models. The variables that are excluded in a model are shown in black for each column, while the variables that are included are colored, with the color related to the log posterior probability. The color of each column is proportional to the log of the posterior probabilities (the lower x-axis) of that model.
Models that are the same color have similar log posterior probabilities which allows us to view models that are clustered together that have marginal likelihoods where the differences are not "worth a bare mention".
This plot indicates that the police expenditure in the two years do not enter the model together, and is an indication of the high correlation between the two variables.[1]
```{r zzc}
am1<- ames_train %>% filter(Sale.Condition == "Normal"| Sale.Condition == "Partial")
am1<- am1 %>% filter(Overall.Qual!=2| Overall.Qual != 3)
am1<- am1 %>% filter(Overall.Cond!=2)
am1<- am1 %>% filter(Land.Slope!='Sev')
lmtA<-lm(log(price)~log(area)+log(Lot.Area)+Year.Built+Overall.Qual+Land.Slope+Sale.Condition+Overall.Cond,data=am1)
```
#### Base Model with BAS Adjustments (lmtA Above)
The base model, lmt, has been adjusted for BAS Model 1 conditions. This will yield a more effective predictive model.
```{r}
model.AIC <- stepAIC(lmt, k = 2)
```
#### AIC Model using Base Explanatory variables
The Akaike information criterion (AIC) is a measure of the relative quality of statistical models for a given set of data. Given a collection of models for the data, AIC estimates the quality of each model, relative to each of the other models. Hence, AIC provides a means for model selection.
AIC is founded on information theory: it offers a relative estimate of the information lost when a given model is used to represent the process that generates the data. In doing so, it deals with the trade-off between the goodness of fit of the model and the complexity of the model.
AIC does not provide a test of a model in the sense of testing a null hypothesis, so it can tell nothing about the quality of the model in an absolute sense. If all the candidate models fit poorly, AIC will not give any warning of that.[2]
The Bayesian information criterion (BIC) or Schwarz criterion (also SBC, SBIC) is a criterion for model selection among a finite set tof models; the model with the lowest BIC is preferred. It is based, in part, on the likelihood function and it is closely related to the Akaike information criterion (AIC).[2]
```{r}
model.AICA <- stepAIC(lmtA, k = 2)
```
#### AIC Model using BAS Adjusted Explanatory variables
* * *
### Section 2.3 Initial Model Residuals
One way to assess the performance of a model is to examine the model's residuals. In the space below, create a residual plot for your preferred model from above and use it to assess whether your model appears to fit the data well. Comment on any interesting structure in the residual plot (trend, outliers, etc.) and briefly discuss potential implications it may have for your model and inference / prediction you might produce.
* * *
#### Residuals and Errors
In statistics and optimization, errors and residuals are two closely related and easily confused measures of the deviation of an observed value of an element of a statistical sample from its "theoretical value". The error (or disturbance) of an observed value is the deviation of the observed value from the (unobservable) true value of a quantity of interest (for example, a population mean), and the residual of an observed value is the difference between the observed value and the estimated value of the quantity of interest (for example, a sample mean). The distinction is most important in regression analysis, where the concepts are sometimes called the regression errors and regression residuals and where they lead to the concept of studentized residuals.[4]
```{r model_resid, cache=TRUE}
plot(lmt$residuals, main="Base Model Residuals vs Collective Index")
plot(lmtA$residuals, main="Base Model with BAS Adjustments Residuals vs Collective Index")
plot(model.AIC$residuals, main='Model AIC Residuals vs Collection Index')
plot(model.AICA$residuals, main='Model AIC with BAS adjustments Residuals vs Collection Index')
```
#### Analysis of the Residual Plots Above
The difference between the observed value of the dependent variable (y) and the predicted value (y) is called the residual (e). Each data point has one residual.
Residual = Observed value - Predicted value ($e = y - y$)
Both the sum and the mean of the residuals are equal to zero. That is, $\sum_{i=1}^{n} e_i$ = 0 and e = 0.
A residual plot is a graph that shows the residuals on the vertical axis and the independent variable on the horizontal axis. If the points in a residual plot are randomly dispersed around the horizontal axis, a linear regression model is appropriate for the data; otherwise, a non-linear model is more appropriate.[5]
The points of interest for the residuals is that all four plots appear to be uniformly distributed and the number of $\bf {outliers}$ is substantially reduced by using the BAS for both the Base and the AIC models.
* * *
### Section 2.4 Initial Model RMSE
You can calculate it directly based on the model output. Be specific about the units of your RMSE (depending on whether you transformed your response variable). The value you report will be more meaningful if it is in the original units (dollars).
* * *
In statistics, the mean squared error (MSE) or mean squared deviation (MSD) of an estimator (of a procedure for estimating an unobserved quantity) measures the average of the squares of the errors or deviations-that is, the difference between the estimator and what is estimated. MSE is a risk function, corresponding to the expected value of the squared error loss or quadratic loss. The difference occurs because of randomness or because the estimator doesn't account for information that could produce a more accurate estimate.[7]
The MSE is a measure of the quality of an estimator-it is always non-negative, and values closer to zero are better.
The MSE is the second moment (about the origin) of the error, and thus incorporates both the variance of the estimator and its bias. For an unbiased estimator, the MSE is the variance of the estimator. Like the variance, MSE has the same units of measurement as the square of the quantity being estimated. In an analogy to standard deviation, taking the square root of MSE yields the root-mean-square error or root-mean-square deviation (RMSE or RMSD), which has the same units as the quantity being estimated; for an unbiased estimator, the RMSE is the square root of the variance, known as the standard deviation.[6]
```{r model_rmse,cache=TRUE,echo=FALSE}
predict.fullAlone <- exp(predict(lmt))
predict.fullAloneA <- exp(predict(lmtA))
predict.AICAlone<-exp(predict(model.AIC))
predict.AICAloneA<-exp(predict(model.AICA))
# Extract Residuals
resid.fullAlone <- ames_train$price - predict.fullAlone
resid.AICAlone<-ames_train$price - predict.AICAlone
resid.fullAloneA <- am1$price - predict.fullAloneA
resid.AICAloneA <- am1$price - predict.AICAloneA
# Calculate RMSE
rmse.fullAlone <- sqrt(mean(resid.fullAlone^2))
rmse.fullAloneA <- sqrt(mean(resid.fullAloneA^2))
rmse.AICAlone<-sqrt(mean(resid.AICAlone^2))
rmse.AICAloneA<-sqrt(mean(resid.AICAloneA^2))
sprintf("%8.0f US Dollars",rmse.fullAlone)
```
#### RMSE Value for Base Training Model (Above)
```{r mrA}
sprintf("%8.0f US Dollars",rmse.fullAloneA)
```
#### RMSE Value for Base Training Model with BAS Adjustment (Above)
```{r mrB}
sprintf("%8.0f US Dollars",rmse.AICAlone)
```
#### RMSE Value for AIC Training Model (Above)
```{r mrC}
sprintf("%8.0f US Dollars",rmse.AICAloneA)
```
#### RMSE Value for AIC Training Model with BAS Adjustment (Above)
#### Analysis of the RMSE Above
It is obvios that applying the BAS adjustments to both the Base and AIC model lowers the RMSE (much lower than the 100,000 dollar limit). Since there is nod difference between RMSE results for the Base and AIC model, the AIC model will be no longer deployed.
* * *
### Section 2.5 Overfitting
The process of building a model generally involves starting with an initial model (as you have done above), identifying its shortcomings, and adapting the model accordingly. This process may be repeated several times until the model fits the data reasonably well. However, the model may do well on training data but perform poorly out-of-sample (meaning, on a dataset other than the original training data) because the model is overly-tuned to specifically fit the training data. This is called “overfitting.” To determine whether overfitting is occurring on a model, compare the performance of a model on both in-sample and out-of-sample data sets. To look at performance of your initial model on out-of-sample data, you will use the data set `ames_test`.
```{r loadtest, message = FALSE}
library(dplyr)
load("ames_test.Rdata")
ames_test$Overall.Qual<-as.factor(ames_test$Overall.Qual)
ames_test$Overall.Cond<-as.factor(ames_test$Overall.Cond)
at<-ames_test
at1<- at %>% filter(Sale.Condition == "Normal"| Sale.Condition == "Partial")
at1<- at1 %>% filter(Overall.Qual!=2| Overall.Qual != 3)
at1<- at1 %>% filter(Overall.Cond!=2)
at1<- at1 %>% filter(Land.Slope!='Sev')
############## TO BE USED IN LATER SECTIONS ############################
tLSet<-dplyr::select(ames_test,price,Overall.Qual,Lot.Area,
Year.Built,Sale.Condition,area,MS.Zoning,Year.Remod.Add,Land.Slope,
Exter.Qual,Lot.Shape,Land.Contour,Lot.Config,Street,
Bsmt.Qual,Bsmt.Cond,Bsmt.Exposure,BsmtFin.Type.1,Bldg.Type,
BsmtFin.SF.1,BsmtFin.Type.2,BsmtFin.SF.2,Bsmt.Unf.SF,
Total.Bsmt.SF,Heating.QC,Central.Air,House.Style,
Electrical,X1st.Flr.SF,X2nd.Flr.SF,
Bsmt.Full.Bath,Bsmt.Half.Bath,Full.Bath,Half.Bath,
Bedroom.AbvGr,Kitchen.AbvGr,TotRms.AbvGrd,
Functional,Fireplaces,
Garage.Yr.Blt,Garage.Finish,Garage.Cars,Garage.Area,
Paved.Drive,Wood.Deck.SF,
Open.Porch.SF,Enclosed.Porch,X3Ssn.Porch,Screen.Porch,
Fence,Misc.Val,Mo.Sold,Yr.Sold,Sale.Type)
tLSet<-data.frame(tLSet)
tLSet$logarea<-log(tLSet$area+1)
tLSet$logLot.Area<-log(tLSet$Lot.Area+1)
tLSet$logGarage.Area<-log(tLSet$Garage.Area+1)
tLSet$logX2nd.Flr.SF<-log(tLSet$X2nd.Flr.SF+1)
tLSet$logBsmtFin.SF.1<-log(tLSet$BsmtFin.SF.1+1)
tLSet$logBsmtFin.SF.2<-log(tLSet$BsmtFin.SF.2+1)
tLSet$logBsmt.Unf.SF<-log(tLSet$Bsmt.Unf.SF+1)
tLSet$logTotal.Bsmt.SF<-log(tLSet$Total.Bsmt.SF+1)
tLSet$logWood.Deck.SF<-log(tLSet$Wood.Deck.SF+1)
tLSet$logOpen.Porch.SF<-log(tLSet$Open.Porch.SF+1)
tLSet$logX1st.Flr.SF<-log(tLSet$X1st.Flr.SF+1)
tLSet<-tLSet[-c(3,6,20,22,23,24,29,30,43,45,46)]
atLSet<-dplyr::select(tLSet,1,2,44,3,45,48,4,19,5,6,31,54,16,39,30,34,18,29,14,28,13)
atSet1<-dplyr::select(ames_test,price,Overall.Qual,Neighborhood,area,BsmtFin.SF.1,
Overall.Cond,Garage.Yr.Blt,Bldg.Type,Total.Bsmt.SF,Year.Built,
Land.Slope,Sale.Condition,Central.Air,Lot.Shape,Kitchen.Qual,
Garage.Cars,Fireplaces,Year.Remod.Add,MS.Zoning)
amLSet<-ames_train
amLSet<-data.frame(amLSet)
amLSet$logarea<-log(amLSet$area+1)
amLSet$logLot.Area<-log(amLSet$Lot.Area+1)
amLSet$logX2nd.Flr.SF<-log(amLSet$X2nd.Flr.SF+1)
amLSet$logBsmtFin.SF.1<-log(amLSet$BsmtFin.SF.1+1)
# Extract Predictions
predict.full <- exp(predict(lmt, ames_test))
predict.fullA <- exp(predict(lmtA, at1))
# Extract Residuals
resid.full <- ames_test$price - predict.full
resid.fullA <- at1$price - predict.fullA
# Calculate RMSE
rmse.full <- sqrt(mean(resid.full^2))
rmse.fullA <- sqrt(mean(resid.fullA^2))
plot(resid.full, main="Base Model Residuals for Test Data")
plot(resid.fullA, main="Base Model Residuals w/BAS for Test Data")
```
#### Test Data Residuals (Above)
```{r ltA}
print('RMSE Value for Base Training Model on Test Data');sprintf("%8.0f US Dollars",rmse.full)
print('RMSE Value for Base Training Model w/BAS Adjustment on Test Data');sprintf("%8.0f US Dollars",rmse.fullA)
```
#### Test Data RMSE (Above)
Use your model from above to generate predictions for the housing prices in the test data set. Are the predictions significantly more accurate (compared to the actual sales prices) for the training data than the test data? Why or why not? Briefly explain how you determined that (what steps or processes did you use)?
* * *
NOTE: Write your written response to section 2.5 here. Delete this note before you submit your work.
```{r initmodel_test}
plmt<-predict(lmt,ames_test,interval='predict')
pplmt<-predict(lmt,ames_train,interval='predict')
plmt<-data.frame(plmt)
pplmt<-data.frame(pplmt)
plmtA<-predict(lmtA,at1,interval='predict')
plmtA<-data.frame(plmtA)
pplmtA<-predict(lmtA,am1,interval='predict')
pplmtA<-data.frame(pplmtA)
pr01<- ggplot(ames_train, aes(ames_train$price/1000, log(ames_train$price)))+
geom_point()+
geom_line(data=pplmt, aes(y=fit),colour='blue')+
geom_ribbon(data=pplmt,aes(ymin=lwr,ymax=upr),alpha=0.1,colour='black',fill="red")+
ylab("Natural Log Price")+
scale_x_discrete(name ="Price (in $1000)" ,
limits=c(0,100,200,300,400,500,600)) +
ggtitle('Fig. IV Training Data: Predicted Natural Log of Price using Base Model ')
pr1<- ggplot(ames_test, aes(ames_test$price/1000, log(ames_test$price)))+
geom_point()+
geom_line(data=plmt, aes(y=fit),colour='red')+
geom_ribbon(data=plmt,aes(ymin=lwr,ymax=upr),alpha=0.1,colour='black',fill="blue")+
ylab("Natural Log Price")+
scale_x_discrete(name ="Price (in $1000)" ,
limits=c(0,100,200,300,400,500,600)) +
ggtitle('Fig. V Test Data: Predicted Natural Log of Price using Base Model ')
pr03<- ggplot(am1, aes(am1$price/1000, log(am1$price)))+
geom_point()+
geom_line(data=pplmtA, aes(y=fit),colour='blue')+
geom_ribbon(data=pplmtA,aes(ymin=lwr,ymax=upr),alpha=0.1,colour='black',fill="red")+
ylab("Natural Log Price")+
scale_x_discrete(name ="Price (in $1000)" ,
limits=c(0,100,200,300,400,500,600)) +
ggtitle('Fig. VI Training Data: Predicted Natural Log of Price using Base w/BAS Adjustment Model ')
pr3<- ggplot(at1, aes(at1$price/1000, log(at1$price)))+
geom_point()+
geom_line(data=plmtA, aes(y=fit),colour='red')+
geom_ribbon(data=plmtA,aes(ymin=lwr,ymax=upr),alpha=0.1,colour='black',fill="blue")+
ylab("Natural Log Price")+
scale_x_discrete(name ="Price (in $1000)" ,
limits=c(0,100,200,300,400,500,600)) +
ggtitle('Fig. VII Test Data: Predicted Natural Log of Price using Base w/BAS Adjustment Mode ')
```
#### Prediction Intvervals
In statistical inference, specifically predictive inference, a prediction interval is an estimate of an interval in which future observations will fall, with a certain probability, given what has already been observed. Prediction intervals are often used in regression analysis.
Prediction intervals are used in both frequentist statistics and Bayesian statistics: a prediction interval bears the same relationship to a future observation that a frequentist confidence interval or Bayesian credible interval bears to an unobservable population parameter: prediction intervals predict the distribution of individual future points, whereas confidence intervals and credible intervals of parameters predict the distribution of estimates of the true population mean or other quantity of interest that cannot be observed.[8]
```{r itA}
plot(pr01)
plot(pr1)
```
#### Base Model 95 percent prediction interval for Training and Test data (above)
In Fig. IV Training Data: Predicted Natural Log of Price using Base Model, a 95% confidence interval is provided for the Training Data. The characteristics of the graph are as follows:
* The black plotted points represents the Natural Log of the price as a function of price, for the Training Data. in other words $P = log(price)$
* The blue jagged line represents the predicted price from the model, using the Training Data.
* The pink region bounded by black borders is 95% prediction interval, for the Traing Data.
In Fig. V Test Data: Predicted Natural Log of Price using Base Model, a 95% confidence interval is provided for the Test Data. The characteristics of the graph are as follows:
* The black plotted points represents the Natural Log of the price as a function of price. in other words $P= log(price)$
* The red jagged line represents the predicted price from the model, using the Test Data.
* The light blue region bounded by black borders is 95% prediction interval, for the Test Data.
```{r itB}
plot(pr03)
plot(pr3)
```
#### Base Model w/BAS 95 percent prediction interval for Training and Test data (above)
Fig. VI Training Data: Predicted Natural Log of Price using Base w/BAS Adjustment Model and Fig. VII Test Data: Predicted Natural Log of Price using Base w/BAS Adjustment Mode, are similar to Figs. VI-V, with the exception that both the Training Data and the Test Data have been adjusted for BAS modeling.
Through the remainder of this report the Training Data, Test Data and the Validation Data will be presented with 95% prediction intervals that have the same characteristics as Figs. IV-VII above.
#### Summary of Overfitting Analysis
Although the RMSE is, in general, smaller for the Test Data than it is for the Training Data, other observations indicate there is overfitting inherent in the models.
An examination of the prediction intervals indicate that the models are overfitted. The width of the Training Data
interval ribbon (pink) is smaller than that of the Test Data interval ribbon (blue).
* * *
**Note to the learner:** If in real-life practice this out-of-sample analysis shows evidence that the training data fits your model a lot better than the test data, it is probably a good idea to go back and revise the model (usually by simplifying the model) to reduce this overfitting. For simplicity, we do not ask you to do this on the assignment, however.
## Part 3 Development of a Final Model
Now that you have developed an initial model to use as a baseline, create a final model with *at most* 20 variables to predict housing prices in Ames, IA, selecting from the full array of variables in the dataset and using any of the tools that we introduced in this specialization.
Carefully document the process that you used to come up with your final model, so that you can answer the questions below.
### Section 3.1 Final Model
Provide the summary table for your model.
* * *
```{r FMA, echo=FALSE}
sumStat <- matrix(c(0.6953192,0.7779075,0.8376505,0.8606549,0.8743366,0.8863358,0.8966699,0.9020662,0.9056622,0.9084827,0.9119799,
0.9144920,0.9163570,0.9181562,0.9204967,0.9222495,0.9233643,0.9245588,0.9261305,
0.6925494,0.7698440,0.8315814,0.8542348,0.8684091,0.8808493,0.8915631,0.8965227,0.8998779,
0.9027629,0.9060592,0.9083293,0.9102276,0.9120526,0.9140822,0.9155927,0.9167079,0.9177242,0.9193457),
nrow = 19, ncol = 2, byrow = FALSE,
dimnames = list(c("Overall.Qual","Neighborhood","logarea","Overall.Cond",
"Year.Built","logLot.Area","Bsmt.Full.Bath","Garage.Type",
"Sale.Condition","logX2nd.Flr.SF","Bldg.Type","Heating.QC",
"logBsmtFin.SF.1","Garage.Cars","MS.Zoning","Kitchen.Qual",
"Fireplaces","Heating","Central.Air"),
c("R Squared","Adj R Squared")))
sumStat<-data.frame(sumStat)
tLSet<-dplyr::select(ames_test,price,Overall.Qual,Lot.Area,
Year.Built,Sale.Condition,area,MS.Zoning,Year.Remod.Add,Land.Slope,
Exter.Qual,Lot.Shape,Land.Contour,Lot.Config,Street,
Bsmt.Qual,Bsmt.Cond,Bsmt.Exposure,BsmtFin.Type.1,Bldg.Type,
BsmtFin.SF.1,BsmtFin.Type.2,BsmtFin.SF.2,Bsmt.Unf.SF,
Total.Bsmt.SF,Heating.QC,Central.Air,House.Style,
Electrical,X1st.Flr.SF,X2nd.Flr.SF,
Bsmt.Full.Bath,Bsmt.Half.Bath,Full.Bath,Half.Bath,
Bedroom.AbvGr,Kitchen.AbvGr,TotRms.AbvGrd,
Functional,Fireplaces,
Garage.Yr.Blt,Garage.Finish,Garage.Cars,Garage.Area,
Paved.Drive,Wood.Deck.SF,
Open.Porch.SF,Enclosed.Porch,X3Ssn.Porch,Screen.Porch,
Fence,Misc.Val,Mo.Sold,Yr.Sold,Sale.Type,Neighborhood,Overall.Cond,Garage.Type,Kitchen.Qual,Heating)
tLSet<-data.frame(tLSet)
tLSet$logarea<-log(tLSet$area+1)
tLSet$logLot.Area<-log(tLSet$Lot.Area+1)
tLSet$logGarage.Area<-log(tLSet$Garage.Area+1)
tLSet$logX2nd.Flr.SF<-log(tLSet$X2nd.Flr.SF+1)
tLSet$logBsmtFin.SF.1<-log(tLSet$BsmtFin.SF.1+1)
tLSet$logBsmtFin.SF.2<-log(tLSet$BsmtFin.SF.2+1)
tLSet$logBsmt.Unf.SF<-log(tLSet$Bsmt.Unf.SF+1)
tLSet$logTotal.Bsmt.SF<-log(tLSet$Total.Bsmt.SF+1)
tLSet$logWood.Deck.SF<-log(tLSet$Wood.Deck.SF+1)
tLSet$logOpen.Porch.SF<-log(tLSet$Open.Porch.SF+1)
tLSet$logX1st.Flr.SF<-log(tLSet$X1st.Flr.SF+1)
#summary(lmt2)
atLSet2<-dplyr::select(tLSet,price,Overall.Qual,Neighborhood,logarea,
Overall.Cond,Year.Built,logLot.Area,Bsmt.Full.Bath,
Garage.Type,Sale.Condition,logX2nd.Flr.SF,Bldg.Type,
Heating.QC,logBsmtFin.SF.1,Garage.Cars,MS.Zoning,
Kitchen.Qual,Heating,Central.Air,
Fireplaces)
ate<-atLSet2
#nrow(ate)
ate<-atLSet2
ate<-filter(ate,Overall.Qual!='1')
ate<-filter(ate,Overall.Qual!='2')
ate<-filter(ate,Overall.Qual!='3')
ate<-filter(ate,Overall.Qual!='10')
ate<-filter(ate,Heating!='OthW')
#nrow(ate)
ate<-filter(ate,MS.Zoning!='C (all)')
ate<-filter(ate,Sale.Condition!='Partial')
ate<-filter(ate,Heating.QC!='Po')
#nrow(ate)
ate<-filter(ate,MS.Zoning!='RH')
#nrow(ate)
ate<-data.frame(ate)
#nrow(ames)
#nrow(ate)
ate<-filter(ate,Overall.Qual!='1')
#nrow(ate)
ate<-filter(ate,Overall.Cond!='3')
ate<-filter(ate,Overall.Cond!='2')
ate<-filter(ate,Overall.Cond!='10')
ate<-filter(ate,Heating!='OthW')
ate<-filter(ate,Heating!='Wall')
#nrow(ate)
ate<-filter(ate,Sale.Condition!='Alloca')
ate<-filter(ate,Sale.Condition!='Family')
ate<-filter(ate,Bldg.Type!='2fmCon')
#nrow(ate)
ate<-data.frame(ate)
amLSet<-ames_train
amLSet<-data.frame(amLSet)
amLSet$logarea<-log(amLSet$area+1)
amLSet$logLot.Area<-log(amLSet$Lot.Area+1)
amLSet$logX2nd.Flr.SF<-log(amLSet$X2nd.Flr.SF+1)
amLSet$logBsmtFin.SF.1<-log(amLSet$BsmtFin.SF.1+1)
amSet2<-dplyr::select(amLSet,price,Overall.Qual,Neighborhood,logarea,
Overall.Cond,Year.Built,logLot.Area,Bsmt.Full.Bath,
Garage.Type,Sale.Condition,logX2nd.Flr.SF,Bldg.Type,
Heating.QC,logBsmtFin.SF.1,Garage.Cars,MS.Zoning,
Kitchen.Qual,Heating,Central.Air,
Fireplaces)
ames<-amSet2
ames<-filter(ames,Overall.Qual!='1')
#nrow(ames)
ames<-filter(ames,Overall.Cond!='3')
#nrow(ames)
ames<-filter(ames,Sale.Condition!='AdjLand')
#nrow(ames)
ames<-filter(ames,Sale.Condition!='Alloca')
ames<-filter(ames,Sale.Condition!='Family')
ames<-filter(ames,Bldg.Type!='2fmCon')
#nrow(ames)
ames<-filter(ames,MS.Zoning!='I (all)')
#nrow(ames)
ames<-data.frame(ames)
aves<-ames
```
```{r model_playground,cache=TRUE}
lmt2<-lm(log(price)~.,data=amSet2)
summary(lmt2)
```
* * *
### Section 3.2 Transformation
Did you decide to transform any variables? Why or why not? Explain in a few sentences.
* * *
#### Training Data Transformations
The only data transfromations that were rendered was a conversion of area, in square units, to natural log of area.
The independent predictor variables that were transformed are provided in the code below.
The transformation came as a consequence of observing the area,in units squared, being right skewed on a histogram plots.
The response variable, price, was also rendered using the natural log function.
```{r model_assess,cache=TRUE}
amLSet<-ames_train
amLSet<-data.frame(amLSet)
amLSet$logarea<-log(amLSet$area+1)
amLSet$logLot.Area<-log(amLSet$Lot.Area+1)
amLSet$logX2nd.Flr.SF<-log(amLSet$X2nd.Flr.SF+1)
amLSet$logBsmtFin.SF.1<-log(amLSet$BsmtFin.SF.1+1)
amSet2<-dplyr::select(amLSet,price,Overall.Qual,Neighborhood,logarea,
Overall.Cond,Year.Built,logLot.Area,Bsmt.Full.Bath,
Garage.Type,Sale.Condition,logX2nd.Flr.SF,Bldg.Type,
Heating.QC,logBsmtFin.SF.1,Garage.Cars,MS.Zoning,
Kitchen.Qual,Heating,Central.Air,
Fireplaces)
```
* * *
### Section 3.3 Variable Interaction
Did you decide to include any variable interactions? Why or why not? Explain in a few sentences.
* * *
#### Colinearity of Data
The variables that contribute the most to the final Adjusted $R^2$are chosen from their F-Statistic value determined by an anova model analysis. The colinearity between these variables is investigated and it was determined that no such relationship exists.
```{r model_inter, cache=TRUE,echo=FALSE}
ames<-amSet2
#nrow(ames)
ames<-filter(ames,Overall.Qual!='1')
ames<-filter(ames,Overall.Qual!='3')
#nrow(ames)
ames<-filter(ames,Sale.Condition!='AdjLand')
ames<-filter(ames,Heating!='OthW')
ames<-filter(ames,Sale.Condition!='Partial')
ames<-filter(ames,MS.Zoning!='C (all)')
#nrow(ames)
ames<-filter(ames,MS.Zoning!='RH')
ames<-filter(ames,Overall.Qual!='2')
ames<-filter(ames,Overall.Qual!='10')
#nrow(ames)
ames<-data.frame(ames)
aves<-ames