PH207x Week 2

Loading data and Deducer

library(foreign)
fhs <- read.dta("./fhs.2d92301d751b.dta")

library(Deducer)
Note: On Mac OS X we strongly recommend using iplots from within JGR.
Proceed at your own risk as iplots cannot resolve potential ev.loop deadlocks.
'Yes' is assumed for all dialogs as they cannot be shown without a deadlock,
also ievent.wait() is disabled.

Homework 2

## part 2
no.missing <- fhs[,c("diabetes1","diabetes2","diabetes3")]
no.missing <- no.missing[complete.cases(no.missing),]

lapply(1:3,function(i) prop.table(table(no.missing[,i])))
[[1]]

     No     Yes 
0.98191 0.01809 

[[2]]

     No     Yes 
0.96725 0.03275 

[[3]]

     No     Yes 
0.92171 0.07829 



## part 3
fhs$bmi1.cat <- cut(fhs[,"bmi1"], breaks = c(-Inf, 18.5, 25, 30, Inf), right = F)

tab1 <- xtabs(~ bmi1.cat +prevhyp1, fhs)

addmargins(prop.table(tab1, 1))
             prevhyp1
bmi1.cat          No    Yes    Sum
  [-Inf,18.5) 0.8947 0.1053 1.0000
  [18.5,25)   0.7944 0.2056 1.0000
  [25,30)     0.6304 0.3696 1.0000
  [30, Inf)   0.4159 0.5841 1.0000
  Sum         2.7355 1.2645 4.0000


fhs <- within(fhs, {
    highbp1 <- NA
    highbp1[sysbp1 >= 140 | diabp1 >= 90] <- 1
    highbp1[sysbp1  < 140 & diabp1  < 90] <- 0
})

tab2 <- xtabs(~ highbp1 +prevchd1, fhs)

prop.table(tab2, 1)
       prevchd1
highbp1      No     Yes
      0 0.96874 0.03126
      1 0.93453 0.06547


## part 4
p4 <- read.table(head = T, text = '
"Age" "period1950-1952" "period1990-1992"
0 100000 100000
20 73412  96902
40 56884  92638
70 31744  79873
')

## 3 Absolute change
lapply(2:4, function(i) {
    survival.props <- p4[i,2:3] / p4[i-1,2:3]
    (survival.props[2] - survival.props[1])
})
[[1]]
  period1990.1992
2          0.2349

[[2]]
  period1990.1992
3          0.1811

[[3]]
  period1990.1992
4          0.3042


## 4 Relative change
lapply(2:4, function(i) {
    survival.props <- p4[i,2:3] / p4[i-1,2:3]
    (survival.props[2] - survival.props[1]) / survival.props[1]
})
[[1]]
  period1990.1992
2            0.32

[[2]]
  period1990.1992
3          0.2338

[[3]]
  period1990.1992
4           0.545