简体   繁体   English

如何对 R 中的时间序列数据进行归一化?

[英]How to normalize the time series data in R?

I'm completely new to the R language and RStudio.我对 R 语言和 RStudio 完全陌生。 I'm trying to predict using knn for the AirPassenger dataset.我正在尝试使用 knn 预测 AirPassenger 数据集。 Dataset used is the inbuilt Air Passengers dataset.使用的数据集是内置的航空乘客数据集。

        [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] 

  [1,]  112  118  132  129  121  135  148  148  136   119   104   118

  [2,]  115  126  141  135  125  149  170  170  158   133   114   140

  [3,]  145  150  178  163  172  178  199  199  184   162   146   166

  [4,]  171  180  193  181  183  218  230  242  209   191   172   194

  [5,]  196  196  236  235  229  243  264  272  237   211   180   201

  [6,]  204  188  235  227  234  264  302  293  259   229   203   229

  [7,]  242  233  267  269  270  315  364  347  312   274   237   278

  [8,]  284  277  317  313  318  374  413  405  355   306   271   306

  [9,]  315  301  356  348  355  422  465  467  404   347   305   336

  [10,]  340  318  362  348  363  435  491  505  404   359   310   337

  [11,]  360  342  406  396  420  472  548  559  463   407   362   405

  [12,]  417  391  419  461  472  535  622  606  508   461   390   432

I'm trying to normalize the data.我正在尝试规范化数据。 My code is this:我的代码是这样的:

library(timeDate)
library(timeSeries)
data("AirPassengers")
AP <- as.matrix(AirPassengers)
P <- matrix(AP, nrow = 12,byrow = TRUE)
ran <- sample(1:12, 0.9 * 12) 
nor <-function(x) { 
  (x -min(x))/(max(x)-min(x))   }
AP_norm <- (lapply(P[,c(1,2,3,4,5,6,7,8,9,10,11,12)], nor))
summary(AP_norm)

But I end up having 144 NAN values instead of normalized values.但我最终得到了 144 个 NAN 值而不是标准化值。 Is there a way to normalize the data?有没有办法标准化数据?

Maybe this is what you are searching for:也许这就是您要搜索的内容:

library(timeDate)
library(timeSeries)
data("AirPassengers")
AP <- as.matrix(AirPassengers)
P <- matrix(AP, nrow = 12,byrow = TRUE)
ran <- sample(1:12, 0.9 * 12) 
nor <-function(x) { 
  (x -min(x))/(max(x)-min(x))   }
AP_norm <- apply(P,2,nor) # difference
summary(AP_norm)

EDIT: a bit more explanation;编辑:更多解释; lapply and sapply are mainly made for lists and vectors (including dataframes). lapplysapply主要用于列表和向量(包括数据帧)。 Your matrix is coerced as a vector of 144 elements, and your nor is applied to every one of them;您的矩阵被强制转换为 144 个元素的向量,并且您的nor应用于每个元素; so it goes wrong.所以它出错了。 In the other hand, apply works well with matrix and the second parameter made it work with columns.另一方面, apply于矩阵,第二个参数使其适用于列。 (1 for rows, 2 for columns) (1 行,2 列)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM