-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_model_bygroup.R
44 lines (32 loc) · 1.05 KB
/
linear_model_bygroup.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
library(tidyverse)
head(Panel_Data_Reg)
w <- Panel_Data_Reg %>%
spread(key = Variable , value = Value)
View(w)
###### 1st way
head(w)
reg <- w %>%
group_by( Bank,`Bank Id`) %>%
do(model = lm(`Net Income(profit after tax)` ~ NPL + `NPL Ratio(%)` + `Total assets` + `Total deposits` + `Total equity` + `Total invesment` + `Total liabilities` + `Total loans and advances`, data = .))
reg$model
d <- data.frame(state=rep(c('NY', 'CA'), c(10, 10)),
year=rep(1:10, 2),
response=c(rnorm(10), rnorm(10)))
fitted_models = d %>% group_by(state) %>% do(model=lm(response ~ year, data = .))
fitted_models$model
######### second way
d %>%
split(.$state) %>%
map(~lm(response ~ year, data =.))
### 3rd way
d %>%
group_by(state) %>%
nest() %>%
mutate(model = map(data,~lm(response ~ year, data = .)), cof = map(model, "coefficients")) %>%
unnest(cof)
# 4th way
md <- d %>%
group_by(state) %>%
nest() %>%
mutate(model = map(data,~lm(response ~ year, data = .)))
md$model