There is a variable recorded for a panel, and a variable with date info, and another with obs info Compute averages over sub-groups of dates for each variable, with different ways of specifying date subgroups. average(svr_data) over svr Reshape data so each date is a variable for selected subset of key variables Merge results from 2 back to main, so that each indi/date observation has as variables all lagged and forward information as additional variables. Append not n lag m forward, but full history as additional variables Doing this allows for lagged intereaction that are time specific in an arbitrary way.

ff_panel_expand_longandwide(df, svr_id_t, svr_id_i, svr_data)

Arguments

svr_id_t

string time variable name

svr_id_i

string individual ID name

svr_data

string variable name

Value

a long panel frame with wide expansion of group mean lagged vars

Author

Fan Wang, http://fanwangecon.github.io

Examples

library(dplyr)
df_hw_cebu <- df_hgt_wgt %>% filter(S.country == 'Cebu' & svymthRound <= 24 & svymthRound > 0)
df_hw_cebu <- df_hw_cebu %>% mutate(mth6 = recode(svymthRound,
                                    `0`="m00t06", `2`="m00t06", `4`="m00t06", `6`="m00t06",
                                    `8`="m08t12", `10`="m08t12", `12`="m08t12",
                                    `14`="m14t18", `16`="m14t18", `18`="m14t18",
                                    `20`="m20t24", `22`="m20t24", `24`="m20t24"))
df_longandwide <- ff_panel_expand_longandwide(df_hw_cebu,
                                         svr_id_t = 'mth6',
                                         svr_id_i = 'indi.id',
                                         svr_data = 'cal')
