あなたが探しているのは、パネルデータの構造です。パネルデータは、断面時系列データとも呼ばれ、エンティティ間だけでなく時間とともに変化するデータです。あなたのケースではwaves
のvalue
は各エンティティ内で時間によって異なりますが、group
はエンティティによって異なります。典型的なパネルデータ形式になるには、簡単なgather
とjoin
を実行することができます。ここで
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
は、エンティティ次元を表し、Iは、パネルデータの時間次元を示すためtime
変数を手動で作成しました。
library(ggplot2)
# Visualize all waves, grouped by health status
ggplot(panel_df, aes(x = time, y = value, group = index)) +
geom_line(aes(color = group))
![enter image description here](https://i.stack.imgur.com/tPsze.png)
# 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)
![enter image description here](https://i.stack.imgur.com/AfqnQ.png)
時間次元での作業:
# plot acf for each entity `value` time series
par(mfrow = c(3, 2))
by(panel_df$value, panel_df$index, function(x) acf(x))
あなたは
ggplot2
で、次のような何かを行うことができ、パネルデータを視覚化するために、
![enter image description here](https://i.stack.imgur.com/lJYVH.png)
library(forecast)
panel_df %>%
filter(index == "waves1") %>%
{autoplot(acf(.$value))}
![enter image description here](https://i.stack.imgur.com/ChSul.png)
最後に、plm
パッケージは、パネルデータでの作業に最適です。計量分析からの様々なパネル回帰モデルが実装されていますが、この回答をもう作成しないために、私は自分の研究のためにいくつかのリンクを残すだけです。pdim
は、あなたのパネルデータのと、それはバランスが取れているかどうかのエンティティと時間次元を伝えます:
library(plm)
# Check dimension of Panel
pdim(panel_df, index = c("index", "time"))
# Balanced Panel: n=6, T=81, N=486
- What is Panel Data?
- Getting Started in Fixed/Random Effects Models using R
- Regressions with Panel Data
私はよりよいのためのあなたのデータを変更しましたデモンストレーション。
データ:
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)
あなたはここに 'data.table'を使用することができます。 – agstudy