如何从列表中提取模型系数并制作图?

时间:2019-05-17 08:12:48

标签: r plot plotly glm coefficients

我有一个数据框 DF ,列为 TrainStation,timeofday,Date和NumberOfPassenger

首先您会看到代码,然后是回归文件,最后是包含回归的列表

TrainStation<-c("East","North","East","North","North","Central","North",
                "Central","East","North","East","North","Central","North",
                "Central","North","Central","North","Central","North","Central",
                "North","Central","East","North","East","North","Central","North",
                "Central","East","North","East","North","Central","East")

TimeOfday<-c(12,12,8,16,10,6,0,7,1,3,23,15,12,8,16,10,1,3,5,7,9,10,12,11,17,2,4,5,
             13,14,18,19,20,21,22,23)

Date<-sample(seq(as.Date('2019/01/01'), as.Date('2019/02/28'), by="day"), 36)
Date<-as.character(Date)

DF<-cbind(TrainStation,TimeOfday,Date)
DF<-as.data.frame(DF)

#Weekdays
DF$Date<-as.Date(DF$Date)
DF$Date<-weekdays(DF$Date)
#TimeOfday
DF$TimeOfday<-strptime(DF$TimeOfday,format = "%H")
DF$TimeOfday<-hour(DF$TimeOfday)

DF$TrainStation<-as.character(DF$TrainStation)
DF$TimeOfday<-as.factor(DF$TimeOfday)
DF$Date<-as.factor(DF$Date)

我回归的数据是这样的:

library(tidyverse)
DF2<-DF%>%
  group_by(TrainStation,Date,TimeOfday)%>%
  summarize(NumberOfPassenger = n_distinct(TrainStation))

然后我列出数据,然后进行回归(glm)

#List and glm
l_DF2<-split(DF2,DF2$TrainStation)
lapply(l_DF2, function(x) glm(formula = NumberOfPassenger~TimeOfday+Date,family = poisson(link = "log"), data = x))

问题:

现在,我想看一下系数并做一些。但是如何从列表中获取系数?

不同型号的样例:

mod<-glm(formula = NumberOfPassenger~TrainStation+TimeOfday+Date,family = poisson(link = "log"), data = DF2)

我可以用以下方式在图中显示TimeOfday的系数:

barplot(coef(mod)[grep("TimeOfday",names(coef(mod)))]) 

如何针对我的情况进行绘图?

1 个答案:

答案 0 :(得分:1)

res <- lapply(l_DF2,
  function(x) {
    glm(formula = NumberOfPassenger ~ TimeOfday + Date,
        family = poisson(link = "log"), data = x)
  }
)
lapply(res, coef)

要提取模型的系数,可以使用stats::coef()。现在,您只需要遍历列表即可,您可以使用lapply()来完成此操作,以适应模型。

如果您没有stats::coef()可用,则可以使用类似的方法从列表中提取coefficients

lapply(res, `[[`, "coefficients")
# or slightly more verbose
lapply(res, function(x) x[["coefficients"]])