[R] Extracting only some coefficients for the logistic regression model and its plot

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Wed Jun 15 07:06:07 CEST 2022


Hello,

To extract all but the first 2 rows, use a negative index on the rows.
I will also coerce to data.frame and add a id column, it will be needed 
to plot the confidence intervals.


ORCI <- exp(cbind(OR = coef(model ), confint(model )))[-(1:2), ]
ORCI <- cbind.data.frame(ORCI, id = row.names(ORCI))


Now the base R and ggplot plots. In both cases plot the bars first, then 
the points.

1. Base R


ymin <- min(apply(ORCI[2:3], 1, range)[1,])
ymax <- max(apply(ORCI[2:3], 1, range)[2,])

plot((ymin + ymax)/2,
      type = "n",
      xaxt = "n",
      xlim = c(0.5, 5.5),
      ylim = c(ymin, ymax),
      xlab = "X3",
      ylab = "Odds Ratio")
with(ORCI, arrows(x0 = seq_along(id),
                   y0 = `2.5 %`,
                   y1 = `97.5 %`,
                   code = 3,
                   angle = 90))
points(OR ~ seq_along(id), ORCI, pch = 16)
axis(1, at = seq_along(ORCI$id), labels = ORCI$id)



2. Package ggplot2


library(ggplot2)

ggplot(ORCI, aes(id, OR)) +
   geom_errorbar(aes(ymin = `2.5 %`, max = `97.5 %`)) +
   geom_point() +
   theme_bw()


Hope this helps,

Rui Barradas

Às 21:01 de 14/06/2022, anteneh asmare escreveu:
> sample_data = read.table("http://freakonometrics.free.fr/db.txt",header=TRUE,sep=";")
> head(sample_data)
> model = glm(Y~0+X1+X2+X3,family=binomial,data=sample_data)
> summary(model)
> exp(coef(model ))
> exp(cbind(OR = coef(model ), confint(model )))
> I have the aove sample data on logistic regression with categorical predictor
> I try the above code i get the follwing  out put,
>              OR       2.5 %     97.5 %
> X1  1.67639337 1.352583976 2.09856514
> X2  1.23377720 1.071959330 1.42496949
> X3A 0.01157565 0.001429430 0.08726854
> X3B 0.06627849 0.008011818 0.54419759
> X3C 0.01118084 0.001339984 0.08721028
> X3D 0.01254032 0.001545240 0.09539880
> X3E 0.10654454 0.013141540 0.87369972
>   but i am wondering i want to extract OR and  CI only for factors, My
> desire out put will be
>   OR       2.5 %     97.5 %
> X3A 0.01157565 0.001429430 0.08726854
> X3B 0.06627849 0.008011818 0.54419759
> X3C 0.01118084 0.001339984 0.08721028
> X3D 0.01254032 0.001545240 0.09539880
> X3E 0.10654454 0.013141540 0.87369972
> Can any one help me the code to extact it?
> additionally I want to plot the above OR with confidence interval for
> the extracted one
> can you also help me the code with plot,or box plot
> Kind Regards,
> Hana
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list