简体   繁体   English

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

[英]How to extract model coeff from a list and make Plots?

I have a data frame DF with the columns TrainStation,timeofday,Date and NumberOfPassenger . 我有一个数据框DF ,其中包含TrainStation,timeofday,Date和NumberOfPassenger列

First you see the code, then the file for the regression and finally the list with the regression 首先,您会看到代码,然后是回归文件,最后是包含回归的列表

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)

And my data for the regression is this: 我的回归数据是:

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

then I make a list with my data, followed by a regression (glm) 然后用数据列出一个列表,然后进行回归(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))

Question: 题:

Now I would like to look at the coefficients and make some Plots . 现在,我想看一下系数并做一些 But how do I get the coefficients from the list? 但是如何从列表中获取系数?

Plot example for a different model: 不同模型的示例:

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

i could show the coefficients of the TimeOfday in a plot with this: 我可以用以下方式在图中显示TimeOfday的系数:

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

How can I plot in my case? 我该如何绘图?

What about 关于什么

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

To extract the coefficients of a model, you can use stats::coef() . 要提取模型的系数,可以使用stats::coef() Now you only have to iterate over your list, which you can do using lapply() as you have already done to fit the models. 现在,您只需要遍历列表,就可以像使用lapply()一样使用lapply()进行操作。

If you did not have stats::coef() available, you could just extract the coefficients entries from the list using something like 如果您没有stats::coef() ,则可以使用类似以下的方法从列表中提取coefficients条目

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM