-
Notifications
You must be signed in to change notification settings - Fork 0
/
oob_test_check.R
151 lines (105 loc) · 4.33 KB
/
oob_test_check.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
len_fmla <- length(attr(terms(fmla), "term.labels"))
test.err = double(len_fmla)
oob.err = double(len_fmla)
for(mtry in 1:len_fmla)
{
set.seed(1234)
rf=randomForest(fmla, webb_train,mtry=mtry,ntree=500)
oob.err[mtry] = rf$mse[500] #Error of all Trees fitted
pred<-predict(rf,webb_test) #Predictions on Test Set for each Tree
test.err[mtry]= with(webb_test, mean( (CUM12_Mboe - pred)^2)) #Mean Squared Test Error
cat(mtry," ") #printing the output to the console
}
matplot(1:mtry , cbind(oob.err,test.err), pch=19,
col=c("red","blue"),type="b",ylab="Mean Squared Error",
xlab="Number of Predictors Considered at each Split")
legend("topright",legend=c("Out of Bag Error","Test Error"),pch=19, col=c("red","blue"))
plot(rf)
##create test / train without caret
set.seed(1234)
N <- nrow(webb_public)
target <- round(N * 0.75)
gp <- runif(N)
webb_train2 <- webb_public[gp < 0.75, ]
webb_test2 <- webb_public[gp > 0.75, ]
##=====================================================================
# Randomly assign rows to ids (1/2/3 represents train/valid/test)
# This will generate a vector of ids of length equal to the number of rows
# The train/valid/test split will be approximately 70% / 15% / 15%
set.seed(12)
assign <- sample(1:3, size = nrow(webb_public), prob = c(0.7, 0.15, 0.15), replace = TRUE)
# Create a train, validation and tests from the original data frame
webb_train <- webb_public[assign == 1, ] # subset the grade data frame to training indices only
webb_valid <- webb_public[assign == 2, ] # subset the grade data frame to validation indices only
webb_test <- webb_public[assign == 3, ] # subset the grade data frame to test indices only
##=====================================================================
dflist <- list(dt_op, train_sm, test_sm)
dfname <- c("dt_op", "train_sm", "test_sm")
#dflist <- list(webb_public, webb_train, webb_test)
#dfname <- c("webb_public", "webb_train", "webb_test")
#dflist <- list(webb_public, webb_train, webb_valid, webb_test)
#dfname <- c("webb_public", "webb_train", "webb_validate", "webb_test")
metric <- data.frame()
for(i in 1:length(dflist)){
df <- data.frame(dflist[i])
df_tbl <- data.frame(wellcount = nrow(df))
df_tbl$mean <- mean(df$CUM365_Mboe, na.rm = TRUE)
df_tbl$median <- median(df$CUM365_Mboe, na.rm = TRUE)
df_tbl$variance <- var(df$CUM365_Mboe, na.rm = TRUE)
df_tbl$sd <- sd(df$CUM365_Mboe, na.rm = TRUE)
df_tbl$P10 <- quantile(df$CUM365_Mboe, probs = (0.90), na.rm = TRUE)
df_tbl$P90 <- quantile(df$CUM365_Mboe, probs = (0.10), na.rm = TRUE)
#df_tbl$SoPhiH <- mean(df$SoPhiH_LEF, na.rm = TRUE)
#df_tbl$tvd <- mean(df$TotalDepthTVD, na.rm = TRUE)
metric <- rbind(metric, df_tbl)#store the results of each loop
#rownames(metric) <- dfname[i]
#print(metric)
}
rownames(metric) <- dfname
metric
comb_webb <- webb_train %>%
mutate(test_train = "train") %>%
bind_rows(., webb_test %>%
mutate(test_train = "test"))
#write.csv(comb_dt, file = "comb_dt.csv")
comb_webb %>%
group_by(OperatorName) %>%
summarise(WellCount = n(),
Avg_CUM12_Mboe = mean(CUM12_Mboe),
Med_CUM12_Mboe = median(CUM12_Mboe),
Avg_Lat_Length = mean(Eff_Lat),
Avg_Lbs_Ft = mean(Lbs_Ft),
Avg_Bbl_Ft = mean(Bbl_Ft))
##====================================================================
##test/train----
#create test / train dataset
set.seed(1234)
trainRow <- createDataPartition(dt$CUM365_Mboe, p = 0.75, list = FALSE)
train_sm <- dt[trainRow, ]
test_sm <- dt[-trainRow, ]
set.seed(1234)
N <- nrow(webb_public)
target <- round(N * 0.75)
gp <- runif(N)
webb_train_random <- webb_public[gp < 0.75, ]
webb_test_random <- webb_public[gp > 0.75, ]
table(dt$Team)/nrow(dt)
table(train_sm$Team)/nrow(train_sm)
table(test_sm$Team)/nrow(test_sm)
dt %>%
group_by(Team, ZONE) %>%
summarise(n = n()) %>%
mutate(rel.freq = paste0(round(100 * n/nrow(dt)), "%"))
train_sm %>%
group_by(Team, ZONE) %>%
summarise(n = n()) %>%
mutate(rel.freq = paste0(round(100 * n/nrow(train_sm)), "%"))
test_sm %>%
group_by(Team, ZONE) %>%
summarise(n = n()) %>%
mutate(rel.freq = paste0(round(100 * n/nrow(test_sm)), "%"))
##====================================================================
#public dataset
table(dt$Team)/nrow(dt)
table(train_sm$Team)/nrow(train_sm)
table(test_sm$Team)/nrow(test_sm)