Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何根据策略更改前的值对I进行分类,并跟踪策略更改后的每个更改?_R_Database - Fatal编程技术网

R 如何根据策略更改前的值对I进行分类,并跟踪策略更改后的每个更改?

R 如何根据策略更改前的值对I进行分类,并跟踪策略更改后的每个更改?,r,database,R,Database,我是新手,也许我的问题很傻。首先,我想描述一下我的数据,然后是问题 我有2000年1月至2010年12月的家庭月度消费数据(不平衡)。2005年1月,消费税从7%提高到10%。此刻,我正试图更多地理解数据,并对数据有更深入的理解 为此,我想取加税前12个月的平均消费量,即2004年1月至2004年12月。然后使用这个计算出的平均值,我想将家庭分为4类:第一类1000-2500美元,第二类2501-5000美元,第三类5001-7500美元,第四类7501-10000美元。(在数据集中,每月最低消

我是新手,也许我的问题很傻。首先,我想描述一下我的数据,然后是问题

我有2000年1月至2010年12月的家庭月度消费数据(不平衡)。2005年1月,消费税从7%提高到10%。此刻,我正试图更多地理解数据,并对数据有更深入的理解

为此,我想取加税前12个月的平均消费量,即2004年1月至2004年12月。然后使用这个计算出的平均值,我想将家庭分为4类:第一类1000-2500美元,第二类2501-5000美元,第三类5001-7500美元,第四类7501-10000美元。(在数据集中,每月最低消费支出为1000美元,最高消费支出为10000.00美元)

使用上述分类标准,我想通过2005年1月、2005年2月至2010年12月每个类别的支出增加多少来检查。我已经在这个问题上挣扎了大约3周,我甚至不知道如何开始。如果有任何建议和帮助,我将不胜感激。事先非常感谢你

我使用税务局的机密数据,无法共享同一数据集。但是,我创建了与之类似的数据:

data2 <- structure(list(id = c(1223, 1223, 1223, 1223, 1223, 1223, 1223, 
1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 1223, 
1223, 1223, 1223, 1223, 1223, 1223, 1224, 1224, 1224, 1224, 1224, 
1224, 1224, 1224, 1224, 1224, 1224, 1224, 1224, 1224, 1224, 1224, 
1224, 1224, 1224, 1224, 1224, 1224, 1224, 1224), con = c(1954, 
1965, 2220, 1789, 2855, 2192, 1028, 2745, 1190, 2892, 1941, 1045, 
1778, 1660, 1037, 1259, 1655, 1429, 1617, 1927, 1105, 1948, 1929, 
1673, 7309, 9420, 9849, 7824, 7522, 7448, 7370, 6717, 9024, 7635, 
9316, 5173, 9071, 5997, 6315, 6636, 9978, 8077, 9170, 5440, 9442, 
6668, 5732, 8460), year = c(2004, 2004, 2004, 2004, 2004, 2004, 
2004, 2004, 2004, 2004, 2004, 2004, 2005, 2005, 2005, 2005, 2005, 
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2004, 2004, 2004, 2004, 
2004, 2004, 2004, 2004, 2004, 2004, 2004, 2004, 2005, 2005, 2005, 
2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005, 2005), month = c(1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 
5, 6, 7, 8, 9, 10, 11, 12)), row.names = c(NA, -48L), class = c("tbl_df", 
"tbl", "data.frame"))

data2FYI,我在整个回答中使用dplyr包(tidyverse的一部分)。我还做了一个假设,你想将2004年后的每一类消费作为一个整体与2004年的平均消费进行比较,而不是以单个家庭为基础。如果这不正确,请告诉我,我可以更改答案

首先,我只为2004年的数据制作了一个单独的表格,并用它来计算全年每个ID的平均消费量(使用
summary()
),然后用每个ID所属的类别创建一个新列(使用
mutate()
case\u when()
),然后计算每个类别的平均消费量

data2_2004 <-
     data2 %>%
     filter(year == 2004) %>%
     group_by(id) %>%
     summarise(mean_con_2004_id = mean(con)) %>%
     mutate(household_category = case_when(between(mean_con_2004_id, 1000, 2500) ~ "cat1",
                                           between(mean_con_2004_id, 2501, 5000) ~ "cat2",
                                           between(mean_con_2004_id, 5001, 7500) ~ "cat3",
                                           between(mean_con_2004_id, 7501, 10000) ~ "cat4")) %>%
     group_by(household_category) %>%
     mutate(mean_con_2004_category = mean(mean_con_2004_id))
data2_post2004 <- data2 %>%
     filter(year > 2004) %>%
     left_join(., data2_2004) 

请提供一个可复制的数据示例,以帮助您了解stlba。我编辑了我的原始问题,并提供了可复制的示例数据。我为这个错误感到抱歉,我也是Stackflow的新手,学习了很多新东西。请接受我对我原来帖子中的错误的道歉。谢谢,不用担心。当你比较从2005年到2004年的数据时,你想把每个家庭与自己或者整个群体的平均值进行比较还是其他什么?非常感谢。我非常感谢你的帮助。是的,这正是我试图做的,但从未成功。现在我能够理解我的数据,并跟踪政策变化如何影响消费。再次感谢你,太好了!很高兴我能帮忙。您可以选择此选项作为答案,以标记您的帖子是否已解决(如果全部正常):)
data2_post2004 <- data2 %>%
     filter(year > 2004) %>%
     left_join(., data2_2004) 
> data2_post2004
# A tibble: 24 x 7
      id   con  year month mean_con_2004_id household_category mean_con_2004_category
   <dbl> <dbl> <dbl> <dbl>            <dbl> <chr>                               <dbl>
 1  1223  1778  2005     1            1985. cat1                                1985.
 2  1223  1660  2005     2            1985. cat1                                1985.
 3  1223  1037  2005     3            1985. cat1                                1985.
 4  1223  1259  2005     4            1985. cat1                                1985.
 5  1223  1655  2005     5            1985. cat1                                1985.
 6  1223  1429  2005     6            1985. cat1                                1985.
 7  1223  1617  2005     7            1985. cat1                                1985.
 8  1223  1927  2005     8            1985. cat1                                1985.
 9  1223  1105  2005     9            1985. cat1                                1985.
10  1223  1948  2005    10            1985. cat1                                1985.
# ... with 14 more rows
data2_post2004_summary <- data2_post2004 %>% 
     group_by(household_category, year, month, mean_con_2004_category) %>%
     summarise(mean_con = mean(con)) %>% 
     mutate(diff_2004 = mean_con - mean_con_2004_category) %>%
     mutate(percent_diff_2004 = diff_2004/mean_con_2004_category * 100)
data2_post2004_summary %>%
     mutate(date = as.Date(paste(year, month, "01", sep = "-"))) %>%
     ggplot(aes(x = date, y = mean_con)) +
     geom_line() +
     geom_line(aes(y = mean_con_2004_category), linetype = "dotted") +
     facet_wrap(facets = vars(household_category))