-
Notifications
You must be signed in to change notification settings - Fork 0
/
yield_prediction.R
61 lines (54 loc) · 2.02 KB
/
yield_prediction.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
# DATA
train<- read.table("training.csv", sep=",", header = T)
test <- read.table("testing.csv", sep=",", header = T)
## remove NA
train <- train[complete.cases(train), ]
# Modeling
library(doParallel)
library(caret)
registerDoParallel(cores = 6)
# detectCores() 8 cores for the computer
## RandomForest
system.time(
m_rf <- train(Yield ~ . - fid - SR_L - NR_L, data = train,
method = "rf", importance = T,
trControl = trainControl(method = 'none'), metric = "RMSE" ) # method=rf random forest
)
pred_rf <- predict(m_rf)
cor(train$Yield, pred_rf)**2
# [1] 0.9502894
## Regulized Random Forest Method
grid <- data.frame(mtry=seq(2,20,by=2))
trcontrol <- trainControl(method = "cv",p = 0.7,search = "grid")
system.time(
m_rf <- train(Yield ~ . - fid - SR_L - NR_L, data = train,
method = "RRF", importance = T,
trControl = trcontrol, metric = "Rsquared") # method=rf random forest
)
pred_rf <- predict(m_rf)
cor(train$Yield, pred_rf)**2
# [1] 0.9515566
grid <- data.frame(mtry=seq(2,20,by=1))
trcontrol <- trainControl(method = "cv",p = 0.7,search = "grid")
system.time(
m_rf <- train(Yield ~ . - fid - SR_L - NR_L, data = train,
method = "RRF", importance = T,
trControl = trcontrol, metric = "Rsquared") # method=rf random forest
)
pred_rf <- predict(m_rf)
cor(train$Yield, pred_rf)**2
# [1] 0.9525071
grid <- data.frame(mtry=seq(2,20,by=2))
trcontrol <- trainControl(method = "cv",p = 0.5,search = "grid")
system.time(
m_rf <- train(Yield ~ . - fid - SR_L - NR_L, data = train,
method = "RRF", importance = T,
trControl = trcontrol, metric = "Rsquared") # method=rf random forest
)
pred_rf <- predict(m_rf)
cor(train$Yield, pred_rf)**2
# [1] 0.9514461
train$Pred_Y <- pred_rf # happy outcome for the training dataset
test$Yield <- predict(m_rf, newdata = test)
write.csv(test[,c("fid","Yield")], file = "submission.csv", row.names = FALSE)
train$Pred_Y <- pred_rf