Hi all: My data has 3 variables: age(3levels : <30y=1 30-50y=2, >50y=3) gender(Male=0, Female=1) CD4 cell count(raw lab measurement) y(1:death 0:alive) I perform logistic regression to find out the factors that influence y. result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial)>From the result,I can get OR(Odds Ratio) of gender via exp(Estimate of Female, since Male is regarded as reference group and has no result).But how can I compute the 95%CI of OR of gender?Thanks a lot for your help. My best! [[alternative HTML version deleted]]
Weidong Gu
2011-Dec-13 17:02 UTC
[R] How to compute 95%CI for OR from logistic regression?
One simple option is to use confint function exp(confint(result)) Weidong On Tue, Dec 13, 2011 at 7:16 AM, ?? <lm_mengxin at 163.com> wrote:> Hi all: > My data has 3 variables: > age(3levels : <30y=1 ?30-50y=2, >50y=3) > gender(Male=0, Female=1) > CD4 cell count(raw lab measurement) > y(1:death ?0:alive) > > I perform logistic regression to find out the factors that influence y. > > result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) > > >From the result,I can get OR(Odds Ratio) of gender ?via exp(Estimate of Female, since Male is regarded as reference group and has no result).But how can I compute the 95%CI of OR of gender? > > > Thanks a lot for your help. > > My best! > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > 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.
Jorge I Velez
2011-Dec-13 17:59 UTC
[R] How to compute 95%CI for OR from logistic regression?
Hi there, Try require(epicalc) logistic.display(result) HTH, Jorge On Tue, Dec 13, 2011 at 7:16 AM, 孟欣 <> wrote:> Hi all: > My data has 3 variables: > age(3levels : <30y=1 30-50y=2, >50y=3) > gender(Male=0, Female=1) > CD4 cell count(raw lab measurement) > y(1:death 0:alive) > > I perform logistic regression to find out the factors that influence y. > > result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) > > >From the result,I can get OR(Odds Ratio) of gender via exp(Estimate of > Female, since Male is regarded as reference group and has no result).But > how can I compute the 95%CI of OR of gender? > > > Thanks a lot for your help. > > My best! > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@r-project.org mailing list > 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. >[[alternative HTML version deleted]]
Hi sir: I follow your suggestion: result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) logistic.display(result) Error in coeff[, 1] : incorrect number of dimensions At 2011-12-14 01:59:36,"Jorge I Velez" <jorgeivanvelez@gmail.com> wrote: Hi there, Try require(epicalc) logistic.display(result) HTH, Jorge On Tue, Dec 13, 2011 at 7:16 AM, ÃÏÐÀ <> wrote: Hi all: My data has 3 variables: age(3levels : <30y=1 30-50y=2, >50y=3) gender(Male=0, Female=1) CD4 cell count(raw lab measurement) y(1:death 0:alive) I perform logistic regression to find out the factors that influence y. result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial)>From the result,I can get OR(Odds Ratio) of gender via exp(Estimate of Female, since Male is regarded as reference group and has no result).But how can I compute the 95%CI of OR of gender?Thanks a lot for your help. My best! [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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. [[alternative HTML version deleted]]
According to the example of logistic.display: model0 <- glm(case ~ induced + spontaneous, family=binomial, data=infert) summary(model0) logistic.display(model0) induced: 3levels 0,1,2 spontaneous: 3levels 0,1,2 So if 0 is reference, we should get 2 OR for " induced1"," induced2"," spontaneous1"," spontaneous2" But the acturally OR is as the following,which is not what I expected: crude OR(95%CI) adj. OR(95%CI) P(Wald's test) P(LR-test) induced (cont. var.) 1.05 (0.74,1.5) 1.52 (1.02,2.27) 0.042 0.042 spontaneous (cont. var.) 2.9 (1.97,4.26) 3.31 (2.19,5.01) < 0.001 < 0.001 Can anyone give me some suggestions? Many thanks! Hi sir: I follow your suggestion: result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) logistic.display(result) Error in coeff[, 1] : incorrect number of dimensions At 2011-12-14 01:59:36,"Jorge I Velez" <jorgeivanvelez@gmail.com> wrote: Hi there, Try require(epicalc) logistic.display(result) HTH, Jorge On Tue, Dec 13, 2011 at 7:16 AM, ÃÏÐÀ <> wrote: Hi all: My data has 3 variables: age(3levels : <30y=1 30-50y=2, >50y=3) gender(Male=0, Female=1) CD4 cell count(raw lab measurement) y(1:death 0:alive) I perform logistic regression to find out the factors that influence y. result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial)>From the result,I can get OR(Odds Ratio) of gender via exp(Estimate of Female, since Male is regarded as reference group and has no result).But how can I compute the 95%CI of OR of gender?Thanks a lot for your help. My best! [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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. [[alternative HTML version deleted]]
Hi, Are you sure? That's not what I got:> require(epicalc) > ?logistic.display > model0 <- glm(case ~ induced + spontaneous, family=binomial, data=infert) > logistic.display(model0)Logistic regression predicting case crude OR(95%CI) adj. OR(95%CI) P(Wald's test) induced (cont. var.) 1.05 (0.74,1.5) 1.52 (1.02,2.27) 0.042 spontaneous (cont. var.) 2.9 (1.97,4.26) 3.31 (2.19,5.01) < 0.001 P(LR-test) induced (cont. var.) 0.042 spontaneous (cont. var.) < 0.001 Log-likelihood = -139.806 No. of observations = 248 AIC value = 285.612 My impression is that you did something else and you are not telling us the full story. Here is my sessionInfo(): R version 2.14.0 Patched (2011-11-12 r57642) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] US.UTF-8 attached base packages: [1] grid splines stats graphics grDevices utils datasets methods [9] base other attached packages: [1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6 [5] epicalc_2.14.0.0 nnet_7.3-1 MASS_7.3-16 survival_2.36-10 [9] maptools_0.8-10 lattice_0.20-0 foreign_0.8-47 sp_0.9-91 [13] maps_2.2-2 loaded via a namespace (and not attached): [1] tools_2.14.0 * * Could you please share your sessionInfo() as well as your OS, i.e., see http://www.r-project.org/posting-guide.html? HTH, Jorge.- 2011/12/13 孟欣 <>> According to the example of logistic.display: > model0 <- glm(case ~ induced + spontaneous, family=binomial, data=infert) > summary(model0) > logistic.display(model0) > > induced: 3levels 0,1,2 > spontaneous: 3levels 0,1,2 > > So if 0 is reference, we should get 2 OR for > " induced1"," induced2"," spontaneous1"," spontaneous2" > > But the acturally OR is as the following,which is not what I expected: > > crude OR(95%CI) adj. OR(95%CI) P(Wald's test) > P(LR-test) > induced (cont. var.) 1.05 (0.74,1.5) 1.52 (1.02,2.27) 0.042 > 0.042 > spontaneous (cont. var.) 2.9 (1.97,4.26) 3.31 (2.19,5.01) < 0.001 > < 0.001 > > > Can anyone give me some suggestions? > > Many thanks! > > > > > > > Hi sir: > I follow your suggestion: > > result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) > logistic.display(result) > > Error in coeff[, 1] : incorrect number of dimensions > > > > > > > At 2011-12-14 01:59:36,"Jorge I Velez" <> wrote: > > Hi there, > > Try > > require(epicalc) > logistic.display(result) > > HTH, > Jorge > > On Tue, Dec 13, 2011 at 7:16 AM, 孟欣 <> wrote: > >> Hi all: >> My data has 3 variables: >> age(3levels : <30y=1 30-50y=2, >50y=3) >> gender(Male=0, Female=1) >> CD4 cell count(raw lab measurement) >> y(1:death 0:alive) >> >> I perform logistic regression to find out the factors that influence y. >> >> result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) >> >> >From the result,I can get OR(Odds Ratio) of gender via exp(Estimate of >> Female, since Male is regarded as reference group and has no result).But >> how can I compute the 95%CI of OR of gender? >> >> >> Thanks a lot for your help. >> >> My best! >> >> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> 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. >> > > > > > >[[alternative HTML version deleted]]
Hi lm_mengxin, If that's the case, just use as.factor():> fit <- glm(case ~ as.factor(induced) + as.factor(spontaneous),family=binomial, data=infert)> logistic.display(fit)OR lower95ci upper95ci Pr(>|Z|) as.factor(induced)1 1.585398 0.7972313 3.152769 1.888869e-01 as.factor(induced)2 2.281856 0.9784062 5.321787 5.620567e-02 as.factor(spontaneous)1 3.630192 1.8855353 6.989152 1.145482e-04 as.factor(spontaneous)2 10.525317 4.4444045 24.926241 8.745128e-08 Also, take a look at ?factor and ?glm. HTH, Jorge.- 2011/12/13 孟欣 <>> Yes,you are right. > > But if I wanna treat "induced" and "spontaneous" as factors, how can I > get the corresponding OR? > > > > > > At 2011-12-14 12:54:30,"Jorge I Velez" <> wrote: > > I forgot to mention (sorry for double posting) that str(infert) shows > that "induced" and "spontaneous" are not factors: > > 'data.frame': 248 obs. of 8 variables: > $ education : Factor w/ 3 levels "0-5yrs","6-11yrs",..: 1 1 1 1 2 2 2 > 2 2 2 ... > $ age : num 26 42 39 34 35 36 23 32 21 28 ... > $ parity : num 6 1 6 4 3 4 1 2 1 2 ... > $ induced : num 1 1 2 2 1 2 0 0 0 0 ... > $ case : num 1 1 1 1 1 1 1 1 1 1 ... > $ spontaneous : num 2 0 0 0 1 1 0 0 1 0 ... > $ stratum : int 1 2 3 4 5 6 7 8 9 10 ... > $ pooled.stratum: num 3 1 4 2 32 36 6 22 5 19 ... > > This explains why you did not see reference levels in those variables. > > HTH, > Jorge.- > > > > On Tue, Dec 13, 2011 at 11:48 PM, Jorge I Velez <> wrote: > >> Hi, >> >> Are you sure? That's not what I got: >> >> > require(epicalc) >> > ?logistic.display >> > model0 <- glm(case ~ induced + spontaneous, family=binomial, >> data=infert) >> > logistic.display(model0) >> >> Logistic regression predicting case >> >> crude OR(95%CI) adj. OR(95%CI) P(Wald's test) >> induced (cont. var.) 1.05 (0.74,1.5) 1.52 (1.02,2.27) 0.042 >> >> spontaneous (cont. var.) 2.9 (1.97,4.26) 3.31 (2.19,5.01) < 0.001 >> >> P(LR-test) >> induced (cont. var.) 0.042 >> >> spontaneous (cont. var.) < 0.001 >> >> Log-likelihood = -139.806 >> No. of observations = 248 >> AIC value = 285.612 >> >> >> My impression is that you did something else and you are not telling us >> the full story. Here is my sessionInfo(): >> >> R version 2.14.0 Patched (2011-11-12 r57642) >> Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) >> >> locale: >> [1] US.UTF-8 >> >> attached base packages: >> [1] grid splines stats graphics grDevices utils datasets >> methods >> [9] base >> >> other attached packages: >> [1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6 >> [5] epicalc_2.14.0.0 nnet_7.3-1 MASS_7.3-16 survival_2.36-10 >> [9] maptools_0.8-10 lattice_0.20-0 foreign_0.8-47 sp_0.9-91 >> [13] maps_2.2-2 >> >> loaded via a namespace (and not attached): >> [1] tools_2.14.0 >> * >> * >> >> >> Could you please share your sessionInfo() as well as your OS, i.e., see >> http://www.r-project.org/posting-guide.html? >> >> HTH, >> Jorge.- >> >> >> 2011/12/13 孟欣 <> >> >>> According to the example of logistic.display: >>> model0 <- glm(case ~ induced + spontaneous, family=binomial, >>> data=infert) >>> summary(model0) >>> logistic.display(model0) >>> >>> induced: 3levels 0,1,2 >>> spontaneous: 3levels 0,1,2 >>> >>> So if 0 is reference, we should get 2 OR for >>> " induced1"," induced2"," spontaneous1"," spontaneous2" >>> >>> But the acturally OR is as the following,which is not what I expected: >>> >>> crude OR(95%CI) adj. OR(95%CI) P(Wald's >>> test) P(LR-test) >>> induced (cont. var.) 1.05 (0.74,1.5) 1.52 (1.02,2.27) 0.042 >>> 0.042 >>> spontaneous (cont. var.) 2.9 (1.97,4.26) 3.31 (2.19,5.01) < 0.001 >>> < 0.001 >>> >>> >>> Can anyone give me some suggestions? >>> >>> Many thanks! >>> >>> >>> >>> >>> >>> >>> Hi sir: >>> I follow your suggestion: >>> >>> result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) >>> logistic.display(result) >>> >>> Error in coeff[, 1] : incorrect number of dimensions >>> >>> >>> >>> >>> >>> >>> At 2011-12-14 01:59:36,"Jorge I Velez" <> wrote: >>> >>> Hi there, >>> >>> Try >>> >>> require(epicalc) >>> logistic.display(result) >>> >>> HTH, >>> Jorge >>> >>> On Tue, Dec 13, 2011 at 7:16 AM, 孟欣 <> wrote: >>> >>>> Hi all: >>>> My data has 3 variables: >>>> age(3levels : <30y=1 30-50y=2, >50y=3) >>>> gender(Male=0, Female=1) >>>> CD4 cell count(raw lab measurement) >>>> y(1:death 0:alive) >>>> >>>> I perform logistic regression to find out the factors that influence y. >>>> >>>> result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) >>>> >>>> >From the result,I can get OR(Odds Ratio) of gender via exp(Estimate >>>> of Female, since Male is regarded as reference group and has no result).But >>>> how can I compute the 95%CI of OR of gender? >>>> >>>> >>>> Thanks a lot for your help. >>>> >>>> My best! >>>> >>>> >>>> >>>> >>>> [[alternative HTML version deleted]] >>>> >>>> ______________________________________________ >>>> R-help@r-project.org mailing list >>>> 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. >>>> >>> >>> >>> >>> >>> >>> >> > > >[[alternative HTML version deleted]]
logistic.display need a rather tidy model ie all independent variables must be the original name not any function of a variable in the dataset of the model. If data1 is your dataset containing y, 'gender' and 'age', what you need to do is> data1$age1 <- factor(data1$age) > result1<-glm(y ~ age1 + gender + CD4,family = binomial, data=data1) > logistic.display(result1)Note that 'gender', like 'y', is already dichotomized (0,1). There is no need to 'factor' Virasakdi C 2011/12/14 ©sªY <lm_mengxin@163.com>> > Hi sir: > I follow your suggestion: > > result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) > logistic.display(result) > > Error in coeff[, 1] : incorrect number of dimensions > > > > > > > At 2011-12-14 01:59:36,"Jorge I Velez" <jorgeivanvelez@gmail.com> wrote: > > Hi there, > > Try > > require(epicalc) > logistic.display(result) > > HTH, > Jorge > > On Tue, Dec 13, 2011 at 7:16 AM, ©sªY <> wrote: > >> Hi all: >> My data has 3 variables: >> age(3levels : <30y=1 30-50y=2, >50y=3) >> gender(Male=0, Female=1) >> CD4 cell count(raw lab measurement) >> y(1:death 0:alive) >> >> I perform logistic regression to find out the factors that influence y. >> >> result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) >> >> >From the result,I can get OR(Odds Ratio) of gender via exp(Estimate of >> Female, since Male is regarded as reference group and has no result).But >> how can I compute the 95%CI of OR of gender? >> >> >> Thanks a lot for your help. >> >> My best! >> >> >> >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help@r-project.org mailing list >> 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. >> > > > > > -- > > -- NOTE: Prince of Songkla University will NEVER ask for your PSU > Passport/Email Username or password by e-mail. If you receive such a > message, please report it to report-phish@psu.ac.th. > ------------------------------ > !!!! NEVER reply to any e-mail asking for your PSU Passport/Email or other > personal details. !!!! > ------------------------------ > For more information, contact the PSU E-Mail Service by dialing 2121 > > >[[alternative HTML version deleted]]
Yes,it works well. Thanks for your help. At 2011-12-14 13:06:14,"Jorge I Velez" <jorgeivanvelez@gmail.com> wrote: Hi lm_mengxin, If that's the case, just use as.factor():> fit <- glm(case ~ as.factor(induced) + as.factor(spontaneous), family=binomial, data=infert) > logistic.display(fit)OR lower95ci upper95ci Pr(>|Z|) as.factor(induced)1 1.585398 0.7972313 3.152769 1.888869e-01 as.factor(induced)2 2.281856 0.9784062 5.321787 5.620567e-02 as.factor(spontaneous)1 3.630192 1.8855353 6.989152 1.145482e-04 as.factor(spontaneous)2 10.525317 4.4444045 24.926241 8.745128e-08 Also, take a look at ?factor and ?glm. HTH, Jorge.- 2011/12/13 ÃÏÐÀ <> Yes,you are right. But if I wanna treat "induced" and "spontaneous" as factors, how can I get the corresponding OR? At 2011-12-14 12:54:30,"Jorge I Velez" <> wrote: I forgot to mention (sorry for double posting) that str(infert) shows that "induced" and "spontaneous" are not factors: 'data.frame':248 obs. of 8 variables: $ education : Factor w/ 3 levels "0-5yrs","6-11yrs",..: 1 1 1 1 2 2 2 2 2 2 ... $ age : num 26 42 39 34 35 36 23 32 21 28 ... $ parity : num 6 1 6 4 3 4 1 2 1 2 ... $ induced : num 1 1 2 2 1 2 0 0 0 0 ... $ case : num 1 1 1 1 1 1 1 1 1 1 ... $ spontaneous : num 2 0 0 0 1 1 0 0 1 0 ... $ stratum : int 1 2 3 4 5 6 7 8 9 10 ... $ pooled.stratum: num 3 1 4 2 32 36 6 22 5 19 ... This explains why you did not see reference levels in those variables. HTH, Jorge.- On Tue, Dec 13, 2011 at 11:48 PM, Jorge I Velez <> wrote: Hi, Are you sure? That's not what I got:> require(epicalc) > ?logistic.display > model0 <- glm(case ~ induced + spontaneous, family=binomial, data=infert) > logistic.display(model0)Logistic regression predicting case crude OR(95%CI) adj. OR(95%CI) P(Wald's test) induced (cont. var.) 1.05 (0.74,1.5) 1.52 (1.02,2.27) 0.042 spontaneous (cont. var.) 2.9 (1.97,4.26) 3.31 (2.19,5.01) < 0.001 P(LR-test) induced (cont. var.) 0.042 spontaneous (cont. var.) < 0.001 Log-likelihood = -139.806 No. of observations = 248 AIC value = 285.612 My impression is that you did something else and you are not telling us the full story. Here is my sessionInfo(): R version 2.14.0 Patched (2011-11-12 r57642) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit) locale: [1] US.UTF-8 attached base packages: [1] grid splines stats graphics grDevices utils datasets methods [9] base other attached packages: [1] ggplot2_0.8.9 proto_0.3-9.2 reshape_0.8.4 plyr_1.6 [5] epicalc_2.14.0.0 nnet_7.3-1 MASS_7.3-16 survival_2.36-10 [9] maptools_0.8-10 lattice_0.20-0 foreign_0.8-47 sp_0.9-91 [13] maps_2.2-2 loaded via a namespace (and not attached): [1] tools_2.14.0 Could you please share your sessionInfo() as well as your OS, i.e., see http://www.r-project.org/posting-guide.html? HTH, Jorge.- 2011/12/13 ÃÏÐÀ <> According to the example of logistic.display: model0 <- glm(case ~ induced + spontaneous, family=binomial, data=infert) summary(model0) logistic.display(model0) induced: 3levels 0,1,2 spontaneous: 3levels 0,1,2 So if 0 is reference, we should get 2 OR for " induced1"," induced2"," spontaneous1"," spontaneous2" But the acturally OR is as the following,which is not what I expected: crude OR(95%CI) adj. OR(95%CI) P(Wald's test) P(LR-test) induced (cont. var.) 1.05 (0.74,1.5) 1.52 (1.02,2.27) 0.042 0.042 spontaneous (cont. var.) 2.9 (1.97,4.26) 3.31 (2.19,5.01) < 0.001 < 0.001 Can anyone give me some suggestions? Many thanks! Hi sir: I follow your suggestion: result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial) logistic.display(result) Error in coeff[, 1] : incorrect number of dimensions At 2011-12-14 01:59:36,"Jorge I Velez" <> wrote: Hi there, Try require(epicalc) logistic.display(result) HTH, Jorge On Tue, Dec 13, 2011 at 7:16 AM, ÃÏÐÀ <> wrote: Hi all: My data has 3 variables: age(3levels : <30y=1 30-50y=2, >50y=3) gender(Male=0, Female=1) CD4 cell count(raw lab measurement) y(1:death 0:alive) I perform logistic regression to find out the factors that influence y. result<-glm(y ~ factor(age) + factor(gender) + CD4,family = binomial)>From the result,I can get OR(Odds Ratio) of gender via exp(Estimate of Female, since Male is regarded as reference group and has no result).But how can I compute the 95%CI of OR of gender?Thanks a lot for your help. My best! [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list 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. [[alternative HTML version deleted]]