#> Joining, by = "indi.id"
print(df_longandwide %>% select(indi.id, svymthRound, mth6, cal, matches('mth6'), everything()), n=100)
#> # A tibble: 16,188 x 20
#>     indi.id svymthRound mth6      cal mth6_cal_m00t06 mth6_cal_m08t12
#>       <dbl>       <dbl> <chr>   <dbl>           <dbl>           <dbl>
#>   1       1           2 m00t06    4.7            65.2           239. 
#>   2       1           4 m00t06   10.3            65.2           239. 
#>   3       1           6 m00t06  181.             65.2           239. 
#>   4       1           8 m08t12   85.3            65.2           239. 
#>   5       1          10 m08t12  288              65.2           239. 
#>   6       1          12 m08t12  343.             65.2           239. 
#>   7       1          14 m14t18   NA              65.2           239. 
#>   8       1          16 m14t18   NA              65.2           239. 
#>   9       1          18 m14t18   NA              65.2           239. 
#>  10       1          20 m20t24   NA              65.2           239. 
#>  11       1          22 m20t24   NA              65.2           239. 
#>  12       1          24 m20t24   NA              65.2           239. 
#>  13       2           2 m00t06  166.             63.6           148. 
#>  14       2           4 m00t06    0.5            63.6           148. 
#>  15       2           6 m00t06   24.1            63.6           148. 
#>  16       2           8 m08t12  155.             63.6           148. 
#>  17       2          10 m08t12  142.             63.6           148. 
#>  18       2          12 m08t12  146.             63.6           148. 
#>  19       2          14 m14t18  228              63.6           148. 
#>  20       2          16 m14t18  194.             63.6           148. 
#>  21       2          18 m14t18  111.             63.6           148. 
#>  22       2          20 m20t24  305              63.6           148. 
#>  23       2          22 m20t24  332.             63.6           148. 
#>  24       2          24 m20t24  363.             63.6           148. 
#>  25       3           2 m00t06    0.5            19.4           206. 
#>  26       3           4 m00t06   12.9            19.4           206. 
#>  27       3           6 m00t06   44.8            19.4           206. 
#>  28       3           8 m08t12   57              19.4           206. 
#>  29       3          10 m08t12   73.7            19.4           206. 
#>  30       3          12 m08t12  488.             19.4           206. 
#>  31       3          14 m14t18  459.             19.4           206. 
#>  32       3          16 m14t18  408              19.4           206. 
#>  33       3          18 m14t18  692.             19.4           206. 
#>  34       3          20 m20t24  612              19.4           206. 
#>  35       3          22 m20t24 1226.             19.4           206. 
#>  36       3          24 m20t24  555.             19.4           206. 
#>  37       4           2 m00t06    0.5            17.5            43.8
#>  38       4           4 m00t06    0.5            17.5            43.8
#>  39       4           6 m00t06   51.4            17.5            43.8
#>  40       4           8 m08t12   39.4            17.5            43.8
#>  41       4          10 m08t12   47              17.5            43.8
#>  42       4          12 m08t12   44.9            17.5            43.8
#>  43       4          14 m14t18  221.             17.5            43.8
#>  44       4          16 m14t18  242.             17.5            43.8
#>  45       4          18 m14t18  300.             17.5            43.8
#>  46       4          20 m20t24  581.             17.5            43.8
#>  47       4          22 m20t24  420.             17.5            43.8
#>  48       4          24 m20t24  466.             17.5            43.8
#>  49       5           2 m00t06   12.1            99.9           131. 
#>  50       5           4 m00t06   25.3            99.9           131. 
#>  51       5           6 m00t06  262.             99.9           131. 
#>  52       5           8 m08t12   71.3            99.9           131. 
#>  53       5          10 m08t12   87.8            99.9           131. 
#>  54       5          12 m08t12  234.             99.9           131. 
#>  55       5          14 m14t18  453.             99.9           131. 
#>  56       5          16 m14t18 1484.             99.9           131. 
#>  57       5          18 m14t18 1206.             99.9           131. 
#>  58       5          20 m20t24 1178.             99.9           131. 
#>  59       5          22 m20t24 1221.             99.9           131. 
#>  60       5          24 m20t24  943.             99.9           131. 
#>  61       6           2 m00t06   34.9           135.            293. 
#>  62       6           4 m00t06  267.            135.            293. 
#>  63       6           6 m00t06  102.            135.            293. 
#>  64       6           8 m08t12  169.            135.            293. 
#>  65       6          10 m08t12  387.            135.            293. 
#>  66       6          12 m08t12  324.            135.            293. 
#>  67       6          14 m14t18  653.            135.            293. 
#>  68       6          16 m14t18  534.            135.            293. 
#>  69       6          18 m14t18  479.            135.            293. 
#>  70       6          20 m20t24  417.            135.            293. 
#>  71       6          22 m20t24  346.            135.            293. 
#>  72       6          24 m20t24  700.            135.            293. 
#>  73       7           2 m00t06    3.6            93.1           270. 
#>  74       7           4 m00t06  153.             93.1           270. 
#>  75       7           6 m00t06  122.             93.1           270. 
#>  76       7           8 m08t12  408.             93.1           270. 
#>  77       7          10 m08t12  173              93.1           270. 
#>  78       7          12 m08t12  228.             93.1           270. 
#>  79       7          14 m14t18  391.             93.1           270. 
#>  80       7          16 m14t18   NA              93.1           270. 
#>  81       7          18 m14t18  637.             93.1           270. 
#>  82       7          20 m20t24  688.             93.1           270. 
#>  83       7          22 m20t24  629.             93.1           270. 
#>  84       7          24 m20t24  510              93.1           270. 
#>  85       8           2 m00t06  307.            384.            713. 
#>  86       8           4 m00t06  363.            384.            713. 
#>  87       8           6 m00t06  483.            384.            713. 
#>  88       8           8 m08t12  418.            384.            713. 
#>  89       8          10 m08t12  694.            384.            713. 
#>  90       8          12 m08t12 1028.            384.            713. 
#>  91       8          14 m14t18  691.            384.            713. 
#>  92       8          16 m14t18  880.            384.            713. 
#>  93       8          18 m14t18  916.            384.            713. 
#>  94       8          20 m20t24  637.            384.            713. 
#>  95       8          22 m20t24 1169.            384.            713. 
#>  96       8          24 m20t24  775.            384.            713. 
#>  97       9           2 m00t06    0.5             7.1            65  
#>  98       9           4 m00t06    0.5             7.1            65  
#>  99       9           6 m00t06   20.3             7.1            65  
#> 100       9           8 m08t12   15.1             7.1            65  
#> # ... with 16,088 more rows, and 14 more variables: mth6_cal_m14t18 <dbl>,
#> #   mth6_cal_m20t24 <dbl>, S.country <chr>, vil.id <dbl>, sex <chr>,
#> #   momEdu <dbl>, wealthIdx <dbl>, hgt <dbl>, wgt <dbl>, hgt0 <dbl>,
#> #   wgt0 <dbl>, prot <dbl>, p.A.prot <dbl>, p.A.nProt <dbl>