根据R中其他列的条件计算列内的斜率,线性回归
Calculating slope within column basing on conditions from other columns in R, Linear regression
让我先让您了解一下数据的样子:
Customer Value Module SubModule ModuleTF month department newCust
1 5 M1 SM1 1 1 DEP1 0
1 3 M1 SM1 1 2 DEP1 0
1 8 M1 SM1 1 3 DEP1 0
1 4 M2 SM1 1 1 DEP2 0
1 5 M2 SM2 1 1 DEP2 0
1 45 A5 null 0 1 DEP2 0
2
...
我想做的是计算 VALUE of MONTH 的斜率,它将成为 df 中的新列。问题是需要为每个模块、子模块和部门计算它。如果 newCust = 0,则不计算。
问题还在于,有时 X 月的值是空的,因此不存在于数据集中。我希望包括这些空值,因为它们显然会影响斜率。此外,模块有时没有子模块,在这种情况下也应该进行计算。是否有必要输入这些空值以使所有模块和子模块具有相同数量的条目?
我希望结果看起来像这样
Customer Value Module SubModule ModuleTF month department newCust slope
1 5 M1 SM1 1 1 DEP1 0 1.2
1 3 M1 SM1 1 2 DEP1 0 1.2
1 8 M1 SM1 1 3 DEP1 0 1.2
1 4 M2 SM1 1 1 DEP2 0 1.35
1 5 M2 SM2 1 1 DEP2 0 1.11
1 45 A5 null 0 1 DEP2 0 0.23
2
...
我们将不胜感激任何帮助!
谢谢!
你可以做的是使用 dplyr
和 purrr
通过不同组的 lm
函数创建线性模型。
从您的示例中获取必要的数据,可以做到
library(dplyr) # for the data munging
library(purrr) # for the do-function (modelling)
# create some example data
df <- data_frame(
customer = rep(1, 6),
value = c(5, 3, 8, 4, 5, 45),
month = c(1, 2, 3, 1, 2, 3),
departement = rep(c("Dep1", "Dep2"), each = 3)
)
# look at the data
df
#> # A tibble: 6 x 4
#> customer value month departement
#> <dbl> <dbl> <dbl> <chr>
#> 1 1 5 1 Dep1
#> 2 1 3 2 Dep1
#> 3 1 8 3 Dep1
#> 4 1 4 1 Dep2
#> 5 1 5 2 Dep2
#> 6 1 45 3 Dep2
# create a linear model per group
df %>%
group_by(customer, departement) %>%
do(mod_lin = lm(value~month, data = .)) %>%
mutate(intercept = mod_lin$coefficients[1],
slope = mod_lin$coefficients[2])
#> Source: local data frame [2 x 5]
#> Groups: <by row>
#>
#> # A tibble: 2 x 5
#> customer departement mod_lin intercept slope
#> <dbl> <chr> <list> <dbl> <dbl>
#> 1 1 Dep1 <S3: lm> 2.333333 1.5
#> 2 1 Dep2 <S3: lm> -23.000000 20.5
如果您想了解更多有关代码方面的信息,只需搜索 dplyr
、r piping
和 purrr
。两个包都有精彩的解说
让我先让您了解一下数据的样子:
Customer Value Module SubModule ModuleTF month department newCust
1 5 M1 SM1 1 1 DEP1 0
1 3 M1 SM1 1 2 DEP1 0
1 8 M1 SM1 1 3 DEP1 0
1 4 M2 SM1 1 1 DEP2 0
1 5 M2 SM2 1 1 DEP2 0
1 45 A5 null 0 1 DEP2 0
2
...
我想做的是计算 VALUE of MONTH 的斜率,它将成为 df 中的新列。问题是需要为每个模块、子模块和部门计算它。如果 newCust = 0,则不计算。 问题还在于,有时 X 月的值是空的,因此不存在于数据集中。我希望包括这些空值,因为它们显然会影响斜率。此外,模块有时没有子模块,在这种情况下也应该进行计算。是否有必要输入这些空值以使所有模块和子模块具有相同数量的条目?
我希望结果看起来像这样
Customer Value Module SubModule ModuleTF month department newCust slope
1 5 M1 SM1 1 1 DEP1 0 1.2
1 3 M1 SM1 1 2 DEP1 0 1.2
1 8 M1 SM1 1 3 DEP1 0 1.2
1 4 M2 SM1 1 1 DEP2 0 1.35
1 5 M2 SM2 1 1 DEP2 0 1.11
1 45 A5 null 0 1 DEP2 0 0.23
2
...
我们将不胜感激任何帮助!
谢谢!
你可以做的是使用 dplyr
和 purrr
通过不同组的 lm
函数创建线性模型。
从您的示例中获取必要的数据,可以做到
library(dplyr) # for the data munging
library(purrr) # for the do-function (modelling)
# create some example data
df <- data_frame(
customer = rep(1, 6),
value = c(5, 3, 8, 4, 5, 45),
month = c(1, 2, 3, 1, 2, 3),
departement = rep(c("Dep1", "Dep2"), each = 3)
)
# look at the data
df
#> # A tibble: 6 x 4
#> customer value month departement
#> <dbl> <dbl> <dbl> <chr>
#> 1 1 5 1 Dep1
#> 2 1 3 2 Dep1
#> 3 1 8 3 Dep1
#> 4 1 4 1 Dep2
#> 5 1 5 2 Dep2
#> 6 1 45 3 Dep2
# create a linear model per group
df %>%
group_by(customer, departement) %>%
do(mod_lin = lm(value~month, data = .)) %>%
mutate(intercept = mod_lin$coefficients[1],
slope = mod_lin$coefficients[2])
#> Source: local data frame [2 x 5]
#> Groups: <by row>
#>
#> # A tibble: 2 x 5
#> customer departement mod_lin intercept slope
#> <dbl> <chr> <list> <dbl> <dbl>
#> 1 1 Dep1 <S3: lm> 2.333333 1.5
#> 2 1 Dep2 <S3: lm> -23.000000 20.5
如果您想了解更多有关代码方面的信息,只需搜索 dplyr
、r piping
和 purrr
。两个包都有精彩的解说