Skip to content
Prev 307146 / 398506 Next

svyhist

Dear Anthony and David,

Thank you so much for your comments and suggestions! 

The sample data set I had embedded in the earlier-sent R script was intended to be used for the reproducible example.

Now I have  used Anthony's revised code on the the entire analytic file.  The code has worked fine.  Thanks, again.

Attached are the 2 .png files.

The only problem I see is that the y-lim in these 2 plots  is not exactly the same.  I have tried this: ylim = c(0,0.005, 0.010, 0.015, 0.020, 0.025, 0.030), which 
did not work. Any thoughts?


Pradip Muhuri

#######################################  Revised Code #######################

setwd ("E:/RDATA")
options(width = 120)
library (survey)
library (KernSmooth)
library (Hmisc)
load("tor.rdata")
#contents (tor)


# Grouping variable (xspd) to be  factor 
tor <- within(tor, {
         xspd2 <- factor(xspd2,levels=c (1,2),
                         labels=c('SPD', 'No SPD'), ordered=TRUE)            
                   } 
              )
# object with survey design variables and data 
nhis <- svydesign (id=~psu,strat=~stratum, weights=~wt8, data=tor, nest=TRUE)

MyBreaks <- c(18,35,45,55,65,75,85,95)

png("svyhist_no_spd_age_at_death.png")
options( survey.lonely.psu = "adjust" )
svyhist (~dthage,
         subset (nhis, mortstat==1 & xspd2=='No SPD'), breaks=MyBreaks,
         main= " ",
         col="grey80", xlab="Age at Death among those Who had SPD"
         )
lines (svysmooth(~dthage, bandwidth=5,subset(nhis, xspd2=='No SPD')), lwd=2)


dev.off ()


png("svyhist_spd_age_at_death.png")
options( survey.lonely.psu = "adjust" )
svyhist (~dthage,
         subset (nhis, mortstat==1 & xspd2=='SPD'), breaks=MyBreaks,
          #ylim = c(0,0.005, 0.010, 0.015, 0.020, 0.025, 0.030),
         main= " ",
         col="grey80",
         xlab="Age at Death among those Who had no SPD Distribution"
         )
lines (svysmooth(~dthage, bandwidth=5,subset(nhis, xspd2=='No SPD')), lwd=2)


dev.off ()

##########################################################################################