首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何对r中的LDA进行重采样?

如何对r中的LDA进行重采样?
EN

Stack Overflow用户
提问于 2017-03-20 11:12:03
回答 1查看 730关注 0票数 2

我认为使用bootstrap可以重新采样我的LDA,但我不确定。另外,如果bootstrap可以工作,我不确定如何在r中编写bootstrap。下面是我的LDA代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2018-07-11 11:34:05

下面的代码使用boot() libraryiris数据集上使用LDA执行引导,以获得LD1LD2系数的标准误差。此外,代码的初始部分显示了不具有相同系数的自举LDA拟合。

代码语言:javascript
复制
# 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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42895242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档