有没有办法列出从重复过程中获得的结果?
Is there a way to make a list of the results gained from a repeat process?
我在重复过程中使用了 ADF 测试,并想列出在单个 list/vector 中获得的测试统计数据,以便在直方图中表示它。
我已经尝试创建一个空列表并使用追加函数将生成的每个测试统计信息添加到该空列表。但是当我 运行 它时,它 return 什么也没有,列表仍然是空的。
x=0
repeat{
B=adf.test(((arima.sim(model=list(ar=c(1.587,-0.6914),ma=c(-1.525,0.7327)),sd=sqrt(0.03204),n=50))),alternative=c("stationary"),k=5)
TestStat1=(B$statistic)
ADF_LIST=append(adf_list, TestStat1, after = length(TestStat1))
x=x+1
if(x==10){
break
}
}
根据我上面的评论,这就是我要做的(我假设 adf.test
来自 tseries
)
set.seed(2018)
library(tseries)
adf_list <- replicate(10, adf.test(
arima.sim(
model = list(ar = c(1.587, -0.6914), ma = c(-1.525, 0.7327)),
sd = sqrt(0.03204),
n = 50))$statistic,
simplify = F)
adf_list
#[[1]]
#Dickey-Fuller
# -2.972603
#
#[[2]]
#Dickey-Fuller
# -2.317256
#
#[[3]]
#Dickey-Fuller
# -3.235755
#
#[[4]]
#Dickey-Fuller
# -3.89434
#
#[[5]]
#Dickey-Fuller
# -1.897195
#
#[[6]]
#Dickey-Fuller
# -3.15147
#
#[[7]]
#Dickey-Fuller
# -2.723526
#
#[[8]]
#Dickey-Fuller
# -2.07447
#
#[[9]]
#Dickey-Fuller
# -2.193647
#
#[[10]]
#Dickey-Fuller
# -2.197571
如您所见,replicate(n, expr, simplify = F)
重复计算 expr
n
次并将结果存储在 list
中。无需手动grow/append一个list
。
如果您更喜欢数组,可以删除 simplify = F
(或设置 simplify = T
,这是默认值)。
更新
要显示直方图,您可以这样做(我已将模拟次数增加到 100 并使用 ggplot2
进行绘图)
library(tidyverse)
set.seed(2018)
replicate(100, adf.test(
arima.sim(
model = list(ar = c(1.587, -0.6914), ma = c(-1.525, 0.7327)),
sd = sqrt(0.03204),
n = 50))$statistic) %>%
data.frame(B = .) %>%
ggplot(aes(B)) +
geom_histogram(bins = 40)
我在重复过程中使用了 ADF 测试,并想列出在单个 list/vector 中获得的测试统计数据,以便在直方图中表示它。
我已经尝试创建一个空列表并使用追加函数将生成的每个测试统计信息添加到该空列表。但是当我 运行 它时,它 return 什么也没有,列表仍然是空的。
x=0
repeat{
B=adf.test(((arima.sim(model=list(ar=c(1.587,-0.6914),ma=c(-1.525,0.7327)),sd=sqrt(0.03204),n=50))),alternative=c("stationary"),k=5)
TestStat1=(B$statistic)
ADF_LIST=append(adf_list, TestStat1, after = length(TestStat1))
x=x+1
if(x==10){
break
}
}
根据我上面的评论,这就是我要做的(我假设 adf.test
来自 tseries
)
set.seed(2018)
library(tseries)
adf_list <- replicate(10, adf.test(
arima.sim(
model = list(ar = c(1.587, -0.6914), ma = c(-1.525, 0.7327)),
sd = sqrt(0.03204),
n = 50))$statistic,
simplify = F)
adf_list
#[[1]]
#Dickey-Fuller
# -2.972603
#
#[[2]]
#Dickey-Fuller
# -2.317256
#
#[[3]]
#Dickey-Fuller
# -3.235755
#
#[[4]]
#Dickey-Fuller
# -3.89434
#
#[[5]]
#Dickey-Fuller
# -1.897195
#
#[[6]]
#Dickey-Fuller
# -3.15147
#
#[[7]]
#Dickey-Fuller
# -2.723526
#
#[[8]]
#Dickey-Fuller
# -2.07447
#
#[[9]]
#Dickey-Fuller
# -2.193647
#
#[[10]]
#Dickey-Fuller
# -2.197571
如您所见,replicate(n, expr, simplify = F)
重复计算 expr
n
次并将结果存储在 list
中。无需手动grow/append一个list
。
如果您更喜欢数组,可以删除 simplify = F
(或设置 simplify = T
,这是默认值)。
更新
要显示直方图,您可以这样做(我已将模拟次数增加到 100 并使用 ggplot2
进行绘图)
library(tidyverse)
set.seed(2018)
replicate(100, adf.test(
arima.sim(
model = list(ar = c(1.587, -0.6914), ma = c(-1.525, 0.7327)),
sd = sqrt(0.03204),
n = 50))$statistic) %>%
data.frame(B = .) %>%
ggplot(aes(B)) +
geom_histogram(bins = 40)