我认为使用 bootstrap 会重新采样我的 LDA,但我不确定。此外,如果 Bootstrap 确实有效,我不确定如何在 r 中编写 Bootstrap 这是我的 LDA 代码:
library('MASS')
n=nrow(iris)
train = sample(n ,size = floor(n*0.75), replace = F)
train.species =Species[train]
test.species=Species[-train]
lda.fit = lda(Species~. , data=iris, subset=train)
最佳答案
下面的代码使用boot()
library使用 LDA 对 iris 数据集执行 Bootstrap 获取 LD1
和 LD2
系数的标准误差。此外,代码的初始部分显示了 LDA 拟合,无需使用相同系数的 Bootstrap 。
# Library
library(MASS)
library(boot)
# Get data
data(iris)
names(iris) <- gsub("\\.", "", names(iris)) #remove dots from column names
# Split data into train and test sets
train_index <-sample(seq(nrow(iris)),floor(dim(iris)[1]*0.75))
train <- iris[train_index,]
test <- iris[-train_index,]
test_Y <- test[, c('Species')]
test_X <- subset(test, select=-c(Species))
#### LDA without bootstrap:
# Fit LDA to train data:
lda.fit = lda(Species ~ . , data=train)
lda.fit
# Predict test_Y based on lda.fit above
lda.pred <- predict(lda.fit, test_X)
lda.class <- lda.pred$class
# Confusion matrix
table(lda.class, test_Y)
#### LDA with bootstrap:
# Fit LDA to train data: to get standard errors for coefficients
set.seed(1)
boot.fn <- function(data,index){
return(coefficients(lda(Species ~ SepalLength + SepalWidth + PetalLength + PetalWidth, data=data, subset=index)))
}
# Call boot(): This returns LD1 and LD2 for each predictor
boot(train, boot.fn, 1000)
# NOTE: Here, in Bootstrap Statistics output, t1* to t4* are LD1 coefficients and t5* to t8* are LD2 coefficients
关于r - 如何在r中重新采样LDA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42895242/