簡體   English   中英

R - 將組/條件變量添加到時間序列

[英]R - Adding group/condition variables to a time series

我有一些來自不同人的生物識別時間序列波形數據,並且一直使用zoo包來存儲數據。 玩具示例:

library(zoo)
w1 <- sin(seq(0,20,0.25))
w2 <- cos(seq(0,20,0.25))
df <- data.frame(w1,w1,w1,w2,w2,w2)
names(df) <- paste("waves", 1:6, sep="")
waves <- zoo(df)

但我也為每個人提供了一堆額外的組/條件變量(例如,他們的年齡,性別,健康狀況)。 所以想象一下,如果我現在需要對健康人的波形做些什么。

根據我的理解,動物園和xts對象都不接受其他變量。 所以我的計划是為這些額外的變量維護一個查找數據幀。 例如:

lookup <- data.frame(index = paste("waves", 1:6, sep=""),
                     group = c("healthy", "unhealthy"))

所以現在,如果我需要對健康人進行抽樣,我可以這樣做:

select <- waves[, lookup$index[lookup$group=="healthy"]]

是否有更好的方法或數據結構來管理時間序列+其他變量?

您正在尋找的是Panel Data結構。 面板數據,也稱為橫截面時間序列數據,是隨時間和實體變化的數據。 在您的情況下,您的wavesvalue在每個實體內隨時間變化,而不同實體的group不同。 我們可以進行簡單的gatherjoin以獲得典型的面板數據格式。

library(tidyr)
library(dplyr)
panel_df = df %>%
  gather(index, value) %>%
  inner_join(lookup, by = "index") %>%
  group_by(index) %>%
  mutate(time = 1:n())

#     index     value   group  time
#     <chr>     <dbl>   <chr> <int>
# 1  waves1 0.0000000 healthy     1
# 2  waves1 0.2474040 healthy     2
# 3  waves1 0.4794255 healthy     3
# 4  waves1 0.6816388 healthy     4
# 5  waves1 0.8414710 healthy     5
# 6  waves1 0.9489846 healthy     6
# 7  waves1 0.9974950 healthy     7
# 8  waves1 0.9839859 healthy     8
# 9  waves1 0.9092974 healthy     9
# 10 waves1 0.7780732 healthy    10
# # ... with 476 more rows

這里, index表示實體維度,我手動創建了一個time變量來指示面板數據的時間維度。

要可視化面板數據,您可以使用ggplot2執行以下ggplot2

library(ggplot2)
# Visualize all waves, grouped by health status
ggplot(panel_df, aes(x = time, y = value, group = index)) +
  geom_line(aes(color = group))

在此輸入圖像描述

# Only Healthy people
panel_df %>%
  filter(group == "healthy") %>%
  ggplot(aes(x = time, y = value, color = index)) +
  geom_line()

# Compare healthy and unhealthy people's waves
panel_df %>%
  ggplot(aes(x = time, y = value, color = index)) +
  geom_line() +
  facet_grid(. ~ group)

在此輸入圖像描述

使用時間維度:

# plot acf for each entity `value` time series
par(mfrow = c(3, 2))
by(panel_df$value, panel_df$index, function(x) acf(x))

在此輸入圖像描述

library(forecast)
panel_df %>%
  filter(index == "waves1") %>%
  {autoplot(acf(.$value))}

在此輸入圖像描述

最后, plm軟件包非常適合處理面板數據。 來自計量經濟學的各種面板回歸模型已經實施,但是為了不再做出這個答案,我將留下一些自己研究的鏈接。 pdim告訴您面板數據的實體和時間維度以及它是否平衡:

library(plm)
# Check dimension of Panel
pdim(panel_df, index = c("index", "time"))
# Balanced Panel: n=6, T=81, N=486
  1. 什么是面板數據?
  2. 使用R開始使用固定/隨機效果模型
  3. 面板數據的回歸

我已修改您的數據以便更好地演示。

數據:

library(zoo)
w1 <- sin(seq(0,20,0.25))
w2 <- cos(seq(0,20,0.25))
w3 = w1*2
w4 = w2*0.5
w5 = w1*w2
w6 = w2^2

df <- data.frame(w1,w2,w3,w4,w5,w6, stringsAsFactors = FALSE)
names(df) <- paste("waves", 1:6, sep="")
waves <- zoo(df)

lookup <- data.frame(index = paste("waves", 1:6, sep=""),
                     group = c("healthy", "unhealthy"),
                     stringsAsFactors = FALSE)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM