R语言光谱的预处理和LDA


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")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值