-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code_dubont.R
223 lines (181 loc) · 8.17 KB
/
Code_dubont.R
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
setwd("~/Documents/Northwestern/WorkingFiles/MSDS_454/Forest_Cover")
library(R.utils)
gunzip("covtype.data.gz")
cat("\n","----- FORMAT DATA -----","\n")
forest <- read.table("covtype.data", sep =",", dec = ".", header = FALSE)
col_headings <- c('elevation',
'aspect',
'slope',
'horiz_dist_hydr',
'vert_dist_hydr',
'horiz_dist_road',
'hillshade_9h',
'hillshade_12h',
'hillshade_15h',
'horiz_dist_fire',
'wld_area_rawah',
'wld_area_neota',
'wld_area_comanche',
'wld_area_cache',
'soil_type_1',
'soil_type_2',
'soil_type_3',
'soil_type_4',
'soil_type_5',
'soil_type_6',
'soil_type_7',
'soil_type_8',
'soil_type_9',
'soil_type_10',
'soil_type_11',
'soil_type_12',
'soil_type_13',
'soil_type_14',
'soil_type_15',
'soil_type_16',
'soil_type_17',
'soil_type_18',
'soil_type_19',
'soil_type_20',
'soil_type_21',
'soil_type_22',
'soil_type_23',
'soil_type_24',
'soil_type_25',
'soil_type_26',
'soil_type_27',
'soil_type_28',
'soil_type_29',
'soil_type_30',
'soil_type_31',
'soil_type_32',
'soil_type_33',
'soil_type_34',
'soil_type_35',
'soil_type_36',
'soil_type_37',
'soil_type_38',
'soil_type_39',
'soil_type_40',
'cover_types'
)
colnames(forest) <- col_headings
is.data.frame(forest)
cat("\n","----- SUMMARY STATISTICS -----","\n")
print(str(forest))
dim(forest)
sum(is.na(forest))
summary(forest)
cat("\n","----- VARIABLE TRANSFORMATIONS -----","\n")
forest.t <- forest
forest.t$wld_area_rawah <- as.factor(forest$wld_area_rawah)
forest.t$wld_area_neota <- as.factor(forest$wld_area_neota)
forest.t$wld_area_comanche <- as.factor(forest$wld_area_comanche)
forest.t$wld_area_cache <- as.factor(forest$wld_area_cache)
forest.t$soil_type_1 <- as.factor(forest$soil_type_1)
forest.t$soil_type_2 <- as.factor(forest$soil_type_2)
forest.t$soil_type_3 <- as.factor(forest$soil_type_3)
forest.t$soil_type_4 <- as.factor(forest$soil_type_4)
forest.t$soil_type_5 <- as.factor(forest$soil_type_5)
forest.t$soil_type_6 <- as.factor(forest$soil_type_6)
forest.t$soil_type_7 <- as.factor(forest$soil_type_7)
forest.t$soil_type_8 <- as.factor(forest$soil_type_8)
forest.t$soil_type_9 <- as.factor(forest$soil_type_9)
forest.t$soil_type_10 <- as.factor(forest$soil_type_10)
forest.t$soil_type_11 <- as.factor(forest$soil_type_11)
forest.t$soil_type_12 <- as.factor(forest$soil_type_12)
forest.t$soil_type_13 <- as.factor(forest$soil_type_13)
forest.t$soil_type_14 <- as.factor(forest$soil_type_14)
forest.t$soil_type_15 <- as.factor(forest$soil_type_15)
forest.t$soil_type_16 <- as.factor(forest$soil_type_16)
forest.t$soil_type_17 <- as.factor(forest$soil_type_17)
forest.t$soil_type_18 <- as.factor(forest$soil_type_18)
forest.t$soil_type_19 <- as.factor(forest$soil_type_19)
forest.t$soil_type_20 <- as.factor(forest$soil_type_20)
forest.t$soil_type_21 <- as.factor(forest$soil_type_21)
forest.t$soil_type_22 <- as.factor(forest$soil_type_22)
forest.t$soil_type_23 <- as.factor(forest$soil_type_23)
forest.t$soil_type_24 <- as.factor(forest$soil_type_24)
forest.t$soil_type_25 <- as.factor(forest$soil_type_25)
forest.t$soil_type_26 <- as.factor(forest$soil_type_26)
forest.t$soil_type_27 <- as.factor(forest$soil_type_27)
forest.t$soil_type_28 <- as.factor(forest$soil_type_28)
forest.t$soil_type_29 <- as.factor(forest$soil_type_29)
forest.t$soil_type_30 <- as.factor(forest$soil_type_30)
forest.t$soil_type_31 <- as.factor(forest$soil_type_31)
forest.t$soil_type_32 <- as.factor(forest$soil_type_32)
forest.t$soil_type_33 <- as.factor(forest$soil_type_33)
forest.t$soil_type_34 <- as.factor(forest$soil_type_34)
forest.t$soil_type_35 <- as.factor(forest$soil_type_35)
forest.t$soil_type_36 <- as.factor(forest$soil_type_36)
forest.t$soil_type_37 <- as.factor(forest$soil_type_37)
forest.t$soil_type_38 <- as.factor(forest$soil_type_38)
forest.t$soil_type_39 <- as.factor(forest$soil_type_39)
forest.t$soil_type_40 <- as.factor(forest$soil_type_40)
forest.t$cover_types <- as.factor(forest$cover_types)
cat("\n","----- RESTRUCTURING OF DUMMY VARIABLES -----","\n")
install.packages("caret")
library(caret)
forest.t$wld_area <- names(forest.t[11:14])[max.col(forest.t[11:14])]
table(forest.t$wld_area)
forest.t$soil_type <- names(forest.t[15:54])[max.col(forest.t[15:54])]
table(forest.t$soil_type)
cat("\n","----- ANALYSIS OF VARIABLES - TABLES & CORRELATION -----","\n")
library(corrplot)
corr= cor(forest[, c(1:10,55)])
corrplot(corr,method="color", outline=T, cl.pos="n", rect.col="black", tl.col="indianred4", addCoef.col="black", number.digits=2, number.cex=0.60, tl.cex=0.7, cl.cex=1, col=colorRampPalette(c("green4", "white", "yellow"))(100))
corr
#produce list of correlations by highest value
corr[lower.tri(corr,diag=TRUE)]=NA #Prepare to drop duplicates and meaningless information
corr=as.data.frame(as.table(corr)) #Turn into a 3-column table
corr=na.omit(corr)
corr=corr[order(-abs(corr$Freq)),] #Sort by highest correlation (whether +ve or -ve)
corr
#highest corr
cat("\n","----- OUTLIER DETECTION -----","\n")
# Multi-variate outlier detection
for(i in 57:length(forest.t)) {
if(is.numeric(forest.t[, i])) {
formula <- paste0(names(forest)[i], "~.-ID")
outlier_check <- lm(formula=formula, data=forest.t)
cooksd <- cooks.distance(outlier_check)
pct_outliers <- round(100*sum(ifelse(cooksd>4*mean(cooksd, na.rm=T),1,0))/length(forest.t[,i]), digits=0)
png(paste0("./imgs/cooksd_",names(forest.t)[i], ".png"))
plot(cooksd, pch="*", cex=2,
main=paste0("Influential Obs for ", names(forest.t)[i], " by Cooks distance: ", pct_outliers, "%")) # plot cook's distance
abline(h = 4*mean(cooksd, na.rm=T), col="red") # add cutoff line
text(x=1:length(cooksd)+1, y=cooksd,
labels=ifelse(cooksd>4*mean(cooksd, na.rm=T),names(cooksd),""), col="red") # add labels
dev.off()
}
}
outlier_check
cat("\n","---------- Identify most important variable using random forests for class variables ----------","\n")
library(tree)
tree.forest.t=tree(cover_types~., forest.t)
summary(tree.forest.t)
par(mfrow=c(1,1))
plot(tree.forest.t)
text(tree.forest.t, pretty=0)
tree.forest.t
cat("\n","----- Perform Graphical EDA - Histograms -----","\n")
require(ggplot2)
library(ggplot2)
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=elevation))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=aspect))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=slope))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=horiz_dist_hydr))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=vert_dist_hydr))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=horiz_dist_road))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=hillshade_9h))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=hillshade_12h))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=hillshade_15h))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=horiz_dist_fire))
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=wld_area), stat="count")
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=soil_type), stat="count")
ggplot(data=forest.t) + geom_histogram(mapping=aes(x=cover_types), stat="count")
dev.off()
cat("\n","----- Perform Graphical EDA - Boxplots -----","\n")
forest.t$cover_types <- as.numeric(cover_types)
boxplot(cover_types~elevation,data=forest.t, xlab="Elevation", ylab="Cover_Types")
plot(forest.t$elevation, forest.t$cover_types, ylab="Cover_Types", xlab="Elevation")