使用ggplot2中的参数绘制逻辑回归

时间:2019-02-27 16:22:37

标签: r ggplot2

我想使用ggplot2直接从参数估计值中绘制逻辑回归,但不确定如何做到。

例如,如果我估算了1500次绘制的alpha和beta参数估计值,则可以这样绘制每条线:

"_id" : 16,
"cup" : "DSLB--1902/2019",
"nombre" : "Rehabilitación de drenaje sanitario en zona sur",
"tipo" : "DRENAJE SANITARIO",
"subtipo" : "LÍNEA DE BOMBEOS",
"descripcion" : "Prueba 1000",
"estatus" : "1",
"capitulos" : {
    "Suministro e instalación" : {
        "PRELIMINARES" : [ 
            {
                "especificacion" : "A-04",
                "partida" : "1.4.4",
                "concepto" : "TRAMITE Y PERMISOS PARA LA CONSTRUCCION DE LA OBRA COMO SON OCUPACIÒN, CRUCES, INSTALACIONES MARGINALES, ROTURA Y REPOSICIÒN DE LA VÌA PÙBLICA ANTE DEPENDENCIAS CORRESPONDIENTES. COMO SON DEPENDENCIAS FEDERALES, ESTATALES, MUNICIPALES Y PRIVADAS, INCLUYE CALCULOS, PLANOS LEVANTAMIENTOS ESPECIFICOS DEL PERMISO, COPIAS REQUERIDAS.",
                "conceptov" : "MUNICIPALES",
                "cantidad" : "123",
                "unidad" : "UN."
            }, 
            {
                "especificacion" : "A-02",
                "partida" : "1.2.5",
                "concepto" : "LOCALIZACIÓN Y/O REPOSICIÓN DE SERVICIOS PÚBLICOS EXISTENTES, COMO SON INSTALACIONES SUBTERRÁNEAS Y AÉREAS DE LAS COMPAÑÍAS DE SERVICIOS PÚBLICOS, QUE PUEDAN INTERFERIR CON EL DESARROLLO DE LOS TRABAJOS ENCOMENDADOS AL CONTRATISTA, COMO SON:",
                "conceptov" : "GAS",
                "cantidad" : "23",
                "unidad" : "UN."
            }
        ]
    }
},
"updated_at" : ISODate("2019-02-25T16:12:38.000-06:00"),
"created_at" : ISODate("2019-02-19T10:51:37.000-06:00")

我将如何在ggplot2中执行此操作?我知道如何使用geom_smooth(),但这有点不同。

1 个答案:

答案 0 :(得分:2)

像在ggplot中一样,您要使用所有需要绘制的数据制作一个data.frame:

d <- data.frame(
  alpha_post = alpha_post, 
  beta_post = beta_post,
  X_lim = rep(seq(from = -3,to = 2,by=.01), each = length(alpha_post))
)
d$y <- with(d, exp(alpha_post + beta_post * X_lim) / (1 + exp(alpha_post + beta_post * X_lim)))

然后绘制图变得很容易:

ggplot(d, aes(X_lim, y, group = alpha_post)) + geom_line()

enter image description here

如果您想更加花哨,请添加一个摘要行,例如意思是:

ggplot(d, aes(X_lim, y)) +
  geom_line(aes(group = alpha_post), alpha = 0.3) +
  geom_line(size = 1, color = 'firebrick', stat = 'summary', fun.y = 'mean')

enter image description here

相关问题