nira: Near-infrared data analysis

Last update: 10.10.2020

Author: M.Sc André Dantas de Medeiros (Universidade Federal de viçosa). Email: andre.d.medeiros@ufv.br

This package uses functions developed from the following packages: pls, prospectr, dplyr, reshape2, ggplot2, ggpubr, factoextra, FactoMineR, readxl, plyr, caret, e1071. We thank the creators of these useful packages.

Warning:

Installation and loading

#Install
if(!require(devtools)) install.packages("devtools")
if(!require(nira)) devtools::install_github("admedeiros/nira")

#Load package
library(nira)

For package update

if(!require(devtools)) install.packages("devtools")
if(require(nira)) remove.packages("nira")
.rs.restartR()
if(!require(nira)) devtools::install_github("admedeiros/nira")
#Load package
library(nira)

Main functions:

import_csv(): Import and merge CSV NIR files

plotmean_df(): Create graphics with the average of the spectra class.

plotraw_df(): Create graphics with the raw of the spectra class.

center_df(): Center the data on the average.

autoscaling_df(): Autoscaling the data using the Z-score transformation.

snv_df(): Standard Normal Variate (SNV) on near-infrared (NIR) data.

msc_df(): Multiplicative Scatter Correction (MSC) on near-infrared (NIR) data.

der_df(): Applying derivatives with Savitzky-Golay smoothing..

pca_df(): Plots a graph of the principal component analysis.

classification_df(): Function to develop classification models

load_classifier_df(): Function to load and apply classifier created with the classification_df function

prediction_df_df(): Function to apply the classifier to new data without labels

library(nira)
library(ggpubr) #for ggarrange
# raw spectra
a<-plotraw_df(nir_seed)
# mean spectra per class
b<-plotmean_df(nir_seed)
# center
c<-plotraw_df(center_df(nir_seed))
d<-plotmean_df(center_df(nir_seed))
# Autoscaling
e<-plotraw_df(autoscaling_df(nir_seed))
f<-plotmean_df(autoscaling_df(nir_seed))
# SNV
g<-plotraw_df(snv_df(nir_seed))
h<-plotmean_df(snv_df(nir_seed))
# MSC
i<-plotraw_df(msc_df(nir_seed))
j<-plotmean_df(msc_df(nir_seed))
# 1st derivative with Savitzky-Golay smoothing
k<-plotraw_df(der_SG(nir_seed,1))
l<-plotmean_df(der_SG(nir_seed,1))
# 2nd derivative with Savitzky-Golay smoothing
m<-plotraw_df(der_SG(nir_seed,2))
n<-plotmean_df(der_SG(nir_seed,2))

x<-ggarrange(a, b,c,d,e,f,g,h,i,j,k,l,m,n, common.legend = TRUE,
          labels = c("Raw spectra", "Mean spectra", "Center          ", "","Autoscaling","","SNV               ","","MSC              ", "","1st derivative", "", "2nd derivative", "" ),
          font.label = list(size=12),
          hjust = -0.7,
          vjust = 1,
          ncol = 2, nrow = 7)
dados<-nir_seed
classification<-classification_df(dados)
p<-classification$plsplot
q<-pca_df(nir_seed)

y<-ggarrange(q,p,
             labels = c("PCA", "PLS-DA"),
             hjust = -8,
              vjust = 2,
              ncol = 1, nrow = 2)
ggarrange(x,y,
          labels = c("Spetral",""),
          ncol = 2, nrow = 1)

Examples

Spectra Visualization

library(nira)
library(ggpubr) #for ggarrange
# raw spectra
a<-plotraw_df(nir_seed)
# mean spectra per class
b<-plotmean_df(nir_seed)
# center
c<-plotraw_df(center_df(nir_seed))
d<-plotmean_df(center_df(nir_seed))
# Autoscaling
e<-plotraw_df(autoscaling_df(nir_seed))
f<-plotmean_df(autoscaling_df(nir_seed))
# SNV
g<-plotraw_df(snv_df(nir_seed))
h<-plotmean_df(snv_df(nir_seed))
# MSC
i<-plotraw_df(msc_df(nir_seed))
j<-plotmean_df(msc_df(nir_seed))
# 1st derivative with Savitzky-Golay smoothing
k<-plotraw_df(der_SG(nir_seed,1))
l<-plotmean_df(der_SG(nir_seed,1))
# 2nd derivative with Savitzky-Golay smoothing
m<-plotraw_df(der_SG(nir_seed,2))
n<-plotmean_df(der_SG(nir_seed,2))

ggarrange(a, b,c,d,e,f,g,h,i,j,k,l,m,n,
  labels = c("Raw spectra", "Mean spectra", "Center", "Center","Autoscaling","Autoscaling","SNV","SNV","MSC", "MSC","1st derivative", "1st derivative", "2nd derivative", "2nd derivative" ),
  ncol = 2, nrow = 7)

View of the exploratory principal component analysis

pca_df(nir_seed,1,2)

Classification

data(nir_seed)
classification<-classification_df(nir_seed,metrics = 2)
Cross-validation
classification$`Cross-validation`
Train results
classification$Training_results
Test results
classification$Testing_results
PLS-DA Plot
classification$plsplot
Variable importance
classification$`Variable importance`

Load the pre-trained model and validate their performance in external labeled data

data(nir_seed)
load_classifier_df(nir_seed)

Load the pre-trained model and use on data without labeling

data(nir_seed)
pc<-prediction_df(nir_seed[,-313])
pca_df(pc)


admedeiros/nira documentation built on Oct. 13, 2020, 8:35 p.m.