r - 如何在r中重新采样LDA?

标签 r resampling statistics-bootstrap

我认为使用 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 获取 LD1LD2 系数的标准误差。此外,代码的初始部分显示了 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/

相关文章:

r - 如何获得 'unbalanced' 的 ggplots 网格?

python - Pandas:重新采样数据框列,获取对应于最大值的离散特征

R:从引导结果向量计算 BCa

R:使用多级模型引导

r - 将样本名称添加到用 s.class 绘制的 PCA

r - 如何在 igraph + R 中保持节点的位置

Pandas 时间序列比较

通过簇替换重新采样

r - 如何在嵌套列表中使用 for 循环?

Python Pandas DataFrame 根据周一至周日的每周定义将每日数据重新采样为一周?