从R的Season包中绘制用函数monthglm生成的一般线性模型glm

如何解决从R的Season包中绘制用函数monthglm生成的一般线性模型glm

问题:

我已基于 Month Season 的协变量,使用功能monthglm()对具有月份分类变量的通用线性模型(glm)进行了拟合,由 Barnett,A.G.,Dobson,A.J. (2010) Analysing Seasonal Health Data. Springer.

撰写

“月” “季节” 的协变量似乎使模型混乱。通过查看模型摘要(见下文),有一些警告“系数:(由于奇异性而未定义3个)”,因此,恰好有三个月的时间未得到正确估计(例如3月,9月和12月),而模型输出则显示 NA's 。因此,从本质上讲,该模型无法区分协变量 Month Season ,因为它们是如此相似。

我想知道是否有人可以在操作数据模型本身方面提供帮助,从而使功能monthglm()能够计算所有月份中所有蓝鲸目击事件的平均值以及上下置信度,同时在模型中包括协变量'Month''Season'?结果,绘制的模型(见下文)在3月,9月和12月缺少三个置信度条。

目标

使用协变量'Month''Season',绘制显示1月至12月之间所有月份的模型结果,显示平均蓝鲸的目次,上下置信度。

如果有人能帮助您,谢谢!

功能:monthglm():

     ##Install pacakages

     library(season)
     library(MASS) # for mvrnorm
     library(survival) # for coxph
     library(ggplot2)

    ##R-code for the function monthglm2()
    
    
    monthglm2<-function(formula,data,family=gaussian(),refmonth=1,monthvar='month',offsetmonth=FALSE,offsetpop=NULL){
      ## checks
      if (refmonth<1|refmonth>12){stop("Reference month must be between 1 and 12")}
      ## original call with defaults (see amer package)
      ans <- as.list(match.call())

  frmls <- formals(deparse(ans[[1]]))
  add <- which(!(names(frmls) %in% names(ans)))
  call<-as.call(c(ans,frmls[add]))

  monthvar=with(data,get(monthvar))
  cmonthvar=class(monthvar)
  ## If month is a character,create the numbers
  if(cmonthvar%in%c('factor','character')){
    if(cmonthvar=='character'){
      if(max(nchar(monthvar))==3){mlevels=substr(month.name,1,3)}else{mlevels=month.name}
      monthvar=factor(monthvar,levels=mlevels)
    }
    months=as.numeric(monthvar)
    data$month=months # add to data for flagleap
    months=as.factor(months)
    levels(months)[months]<-month.abb[months]
    months<-relevel(months,ref=month.abb[refmonth]) # set reference month ### TYPO HERE,changed from months.u
  }
  ## Transform month numbers to names
  if(cmonthvar%in%c('integer','numeric')){
    months.u<-as.factor(monthvar)
    nums<-as.numeric(nochars(levels(months.u))) # Month numbers
    levels(months.u)[nums]<-month.abb[nums]
    months<-relevel(months.u,ref=month.abb[refmonth]) # set reference month
  }
  ## prepare data/formula
  parts<-paste(formula)
  f<-as.formula(paste(parts[2],parts[1],parts[3:length(formula)],'+months'))
  dep<-parts[2] # dependent variable
  days<-flagleap(data=data,report=FALSE,matchin=T) # get the number of days in each month
  l<-nrow(data)
  if(is.null(offsetpop)==FALSE){poff=with(data,eval(offsetpop))} else{poff=rep(1,l)} #
  if(offsetmonth==TRUE){moff=days$ndaysmonth/(365.25/12)} else{moff=rep(1,l)} # days per month divided by average month length
  ###  data$off<-log(poff*moff)
  off<-log(poff*moff)  #
  fit<-glm(formula=f,data=data,family=family,offset=off)
  ## return
  toret<-list()
  toret$call<-call
  toret$glm<-fit
  toret$fitted.values<-fitted(fit)
  toret$residuals<-residuals(fit)
  class(toret)<-'monthglm'
  return(toret)
}

模型

Sightings$year <- Sightings$Year

model<-monthglm2(formula=Frequency_Blue_Whales_Year_Month~Year+Season,family=poisson(),offsetmonth=TRUE,monthvar='Month',data=Sightings)

模型输出

Call:  glm(formula = f,family = family,data = data,offset = off)

Coefficients:
 (Intercept)          Year  SeasonSpring  SeasonSummer  Seasonwinter  SeasonWinter     monthsFeb     monthsMar     monthsApr     monthsMay  
  -323.25725       0.16196       0.43926      -0.03365       0.76373       0.91534      -0.06261            NA      -0.23382       0.27876  
   monthsJun     monthsJul     monthsAug     monthsSep     monthsOct     monthsNov     monthsDec  
    -1.97313     -19.55938       0.25231      -1.94416       0.00643       0.77171            NA  

Degrees of Freedom: 35 Total (i.e. Null);  21 Residual
Null Deviance:      940.7 
Residual Deviance: 195.4    AIC: 386.7

摘要(模型)

Number of observations = 36 
Rate ratios 
                  mean      lower     upper      zvalue       pvalue
monthsFeb 9.393137e-01 0.67978181 1.2979315 -0.37944839 7.043549e-01
monthsApr 7.915059e-01 0.54509500 1.1493073 -1.22869325 2.191868e-01
monthsMay 1.321488e+00 0.83554494 2.0900500  1.19180025 2.333396e-01
monthsJun 1.390209e-01 0.03860611 0.5006151 -3.01844013 2.540796e-03
monthsJul 3.202360e-09 0.00000000       Inf -0.01615812 9.871082e-01
monthsAug 1.286991e+00 1.01676543 1.6290337  2.09823277 3.588459e-02
monthsSep 1.431068e-01 0.05831898 0.3511647 -4.24489759 2.186933e-05
monthsOct 1.006450e+00 0.73231254 1.3832102  0.03963081 9.683875e-01
monthsNov 2.163470e+00 1.64625758 2.8431777  5.53616916 3.091590e-08

图解

plot(model,ylim=c(0,1.4))

插入y标签和x标签的错误消息

##I am also unable to plot the x-labels and the y-labels

plot(model,+      ylim=c(0,1.4),+ ylab="Mean Blue Whale Sightings",+ xlab="Month")
Error in plot.default(order,toplot$mean,xaxt = "n",xlab = "",ylab = "",: 
  formal argument "xlab" matched by multiple actual arguments

绘图图

enter image description here

数据框(称为瞄准镜)

    structure(list(Year = c(2015L,2016L,2017L,2015L,2017L),Month = structure(c(5L,5L,4L,8L,1L,9L,7L,6L,2L,12L,11L,10L,3L,3L),.Label = c("April","August","December","Feb","Jan","July","June","Mar","May","November","October","September"
),class = "factor"),Frequency_Blue_Whales_Year_Month = c(76L,78L,66L,28L,54L,37L,39L,31L,88L,46L,23L,0L,22L,44L,30L,35L,41L,43L,65L,90L),Season = structure(c(4L,5L),.Label = c("Autumn","Spring","Summer","winter","Winter"),class = "factor")),class = "data.frame",row.names = c(NA,-36L))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-