R 包(基线)应用程序到示例数据集

R-package(baseline) application to sample dataset

我正在尝试在我拥有的示例数据集上使用 R 基线包,以测试和评估我拥有的当前基线算法。

我想应用 fillpeaks 算法作为趋势线进行比较。

bc.fillPeaks <- baseline(milk$spectra[1, drop=FALSE], lambda=6,
                         hwi=50, it=10, int=2000, method="fillPeaks")
plot(bc.fillPeaks)

但我的问题是我的示例数据不符合示例中使用的矩阵结构。当我查看示例中使用的 data.frame 时,我不明白

'data.frame':   45 obs. of  2 variables
 $ cow    : num  0 0.25 0.375 0.875 0.5 0.75 0.5 0.125 0 0.125 ...
 $ spectra: num [1:45, 1:21451] 1029 371 606 368 554 ...
  ..- attr(*, "dimnames")=List of 2
  .. ..$ : NULL
  .. ..$ : chr  "4999.94078628963" "5001.55954267662" "5003.17856106153" "5004.79784144435" ...
 - attr(*, "terms")=Classes 'terms', 'formula' length 3 cow ~ spectra
  .. ..- attr(*, "variables")= language list(cow, spectra)
  .. ..- attr(*, "factors")= int [1:2, 1] 0 1
  .. .. ..- attr(*, "dimnames")=List of 2
  .. .. .. ..$ : chr [1:2] "cow" "spectra"
  .. .. .. ..$ : chr "spectra"
  .. ..- attr(*, "term.labels")= chr "spectra"
  .. ..- attr(*, "order")= int 1
  .. ..- attr(*, "intercept")= int 1
  .. ..- attr(*, "response")= int 1
  .. ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 
  .. ..- attr(*, "predvars")= language list(cow, spectra)
  .. ..- attr(*, "dataClasses")= Named chr [1:2] "numeric" "nmatrix.21451"
  .. .. ..- attr(*, "names")= chr [1:2] "cow" "spectra"

因此,我的问题是,如果你们中的任何人有使用基线包和数据集(牛奶)的经验,以及我如何转换结构化数据集的想法:日期,访问,Old_baseline_visits 拟合和测试 R 包中的基线算法

我使用过基线,起初发现它有点混乱,尤其是示例数据。正如帮助文件中所说,基线需要一个矩阵,其中的光谱按行排列。即使你只有一个"spectrum",也需要是单行矩阵的形式。试试这个:

foo <- data.frame(Date=seq.Date(as.Date("1957-01-01"), by = "day", 
                            length.out = ncol(milk$spectra)),
              Visits=milk$spectra[1,],
              Old_baseline_visits=milk$spectra[1,], row.names = NULL)
foo.t <- t(foo$Visits) # Visits in a single row matrix 
bc.fillPeaks <- baseline(foo.t, lambda=6,
                     hwi=50, it=10, int=2000, method='fillPeaks')
plot(bc.fillPeaks)

如果您想要原始数据框中的基线和校正光谱,试试这个:

foo$New_baseline <- c(getBaseline(bc.fillPeaks))
foo$New_corrected <- c(getCorrected(bc.fillPeaks))
plot(foo$Date, foo$New_corrected, "l")

或者,如果您不需要基线对象,您可以使用 baseline.fillPeaks(),其中 returns 一个列表。