将函数应用于多个列 [英] Applying a function to multiple columns
问题描述
我想将一个函数应用于多个列。数据框中的数据数据
的结构如下:
col1 col2 col3
xxx
xxx
xxx
特别是,我想申请ADF测试每列的时间序列。
我以为这样可能会起作用:
f< - function(x)ur.df(x,type =none,lags = 10,selectlags =AIC))
sapply(data,f)
但是,处理列的变量似乎有问题。
如何正确执行?
更新:使用此命令创建具有随机值的三列: p>
data = data.frame(matrix(rnorm(30),nrow = 10))
我可以看到您的代码有两个问题:
1)在你的函数定义中,你有一个括号太多;它应该是:
f < - function(x)ur.df(x,type =none,lags = 10,selectlags =AIC)
2)给定维度的滞后数太高的数据集。以下工作(分别注意不同数据集的不同维度和滞后):
库(urca)
data< - data.frame(matrix(rnorm(300),nrow = 100))
f< - function(x)ur.df(x,type =none,lags = 10,selectlags =AIC)
sapply(data,f)
data2 = data.frame(matrix(rnorm(30),nrow = 10))
f2& (x)ur.df(x,type =none,lags = 3,selectlags =AIC)
sapply(data2,f2)
其中给出以下输出(当然,数字当然不同,因为我没有为 rnorm
设置种子)
$ X1增强的Dickey-Fuller测试单元根/协整测试测试统计量的
值为:-6.0255$ X2增强的Dickey-Fuller测试单元根/协整测试测试统计量的
值为:-7.164
$ X3增强的Dickey-Fuller测试单元根/协整测试测试统计的
值为:-5.0921
和
$ X1增强的Dickey-Fuller测试单元根/协整测试测试统计量的
值是:-1.2124
$ X2增强的Dickey-Fuller测试单元根/协整测试测试统计量的
值为:-0.8715
$ X3增强的Dickey-Fuller测试单元根/协整检验测试统计量的
值为:-0.6598
I want to apply a function to multiple columns. My data in the dataframe data
is structured as follows:
col1 col2 col3
x x x
x x x
x x x
In particular, I want to apply an ADF test on the time-series of each column.
I thought something like this might work:
f <- function(x) ur.df(x, type = "none", lags = 10, selectlags = "AIC"))
sapply(data, f)
However, it seems that there's a problem handling the "variable" of the column.
How is it done correctly?
Update: Use this to create three columns with random values:
data = data.frame(matrix(rnorm(30), nrow=10))
There are two issues with your code as far as I can see:
1) In your function definition, you have one parenthesis too much; it should be:
f <- function(x) ur.df(x, type = "none", lags = 10, selectlags = "AIC")
2) The number of lags is too high for the given dimension of the dataset. The following works (note the different dimensions and lags of and for the different datasets, respectively):
library(urca)
data <- data.frame(matrix(rnorm(300), nrow=100))
f <- function(x) ur.df(x, type = "none", lags = 10, selectlags = "AIC")
sapply(data,f)
data2 = data.frame(matrix(rnorm(30), nrow=10))
f2 <- function(x) ur.df(x, type = "none", lags = 3, selectlags = "AIC")
sapply(data2,f2)
which gives you the following output (numbers can of course differ since I did not set a seed for rnorm
):
$X1 Augmented Dickey-Fuller Test Unit Root / Cointegration Test The value of the test statistic is: -6.0255
$X2 Augmented Dickey-Fuller Test Unit Root / Cointegration Test The value of the test statistic is: -7.164
$X3 Augmented Dickey-Fuller Test Unit Root / Cointegration Test The value of the test statistic is: -5.0921
and
$X1 Augmented Dickey-Fuller Test Unit Root / Cointegration Test The value of the test statistic is: -1.2124
$X2 Augmented Dickey-Fuller Test Unit Root / Cointegration Test The value of the test statistic is: -0.8715
$X3 Augmented Dickey-Fuller Test Unit Root / Cointegration Test The value of the test statistic is: -0.6598
这篇关于将函数应用于多个列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!