英文:
Add results of box.test within a loop in a new column of dataframe
问题
我有一个包含许多时间序列的数据集。我想使用Box检验来检查每个序列是否具有平稳性。我的循环用于进行检验,但如何将结果(x²和p值)导出到现有的数据框(每个时间序列作为行)作为新列呢?
这是我的数据框示例:
a <- c(0.2569, 0.0145896, 0.0369, 0.025986, 0.12569, 0.3695)
b <- c(0.125, 0.04582, 0.2569, 0.256369, 0.25698, 0.1456)
c <- c(0.2584, 0.05698, 0.1258, 0.2569, 0.098563, 0.1569)
df <- data.frame(a, b, c)
以下是我的循环,它可以成功为每个时间序列提供x²和p值:
for(i in 1:ncol(df)) {
box <- Box.test(df[, i], type = "Ljung-Box")
print(box)
}
现在结果应该被传递到一个数据框的空列中,如下所示:
d <- c("series1", "series2", "series3")
e <- c("green", "black", "red")
f <- c(18, 24, 12)
p_value <- NA # 创建一个空列
x <- NA
df2 <- data.frame(d, e, f, p_value, x)
我的第一个想法是这样的:
df2$p_value <- box$p.value
但在这里,每一行都会得到相同的p值。
我认为,我需要使用一个新的循环,但是我不知道如何实现"df2[i,] <- df2[i,]":
for(i in 1:nrow(df2)) {
df2$p_value <- box$p.value(df2[i,] <- df2[i,])
}
这并不起作用。有人可以帮助我吗?也许使用另一个函数?
英文:
I have a data set with many time series. I would like to check each series for stationarity using the box test. My loop works fine for the test, but how can I export the results (x² and p-value) to an existing dataframe (each time series as rows) as new column?
Here is my dataframe example:
a <- c(0.2569, 0.0145896, 0.0369, 0.025986, 0.12569, 0.3695)
b <- c(0.125, 0.04582, 0.2569, 0.256369, 0.25698, 0.1456)
c <- c(0.2584, 0.05698, 0.1258, 0.2569, 0.098563, 0.1569)
df <- data.frame(a,b,c)
Here the loop, it works fine and give me for every time series x² und p-value:
for(i in 1:ncol(df)) {
box <- Box.test(df[ , i] <- df[ , i], type = "Ljung-Box")
print(box)
}
Now the results should be transferred to the empty columns in a dataframe like this:
d <- c("series1", "series2", "series3")
e <- c("green", "black", "red")
f <- c(18, 24, 12)
p_value <- NA #to create an empty column
x <- NA
df2 <- data.frame(d,e,f,p_value,x)
My first idea was this:
df2$p_value <- box$p.value
But here I get in each row the same p_value.
I think, I have to do it with a new loop, but here I dont now how to implement "df2[i , ] <- df2[i ,]" it:
for(i in 1:nrow(df2)) {
df2$p_value <- box$p.value(df2[i , ] <- df2[i ,])
}
This doesn't work.
Can somebody help me? Maybe with another function?
答案1
得分: 1
你可以使用 sapply
和子集来获取 p 值。
bres <- sapply(df, function(x) Box.test(x, type="Ljung-Box")[['p.value']])
我强烈建议明确地创建一个字典 a
以避免错误。
a <- setNames(c("series1", "series2", "series3"), c('a', 'b', 'c'))
然后使用 cbind
。
cbind(df2, p_value=bres[match(df2$d, a)])
# d e f p_value
# a series1 green 18 0.8206314
# b series2 black 24 0.6379121
# c series3 red 12 0.1574567
数据:
df <- structure(list(a = c(0.2569, 0.0145896, 0.0369, 0.025986, 0.12569,
0.3695), b = c(0.125, 0.04582, 0.2569, 0.256369, 0.25698, 0.1456
), c = c(0.2584, 0.05698, 0.1258, 0.2569, 0.098563, 0.1569)), row.names = c(NA,
-6L), class = "data.frame")
df2 <- data.frame(d=c("series1", "series2", "series3"),
e=c("green", "black", "red"),
f=c(18, 24, 12))
英文:
You can use sapply
and subset for the p value.
bres <- sapply(df, \(x) Box.test(x, type="Ljung-Box")[['p.value']])
I strongly recommend to explicitly formulate a dictionary a
to avoid mistakes.
a <- setNames(c("series1", "series2", "series3"), c('a', 'b', 'c'))
Then cbind
.
cbind(df2, p_value=bres[match(df2$d, a)])
# d e f p_value
# a series1 green 18 0.8206314
# b series2 black 24 0.6379121
# c series3 red 12 0.1574567
Data:
df <- structure(list(a = c(0.2569, 0.0145896, 0.0369, 0.025986, 0.12569,
0.3695), b = c(0.125, 0.04582, 0.2569, 0.256369, 0.25698, 0.1456
), c = c(0.2584, 0.05698, 0.1258, 0.2569, 0.098563, 0.1569)), row.names = c(NA,
-6L), class = "data.frame")
df2 <- data.frame(d=c("series1", "series2", "series3"),
e=c("green", "black", "red"),
f=c(18, 24, 12))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论