Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
716 views
in Technique[技术] by (71.8m points)

calculate r square (Multiple determination coefficient) using GBM in R

I perform GBM models. Data sample

a=structure(list(yield = c(1842L, 2147L, 2444L, 3850L, 1866L, 2897L, 
1783L, 2434L, 2650L, 2863L), time.diff = c(122, 186, 177, 168, 
162, 186, 161, 125, 187, 185), Biomass = c(18400L, 6400L, 8620L, 
12800L, 5400L, 10400L, 6000L, 8800L, 9080L, 60000L)), class = "data.frame", row.names = c(NA, 
-10L))

my code

indexes = createDataPartition(a$yield, p = .7, list = F)
train = a[indexes, ]
test = a[-indexes, ]

write.csv(test,"test.csv")

ames_train <- train
ames_test  <- test

str(ames_train)

# train GBM model
gbm.fit <- gbm(
  formula = yield ~ .,
  distribution = "gaussian",
  data = ames_train,
  n.trees = 10000,
  interaction.depth = 1,
  shrinkage = 0.001,
  cv.folds = 5,
  n.cores = NULL, # will use all cores by default
  verbose = FALSE
)  


# print results
print(gbm.fit)


# get MSE and compute RMSE
sqrt(min(gbm.fit$cv.error))

Here indicated MSE and RMSE How can i calculate r square (Multiple determination coefficient) for this model?

question from:https://stackoverflow.com/questions/65885376/calculate-r-square-multiple-determination-coefficient-using-gbm-in-r

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This way you get the r2 with predictions obtained by cross-validation, which are true predictions.

r <- as.numeric(gbm.fit$cv.statistics[3])

rsq = round(r^2,2)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...