在 R 中绘制多元回归时出现点和胡须错误
Dot-and-whiskers error when plotting multiple regressions in R
我已经按照 上的代码使用点和须线绘制多元回归,但是在尝试复制结果时以及 运行 使用我自己的代码时收到以下错误数据和模型:
Error in match.arg(tolower(centrality), c("median", "mean", "map", "trimmed", :
'arg' should be one of “median”, “mean”, “map”, “trimmed”, “all"
我正在运行宁以下代码:
nblaborforcer <- glm.nb(asylum~laborforcerl + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb1 <- tidy(nblaborforcer)
nblaborforcefpercent<- glm.nb(asylum~laborforcefpercentL + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb2 <- tidy(nblaborforcefpercent)
dwplot(list(nb1, nb2))+ ylim(breaks = c("laborforcerl", "laborforcefpercentL"))
当我运行下面的代码found here解决了这个问题:
dwplot(list(nb1, nb2), centrality = NULL)+ ylim(breaks = c("laborforcerl", "nblaborforcefpercent"))
我收到此错误:
Error in `$<-.data.frame`(`*tmp*`, "estimate", value = numeric(0)) : replacement has 0 rows, data has 8
这是我的回溯
> traceback()
5: stop(sprintf(ngettext(N, "replacement has %d row, data has %d",
"replacement has %d rows, data has %d"), N, nrows), domain = NA)
4: `$<-.data.frame`(`*tmp*`, "estimate", value = numeric(0))
3: `$<-`(`*tmp*`, "estimate", value = numeric(0))
2: add_NAs(df, n_models, mod_names)
1: dwplot(list(nb1, nb2), centrality = NULL)
这是我的会话信息:
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] broom.mixed_0.2.7 glmmTMB_1.1.2.3 lme4_1.1-26 Matrix_1.3-4 broom_0.7.6
[6] dotwhisker_0.7.4 jtools_2.1.4 boot_1.3-28 pscl_1.5.5 MASS_7.3-54
[11] plyr_1.8.6 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.5 purrr_0.3.4
[16] readr_1.4.0 tidyr_1.1.4 tibble_3.1.1 ggplot2_3.3.5 tidyverse_1.3.1
loaded via a namespace (and not attached):
[1] httr_1.4.2 pkgload_1.1.0 jsonlite_1.7.2 splines_4.0.2
[5] modelr_0.1.8 datawizard_0.2.1 assertthat_0.2.1 statmod_1.4.35
[9] pander_0.6.4 ggstance_0.3.5 cellranger_1.1.0 bayestestR_0.11.0
[13] numDeriv_2016.8-1.1 pillar_1.6.0 backports_1.2.1 lattice_0.20-44
[17] glue_1.4.2 digest_0.6.27 rvest_1.0.0 minqa_1.2.4
[21] colorspace_2.0-0 pkgconfig_2.0.3 haven_2.4.1 scales_1.1.1
[25] generics_0.1.0 farver_2.0.3 ellipsis_0.3.1 withr_2.3.0
[29] TMB_1.7.22 cli_2.5.0 magrittr_2.0.1 crayon_1.4.1
[33] readxl_1.3.1 fs_1.5.0 fansi_0.4.1 nlme_3.1-148
[37] xml2_1.3.2 tools_4.0.2 hms_1.0.0 lifecycle_1.0.0
[41] munsell_0.5.0 reprex_2.0.0 compiler_4.0.2 rlang_0.4.10
[45] grid_4.0.2 nloptr_1.2.2.2 parameters_0.14.0 rstudioapi_0.13
[49] labeling_0.4.2 testthat_3.0.0 gtable_0.3.0 DBI_1.1.1
[53] R6_2.5.0 lubridate_1.7.10 utf8_1.1.4 rprojroot_2.0.2
[57] insight_0.14.5 desc_1.2.0 stringi_1.5.3 Rcpp_1.0.5
[61] vctrs_0.3.7 dbplyr_2.1.1 tidyselect_1.1.0
理想情况下,我希望有一个包含 5 个以上回归的图。
更新
当我 运行 带有样本数据 mtcars 的代码时,我得到了想要的结果。当我将我的数据与负二项式模型一起使用时,我得到了同样的错误。
options("install.lock"=FALSE)
install.packages("ggplot2")
library(dotwhisker)
library(dplyr)
install.packages("tidyr")
library(tidyverse)
data("mtcars")
m1 <- lm(mpg ~ wt + cyl + disp + gear, data = mtcars)
dwplot(m1) + ylim(c("wt"))
m2 <- update(m1, . ~ . + hp) # add another predictor
m3 <- update(m2, . ~ . + am) # and another
summary(m1)
summary(m2)
dwplot(list(m1, m2, m3)) + ylim(c("wt", "cyl"))
dissertation2<-read.csv("C:/Users/diron/Google Drive/Vanderbilt/R working directory/dissertation2.csv")
library(MASS)
install.packages("pscl")
library(pscl)
install.packages("boot")
library(boot)
install.packages("jtools")
library(jtools)
nblaborforcer <- glm.nb(asylum~laborforcerl + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb1 <- tidy(nblaborforcer)
dwplot(nb1) + ylim(c("laborforcerl"))
nblaborforcefpercent<- glm.nb(asylum~laborforcefpercentL + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb2<-tidy(nblaborforcefpercent)
dwplot(nb2) + ylim(c("laborforcefpercentL"))
dwplot(list(nb1,nb2))
Error in match.arg(tolower(centrality), c("median", "mean", "map", "trimmed", :
'arg' should be one of “median”, “mean”, “map”, “trimmed”, “all”
我做错了什么?因为我使用的是负二项式模型,所以我必须使用不同的包吗?
我使用 tidy models
找到了我自己的问题的解决方案,如 。
nb1 <- tidy(nblaborforcer) %>%
mutate(model = "nb1")
nb2 <- tidy(nblaborforcefpercent) %>%
mutate(model = "nb2")
tidy_mods <- bind_rows(nb1, nb2)
dwplot(tidy_mods) + ylim(c("laborforcerl", "laborforcefpercentL"))
我已经按照
Error in match.arg(tolower(centrality), c("median", "mean", "map", "trimmed", :
'arg' should be one of “median”, “mean”, “map”, “trimmed”, “all"
我正在运行宁以下代码:
nblaborforcer <- glm.nb(asylum~laborforcerl + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb1 <- tidy(nblaborforcer)
nblaborforcefpercent<- glm.nb(asylum~laborforcefpercentL + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb2 <- tidy(nblaborforcefpercent)
dwplot(list(nb1, nb2))+ ylim(breaks = c("laborforcerl", "laborforcefpercentL"))
当我运行下面的代码found here解决了这个问题:
dwplot(list(nb1, nb2), centrality = NULL)+ ylim(breaks = c("laborforcerl", "nblaborforcefpercent"))
我收到此错误:
Error in `$<-.data.frame`(`*tmp*`, "estimate", value = numeric(0)) : replacement has 0 rows, data has 8
这是我的回溯
> traceback()
5: stop(sprintf(ngettext(N, "replacement has %d row, data has %d",
"replacement has %d rows, data has %d"), N, nrows), domain = NA)
4: `$<-.data.frame`(`*tmp*`, "estimate", value = numeric(0))
3: `$<-`(`*tmp*`, "estimate", value = numeric(0))
2: add_NAs(df, n_models, mod_names)
1: dwplot(list(nb1, nb2), centrality = NULL)
这是我的会话信息:
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] broom.mixed_0.2.7 glmmTMB_1.1.2.3 lme4_1.1-26 Matrix_1.3-4 broom_0.7.6
[6] dotwhisker_0.7.4 jtools_2.1.4 boot_1.3-28 pscl_1.5.5 MASS_7.3-54
[11] plyr_1.8.6 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.5 purrr_0.3.4
[16] readr_1.4.0 tidyr_1.1.4 tibble_3.1.1 ggplot2_3.3.5 tidyverse_1.3.1
loaded via a namespace (and not attached):
[1] httr_1.4.2 pkgload_1.1.0 jsonlite_1.7.2 splines_4.0.2
[5] modelr_0.1.8 datawizard_0.2.1 assertthat_0.2.1 statmod_1.4.35
[9] pander_0.6.4 ggstance_0.3.5 cellranger_1.1.0 bayestestR_0.11.0
[13] numDeriv_2016.8-1.1 pillar_1.6.0 backports_1.2.1 lattice_0.20-44
[17] glue_1.4.2 digest_0.6.27 rvest_1.0.0 minqa_1.2.4
[21] colorspace_2.0-0 pkgconfig_2.0.3 haven_2.4.1 scales_1.1.1
[25] generics_0.1.0 farver_2.0.3 ellipsis_0.3.1 withr_2.3.0
[29] TMB_1.7.22 cli_2.5.0 magrittr_2.0.1 crayon_1.4.1
[33] readxl_1.3.1 fs_1.5.0 fansi_0.4.1 nlme_3.1-148
[37] xml2_1.3.2 tools_4.0.2 hms_1.0.0 lifecycle_1.0.0
[41] munsell_0.5.0 reprex_2.0.0 compiler_4.0.2 rlang_0.4.10
[45] grid_4.0.2 nloptr_1.2.2.2 parameters_0.14.0 rstudioapi_0.13
[49] labeling_0.4.2 testthat_3.0.0 gtable_0.3.0 DBI_1.1.1
[53] R6_2.5.0 lubridate_1.7.10 utf8_1.1.4 rprojroot_2.0.2
[57] insight_0.14.5 desc_1.2.0 stringi_1.5.3 Rcpp_1.0.5
[61] vctrs_0.3.7 dbplyr_2.1.1 tidyselect_1.1.0
理想情况下,我希望有一个包含 5 个以上回归的图。
更新
当我 运行 带有样本数据 mtcars 的代码时,我得到了想要的结果。当我将我的数据与负二项式模型一起使用时,我得到了同样的错误。
options("install.lock"=FALSE)
install.packages("ggplot2")
library(dotwhisker)
library(dplyr)
install.packages("tidyr")
library(tidyverse)
data("mtcars")
m1 <- lm(mpg ~ wt + cyl + disp + gear, data = mtcars)
dwplot(m1) + ylim(c("wt"))
m2 <- update(m1, . ~ . + hp) # add another predictor
m3 <- update(m2, . ~ . + am) # and another
summary(m1)
summary(m2)
dwplot(list(m1, m2, m3)) + ylim(c("wt", "cyl"))
dissertation2<-read.csv("C:/Users/diron/Google Drive/Vanderbilt/R working directory/dissertation2.csv")
library(MASS)
install.packages("pscl")
library(pscl)
install.packages("boot")
library(boot)
install.packages("jtools")
library(jtools)
nblaborforcer <- glm.nb(asylum~laborforcerl + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb1 <- tidy(nblaborforcer)
dwplot(nb1) + ylim(c("laborforcerl"))
nblaborforcefpercent<- glm.nb(asylum~laborforcefpercentL + hr_scoreL + warL + asylumL + origin-1 + offset(log(population)), data = dissertation2, control = glm.control(maxit = 500))
nb2<-tidy(nblaborforcefpercent)
dwplot(nb2) + ylim(c("laborforcefpercentL"))
dwplot(list(nb1,nb2))
Error in match.arg(tolower(centrality), c("median", "mean", "map", "trimmed", :
'arg' should be one of “median”, “mean”, “map”, “trimmed”, “all”
我做错了什么?因为我使用的是负二项式模型,所以我必须使用不同的包吗?
我使用 tidy models
找到了我自己的问题的解决方案,如
nb1 <- tidy(nblaborforcer) %>%
mutate(model = "nb1")
nb2 <- tidy(nblaborforcefpercent) %>%
mutate(model = "nb2")
tidy_mods <- bind_rows(nb1, nb2)
dwplot(tidy_mods) + ylim(c("laborforcerl", "laborforcefpercentL"))