Wednesday, February 6, 2013

Weighted Logistic Regression in R, SPSS, Stata

In R:

We will use mtcars dataset to illustrate.  (R version 2.5.1)

> data(mtcars)
> mtcars <- as.data.frame(mtcars)
> mtcars$p <- runif(nrow(mtcars)) # create weights
> mtcars2 <- mtcars[c("am", "hp", "wt", "p")]
> head(mtcars2)
                  am  hp    wt         p
Mazda RX4          1 110 2.620 0.8901532
Mazda RX4 Wag      1 110 2.875 0.4601596
Datsun 710         1  93 2.320 0.9779983
Hornet 4 Drive     0 110 3.215 0.6889322
Hornet Sportabout  0 175 3.440 0.3856835
Valiant            0 105 3.460 0.1209667


> write.table(mtcars2, "mtcars.txt")
> glm(am ~ hp + wt, weights = p, data =mtcars2, family = binomial("logit"))

Call:  glm(formula = am ~ hp + wt, family = binomial("logit"), data = a, weights = p)

Coefficients:
(Intercept)           hp           wt  
   33.88418      0.04686    -13.21937  

Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
Null Deviance:      23.53 
Residual Deviance: 3.21         AIC: 7.49 
Warning message:
In eval(expr, envir, enclos) : non-integer #successes in a binomial glm!

In SPSS: 

Read the mtcat.txt in SPSS (notice that first column name is blank, so give it a name. Check if the data is read properly.) (SPSS version 20)

1. First set "p" as weights. Go to Analyze > Complex samples > Prepare for Analysis > create a plan file in c:\ directory > give a name, say, glmplan > add "p" variable as Sample weight > choose sampling with replacement (WR) with FPC > finish.

2. Use "p" weights in logistic regression from complex samples. Select Analyze > Complex samples > Logistic regression. Here are the syntax with all the specifications chosen.


* Complex Samples Logistic Regression.
CSLOGISTIC  am(LOW) WITH wt hp
  /PLAN FILE='C:\glmplan.csaplan' 
  /MODEL wt hp
  /INTERCEPT INCLUDE=YES SHOW=YES
  /STATISTICS PARAMETER EXP SE CINTERVAL TTEST DEFF DEFFSQRT
  /TEST TYPE=F PADJUST=LSD
  /ODDSRATIOS COVARIATE=[wt(1)]
  /ODDSRATIOS COVARIATE=[hp(1)]
  /MISSING CLASSMISSING=EXCLUDE
  /CRITERIA MXITER=10000 MXSTEP=5 PCONVERGE=[1E-006 RELATIVE] LCONVERGE=[0] CHKSEP=20 CILEVEL=95
  /PRINT SUMMARY CLASSTABLE COVB CORB VARIABLEINFO SAMPLEINFO.


Parameter estimates
Am
par
B
SE

Intercept
33.884
16.60
1
Wt
-13.219
6.97

Hp
0.047
0.017

Dependent Variable: am (reference category = 0) Model: (Intercept), wt, hp

Save the SPSS file in Stata (8+) dta format

In Stata:

Read the dataset in Stata first. (Stata version 11)
. use "C:\mtcars.dta", clear
Then use logit or logistic or glm command

. xi: glm am hp wt [pw = p], fam(bin)
. xi: logistic am hp wt [pw = p] //(reports odds ratio)
. xi: logit am hp wt [pw = p]


Iteration 0:   log pseudolikelihood = -11.765859  

Iteration 1:   log pseudolikelihood = -2.6460228  
Iteration 2:   log pseudolikelihood = -1.9094795  
Iteration 3:   log pseudolikelihood = -1.6099406  
Iteration 4:   log pseudolikelihood = -1.6051509  
Iteration 5:   log pseudolikelihood = -1.6051144  
Iteration 6:   log pseudolikelihood = -1.6051144  

Logistic regression                               Number of obs   =         32

                                                  Wald chi2(2)    =       9.10
                                                  Prob > chi2     =     0.0106
Log pseudolikelihood = -1.6051144                 Pseudo R2       =     0.8636

------------------------------------------------------------------------------

             |               Robust
          am |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          hp |   .0468581   .0169369     2.77   0.006     .0136623    .0800539
          wt |  -13.21937    5.96723    -2.22   0.027    -24.91492    -1.52381
       _cons |   33.88418   16.59486     2.04   0.041     1.358857     66.4095
------------------------------------------------------------------------------
Note: 3 failures and 1 success completely determined.

1 comment:

  1. I am 29 years old and have been diagnosed with breast cancer, ease of treatment and a similar story, except for my first acceptance as a rejection of herbal medicine. I was not part of the Perseid movement and did not really build relationships with any of them, I just believed in their operation. I say this because it was during the use of Dr. Itua herbal medicine that I now attest that herbal medicine is real, the phytotherapy Dr. Itua heal my breast cancer which I suffered for 2 years. Dr. Itua herbal medicine is made of natural herbs, with no side effects, and easy to drink. If you have the same breast cancer or any type of human illness, including HIV / AIDS, herpes,Ovarian Cancer,Pancratics cancer,bladder cancer, prostate cancer, Glaucoma., Cataracts,Macular degeneration,Cardiovascular disease,Autism,Lung disease.Enlarged prostate,Osteoporosis.Alzheimer's disease,Brain cancer,Esophageal cancer,Gallbladder cancer,Gestational trophoblastic disease,Head and neck cancer,Intestinal cancer,Kidney cancer,Melanoma,Mesothelioma,Multiple myeloma,Neuroendocrine tumors,Hodgkin lymphoma,Oral cancer,Ovarian cancer,Sinus cancer,,Soft tissue sarcoma,Spinal cancer,Stomach cancer,Testicular cancer,Throat cancer,Thyroid Cancer,Uterine cancer,Vaginal cancer,Vulvar cancer.psoriasis ,Tach Diseases,Lupus,Endomertil Cancer, cerebrovascular diseases,
    Dementia.lung cancer,skin cancer,testicular Cancer,Lupus, , LEUKEMIA, VIRUSES, HEPATITIS, INFERTILITY WOMEN / MAN,CONTACT EMAIL / WHATSAPP: Or drituaherbalcenter@gmail.com/ +2348149277967

    ReplyDelete