R-package(baseline) application to sample dataset

天涯浪子 提交于 2019-12-25 08:06:09

问题


I am trying to use the R baseline-package on a sample dataset that I have for, to test and evaluate the current baseline algorithm that I have.

I wanted to apply the fillpeaks algorithm as a trend line to compare.

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

But my problem is that the sample data that I have does not fit the matrix structure which is used in the example. When I look at the data.frame used for the example I don't understand it

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

My question is therefore if any of you have experience with the baseline-package and the dataset (milk) used and ideas to how I can convert my data set which is structed: Date, Visits, Old_baseline_visits To fit and test the baseline algorithm from the R-package


回答1:


I have used baseline, and found it slightly confusing at first, particularly the example data. As it says in the help file, baseline expects a matrix with the spectra in rows. Even if you only have one "spectrum", it needs to be in the form of a single row matrix. Try this:

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)

If you want the baseline and corrected spectra back in your original data frame, try this:

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

Alternatively, if you don't need the baseline object, you can use baseline.fillPeaks(), which returns a list.



来源:https://stackoverflow.com/questions/37346967/r-packagebaseline-application-to-sample-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!