library(ggplot2)
library(hrbrthemes)
library(caret)
library(ggplot2)
library(graphics)
library(baseline)
data <- read.csv('G:/canjia/513/base.csv',header = T)
data[,-1] <- scale(data[,-1])
#去除数字前x
sub=sub('^X','',colnames(data)[-1])
colnames(data)[-1] <- sub
nir <- as.matrix(spec[,-1])
spec_name <- colnames(spec)[-1]
#snv处理
snv_spec <- standardNormalVariate(nir)
matplot(spec_name,t(snv_spec),type = 'l')
snv_spec <- cbind(spec$typee, snv_spec)
colnames(snv_spec)[1] <- 'typee'
# SG11平滑1阶和2阶
data_ref <- spec[ ,-1]
sg_data_ref <- savitzkyGolay(data_ref,0,2,11)
dim(sg_data_ref)
matplot(colnames(sg_data_ref),t(sg_data_ref),type = 'l')
sg_data_ref <- cbind(spec$typee, sg_data_ref)
colnames(sg_data_ref)[1] <- 'typee'
write.csv (sg_data_ref, file ="G:/canjia/513/sg.csv",row.names = FALSE)
sg_data_ref2 <- savitzkyGolay(data_ref,1,2,11)
dim(sg_data_ref2)
matplot(colnames(sg_data_ref2),t(sg_data_ref2),type = 'l')
sg_data_ref3 <- savitzkyGolay(data_ref,2,2,11)
dim(sg_data_ref3)
matplot(colnames(sg_data_ref3),t(sg_data_ref3),type = 'l')
sg_data_ref4 <- savitzkyGolay(snv_spec[,-1],0,2,11)
dim(sg_data_ref4)
matplot(colnames(sg_data_ref4),t(sg_data_ref4),type = 'l')
sg_data_ref5 <- savitzkyGolay(snv_spec[,-1],1,2,11)
dim(sg_data_ref5)
matplot(colnames(sg_data_ref5),t(sg_data_ref5),type = 'l')
sg_data_ref6 <- savitzkyGolay(snv_spec[,-1],2,2,11)
dim(sg_data_ref6)
matplot(colnames(sg_data_ref6),t(sg_data_ref6),type = 'l')
#读取处理后文件
data <- read.csv('G:/canjia/513/sg.csv',header = T)
#lda funtion
library(heplots)
set.seed(222)
index<-sample(1:nrow(data),0.7*nrow(data))
#训练集
dftrain<-data[index,]
#测试集
dftest<-data[-index,]
dftrain$typee <- as.factor(dftrain$typee)
dftest$typee <- as.factor(dftest$typee)
#boxM(cbind(data[2:513])~typee,data=data)
library(MASS) #导入包MASS
species<- lda(typee ~. , data = dftrain)
speciesPredict <- predict(species,newdata = dftest)
newGroup <-speciesPredict$class
newGroup
cbind(dftest$typee, speciesPredict$x , newGroup)
tab <- table(dftest$typee, newGroup)
tab
confusion_matrix <- confusionMatrix(dftest$typee, newGroup)
confusion_matrix_data <- confusion_matrix$table
# 使用ggplot2包绘制混淆矩阵图
summary(f1)
fig2 <- ggplot(data=as.data.frame(confusion_matrix_data), aes(x=Reference, y=Prediction, fill=Freq)) +
geom_tile() +
geom_text(aes(label=sprintf("%d", Freq)), vjust=1) +
scale_fill_gradient(low="white", high="blue") +
theme_minimal() +
labs(x="True Class", y="Predicted Class", fill="Frequency")
fig2
library(eoffice)
topptx(fig2,filename = "G:/canjia/513/lda.pptx")
R语言光谱的预处理和LDA
最新推荐文章于 2024-10-16 00:10:31 发布