为面板数据中的每个组变量提取拟合值
extract fitted values for each group variable in panel data
我有一个包含大量组的面板数据集。我计算了每组的拟合值,我想将所有拟合值组合到一个新的数据集中。我正在寻找一个可能的捷径来避免手动执行此操作。
以下数据集与我正在处理的数据集相似(尽管就组而言规模要小得多)。
set.seed(999)
dt <- data.frame("Group"=rep((LETTERS[1:10]), each=15),
"Year"=2001:2015,"value"=5+rnorm(150, 3,1))
names(dt)
head(dt)
table(dt$Year, dt$Group)
library(reshape2)
dt_tbl1 <- dcast(dt,Year~Group)
dt_tbl1
library(forecast)
tsMat <- ts(dcast(dt, Year ~ Group), start=2001, freq=1)
dt_ses <- lapply(tsMat, function(x) ses(x))
我正在寻找一些帮助来自动执行以下步骤。添加数据框中的所有剩余组。
dt_tbl2 <- data.frame("Year"=2001:2015,
data.frame(dt_ses$A$fitted),
data.frame(dt_ses$B$fitted),
data.frame(dt_ses$C$fitted))
并重命名新数据集中的变量以与原始组相关
names(dt_tbl2)[2:4] <- c("A_hat", "B_hat", "C_hat")
一旦完成,dt_tbl2 的格式应该与 dt_tbl1 相同。
我尝试使用 sapply() 和 lapply() 但似乎没有任何效果。
谢谢
TCS
dt_tbl = data.frame(Year = c(tsMat[,1]),
sapply(colnames(tsMat)[-1], function(col) {dt_ses[[col]]$fitted}))
names(dt_tbl)[-1] = paste0(names(dt_tbl)[-1], "_hat")
Year A_hat B_hat C_hat D_hat E_hat F_hat G_hat H_hat I_hat J_hat
1 2001 7.618084 7.521736 8.709448 7.967254 8.096049 7.932307 7.997542 7.552510 7.855070 8.136634
2 2002 7.662074 7.521647 9.150131 7.967285 8.095947 7.932320 7.997708 7.552295 7.855037 8.136680
3 2003 7.234079 7.521702 9.005576 7.967083 8.096054 7.932381 7.997711 7.552369 7.855063 8.136535
4 2004 7.919614 7.521760 8.787972 7.967088 8.096149 7.932181 7.997613 7.552552 7.854865 8.136433
5 2005 8.073512 7.521898 8.865025 7.967149 8.096250 7.932232 7.997742 7.552446 7.854703 8.136329
6 2006 7.919455 7.521738 8.572195 7.967149 8.096238 7.932222 7.997629 7.552423 7.854840 8.136423
7 2007 7.706265 7.521663 7.864789 7.967141 8.096114 7.932165 7.997734 7.552438 7.854872 8.136405
8 2008 7.010270 7.521775 7.802812 7.967079 8.095963 7.932270 7.997677 7.552331 7.854913 8.136533
9 2009 6.888603 7.521787 7.992457 7.967257 8.095926 7.932154 7.997648 7.552284 7.854947 8.136511
10 2010 6.951684 7.521864 8.130820 7.967297 8.095874 7.932169 7.997624 7.552245 7.855069 8.136587
11 2011 6.919762 7.521800 7.726980 7.967294 8.095964 7.932056 7.997646 7.552268 7.855106 8.136625
12 2012 7.976188 7.521912 7.670982 7.967325 8.095944 7.932025 7.997481 7.552206 7.855076 8.136553
13 2013 8.045479 7.521849 7.759875 7.967385 8.095928 7.932168 7.997529 7.552346 7.855074 8.136648
14 2014 8.437745 7.521808 7.485442 7.967260 8.096013 7.932149 7.997449 7.552503 7.855118 8.136644
15 2015 8.321283 7.521701 7.328202 7.967307 8.095962 7.932257 7.997381 7.552420 7.855088 8.136548
您可以尝试 purrr
包中的 transpose
,它会转置嵌套列表。
library(purrr)
t_dt_ses <- transpose(dt_ses)
dt_tbl3 <- data.frame(t_dt_ses$fitted)
# update the year column
dt_tbl3$Year<- 2001:2015
head(dt_tbl3)
# Year A B C D E F G H I J
# 1 2001 7.618084 7.521736 8.709448 7.967254 8.096049 7.932307 7.997542 7.552510 7.855070 8.136634
# 2 2002 7.662074 7.521647 9.150131 7.967285 8.095947 7.932320 7.997708 7.552295 7.855037 8.136680
# 3 2003 7.234079 7.521702 9.005576 7.967083 8.096054 7.932381 7.997711 7.552369 7.855063 8.136535
# 4 2004 7.919614 7.521760 8.787972 7.967088 8.096149 7.932181 7.997613 7.552552 7.854865 8.136433
# 5 2005 8.073512 7.521898 8.865025 7.967149 8.096250 7.932232 7.997742 7.552446 7.854703 8.136329
# 6 2006 7.919455 7.521738 8.572195 7.967149 8.096238 7.932222 7.997629 7.552423 7.854840 8.136423
我有一个包含大量组的面板数据集。我计算了每组的拟合值,我想将所有拟合值组合到一个新的数据集中。我正在寻找一个可能的捷径来避免手动执行此操作。
以下数据集与我正在处理的数据集相似(尽管就组而言规模要小得多)。
set.seed(999)
dt <- data.frame("Group"=rep((LETTERS[1:10]), each=15),
"Year"=2001:2015,"value"=5+rnorm(150, 3,1))
names(dt)
head(dt)
table(dt$Year, dt$Group)
library(reshape2)
dt_tbl1 <- dcast(dt,Year~Group)
dt_tbl1
library(forecast)
tsMat <- ts(dcast(dt, Year ~ Group), start=2001, freq=1)
dt_ses <- lapply(tsMat, function(x) ses(x))
我正在寻找一些帮助来自动执行以下步骤。添加数据框中的所有剩余组。
dt_tbl2 <- data.frame("Year"=2001:2015,
data.frame(dt_ses$A$fitted),
data.frame(dt_ses$B$fitted),
data.frame(dt_ses$C$fitted))
并重命名新数据集中的变量以与原始组相关
names(dt_tbl2)[2:4] <- c("A_hat", "B_hat", "C_hat")
一旦完成,dt_tbl2 的格式应该与 dt_tbl1 相同。
我尝试使用 sapply() 和 lapply() 但似乎没有任何效果。 谢谢 TCS
dt_tbl = data.frame(Year = c(tsMat[,1]),
sapply(colnames(tsMat)[-1], function(col) {dt_ses[[col]]$fitted}))
names(dt_tbl)[-1] = paste0(names(dt_tbl)[-1], "_hat")
Year A_hat B_hat C_hat D_hat E_hat F_hat G_hat H_hat I_hat J_hat 1 2001 7.618084 7.521736 8.709448 7.967254 8.096049 7.932307 7.997542 7.552510 7.855070 8.136634 2 2002 7.662074 7.521647 9.150131 7.967285 8.095947 7.932320 7.997708 7.552295 7.855037 8.136680 3 2003 7.234079 7.521702 9.005576 7.967083 8.096054 7.932381 7.997711 7.552369 7.855063 8.136535 4 2004 7.919614 7.521760 8.787972 7.967088 8.096149 7.932181 7.997613 7.552552 7.854865 8.136433 5 2005 8.073512 7.521898 8.865025 7.967149 8.096250 7.932232 7.997742 7.552446 7.854703 8.136329 6 2006 7.919455 7.521738 8.572195 7.967149 8.096238 7.932222 7.997629 7.552423 7.854840 8.136423 7 2007 7.706265 7.521663 7.864789 7.967141 8.096114 7.932165 7.997734 7.552438 7.854872 8.136405 8 2008 7.010270 7.521775 7.802812 7.967079 8.095963 7.932270 7.997677 7.552331 7.854913 8.136533 9 2009 6.888603 7.521787 7.992457 7.967257 8.095926 7.932154 7.997648 7.552284 7.854947 8.136511 10 2010 6.951684 7.521864 8.130820 7.967297 8.095874 7.932169 7.997624 7.552245 7.855069 8.136587 11 2011 6.919762 7.521800 7.726980 7.967294 8.095964 7.932056 7.997646 7.552268 7.855106 8.136625 12 2012 7.976188 7.521912 7.670982 7.967325 8.095944 7.932025 7.997481 7.552206 7.855076 8.136553 13 2013 8.045479 7.521849 7.759875 7.967385 8.095928 7.932168 7.997529 7.552346 7.855074 8.136648 14 2014 8.437745 7.521808 7.485442 7.967260 8.096013 7.932149 7.997449 7.552503 7.855118 8.136644 15 2015 8.321283 7.521701 7.328202 7.967307 8.095962 7.932257 7.997381 7.552420 7.855088 8.136548
您可以尝试 purrr
包中的 transpose
,它会转置嵌套列表。
library(purrr)
t_dt_ses <- transpose(dt_ses)
dt_tbl3 <- data.frame(t_dt_ses$fitted)
# update the year column
dt_tbl3$Year<- 2001:2015
head(dt_tbl3)
# Year A B C D E F G H I J
# 1 2001 7.618084 7.521736 8.709448 7.967254 8.096049 7.932307 7.997542 7.552510 7.855070 8.136634
# 2 2002 7.662074 7.521647 9.150131 7.967285 8.095947 7.932320 7.997708 7.552295 7.855037 8.136680
# 3 2003 7.234079 7.521702 9.005576 7.967083 8.096054 7.932381 7.997711 7.552369 7.855063 8.136535
# 4 2004 7.919614 7.521760 8.787972 7.967088 8.096149 7.932181 7.997613 7.552552 7.854865 8.136433
# 5 2005 8.073512 7.521898 8.865025 7.967149 8.096250 7.932232 7.997742 7.552446 7.854703 8.136329
# 6 2006 7.919455 7.521738 8.572195 7.967149 8.096238 7.932222 7.997629 7.552423 7.854840 8.136423