Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9125039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:42:31+00:00 2026-06-17T06:42:31+00:00

Could anyone help me replicate these relative risk calculations (and its confidence interval) in

  • 0

Could anyone help me replicate these relative risk calculations (and its confidence interval) in R?

A similar procedure used in Stata is described here. Could anyone show me how to do this in R (my data has clusters and strata but I’ve taken a simpler example)? I’ve tried the relrisk.est function but I’d rather use the survey package as this handles very complex designs. I’d also like to compare Stata and R estimates.I’m using Poisson as suggested here.

###STATA CODE
use http://www.ats.ucla.edu/stat/stata/faq/eyestudy 
tabulate carrot lenses
*same as R binomial svyglm below
xi: glm lenses carrot, fam(bin) 
*switch reference code
char carrot[omit] 1
xi: glm lenses i.carrot, fam(poisson) link(log) nolog robust eform

###R
library(foreign)
library(survey)
D<-read.dta("http://www.ats.ucla.edu/stat/stata/faq/eyestudy.dta")
table(D$lenses,D$carrot)
D$wgt<-rep(1,nrow(D))
Dd<-svydesign(id=~1,data=D,weights=~wgt)
#change category and eform....?
svyglm(lenses~carrot,Dd,family=binomial)
svyglm(lenses~carrot,Dd,family=quasipoisson(log))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T06:42:32+00:00Added an answer on June 17, 2026 at 6:42 am

    Your example is a simple dataset, so you don’t need to use the survey package at all. I would also suggest that, when learning multiple regression with R, you start with simpler examples and gradually build up your understanding of the methods involved.

    After all, my opinion is that the philosophies of Stata and R differ. Stata easily throws a ton of info in your face, without you knowing what it means or how it is derived. On the other hand R can be just as (or even more) powerful and more versatile, but lacks Stata’s “automation” and forces you to go slow to get the results you want.

    Here’s a more literal translation of your Stata code:

    require(foreign)
    data <- read.dta("http://www.ats.ucla.edu/stat/stata/faq/eyestudy.dta")
    with(data, table(carrot, lenses))
    glm.out.1 <- glm(lenses ~ carrot, family="binomial", data=data)
    summary(glm.out.1)
    logLik(glm.out.1)   # get the log-likelihood for the model, as well
    glm.out.2 <- glm(lenses ~ factor(carrot, levels=c(1,0)), family="poisson"(link="log"), data=data)
    summary(glm.out.2)
    logLik(glm.out.2)
    exp(cbind(coefficients(glm.out.2), confint(glm.out.2)))
    
    # deriving robust estimates. vcovHC() is in the sandwich package.
    require(sandwich)
    robSE <- sqrt(diag(vcovHC(glm.out.2, type="HC")))[2]
    rob <- coef(glm.out.2)[2]
    rob <- exp(c(rob, rob+qnorm(0.025)*robSE, rob-qnorm(0.025)*robSE))
    names(rob) <- c("", "2.5 %", "97.5 %")
    rob
    

    Note that the (link="log") in the second glm() call is not necessary, because it is the default link function when family="poisson".

    For deriving the “robust” estimates, I had to read this, which was very helpful. You have to use the vcovHC() function in the sandwich package to obtain a different variance-covariance matrix than the one calculated by glm(), and use that to compute the standard error and parameter estimates.

    The “robust” estimates were almost identical to what I got from Stata, down to the third decimal. All other results were completely identical; run the code and see for yourself.

    Oh, and one more thing: when you find your way using glm() with non-stratified designs, then map your way across the survey package, which contains counterparts of this and other analysis functions modified for complex designs. I also recommend you read Thomas Lumley’s book “Complex Surveys: A guide to analysis using R”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could anyone help on the below stored procedure? I am getting error Incorrect syntax
Could anyone help me to understand following line of code: sol< ?=f((1<< n)-1,i,0)+abs(P[i])*price; I
Could anyone help me in assessing why the code below doesn't work. I'm using
Could anyone please help me convert this code to vb.net, I have tried it
Could anyone please help me using C# Api in Z3. I have no idea
Ok so could anyone please help me out with the VB for auto entering
I wonder if anyone could help me with a problem I've been having. I
I was wondering if anyone could help me implement color selection in my iOS
Im asking to see if anyone could help me with my problem which is
So I'm just learning Forth and was curious if anyone could help me understand

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.