# R code for chapter 62 conditional logistic regression
# Book details:
# Health Science Statistics using R and R Commander
# by Robin Beaumont
# Paperback - ISBN 9781907904318
# http://www.amazon.co.uk/Health-Science-Statistics-using-Commander/dp/190790431X
# or direct from publisher at: 
# http://www.scionpublishing.com/
# book website: http://www.robin-beaumont.co.uk/rbook
###########################################################
###########
#  using example from Campbell stat a square 2 p.49

library(foreign)
# local load
hospital_monitoring1 <- 
  read.spss("D:/web_sites_mine/HIcourseweb new/book2data/condit_log_reg_campbell3_added.sav",
   use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
colnames(hospital_monitoring1) <- tolower(colnames(hospital_monitoring1))

# for web:
hospital_monitoring1 <- 
  read.spss(file=file.choose(),
   use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
colnames(hospital_monitoring1) <- tolower(colnames(hospital_monitoring1))
################
names(hospital_monitoring1)
# [1] "pairing"     "outcome"     "monitoring"  "casecontrol" "bmi"
str(hospital_monitoring1)

# edit(hospital_monitoring1)

attach(hospital_monitoring1)
monitoring_numeric <- as.numeric(monitoring) -1
monitoring_numeric
casecontrol_numeric <- as.numeric(casecontrol) -1
casecontrol_numeric
matched <- rep(1:35, rep(2,35))
matched
detach(hospital_monitoring1)

number_data <- data.frame(cbind( matched, monitoring_numeric, casecontrol_numeric))
number_data

wide_data <- reshape (number_data,  timevar= "casecontrol_numeric", 
v.names = c("monitoring_numeric"),
idvar = "matched", direction = "wide")
wide_data

attach(wide_data)
#  for the dnn option = row , column
table(monitoring_numeric.0, monitoring_numeric.1, 
dnn = c("case:adeq/inadeq", "control:adeq/inadeq"))



# alternative easy way use epicalc and recode variable
###############################

# can use the ifelse statement where
# dataset$new.variable<-ifelse
# (dataset$old.variable=="some value", 
# "what you want the new variable to be if the prior logical statement is true", 
# "what you want the new variable to be if the prior logical statement is false").
### example
# We want to create a new variable that is 1 when value is equal to or greater than 10, and  0 if less than 10.
# data$water.10<-ifelse(data$number_of_glasses>9,1,0)
# mydata$v1[mydata$v1==99] <- NA
##############################
# first recode the number_data$casecontrol_numeric have; 0=control; 1=case
#  need 0=case; 1=control

number_data$casecontrol_numeric2 <-ifelse(number_data$casecontrol_numeric ==0, 1, 0)
number_data$casecontrol_numeric2

####### alternative avoiding ifelse
# 1 create new variable
number_data$casecontrol_numeric3  <- NA
# Then recode the old field into the new one for the specified rows
number_data$casecontrol_numeric3[number_data$casecontrol_numeric==0] <- 1
number_data$casecontrol_numeric3[number_data$casecontrol_numeric==1] <- 0
number_data$casecontrol_numeric3
###################
####  alternative cross tab with 95% ci of odds ratio
library(epicalc)
matchTab(number_data$casecontrol_numeric2, number_data$monitoring_numeric,  strata=number_data$matched)
detach(wide_data)
####
############### model development

monitor_model1 <- clogit(as.numeric(outcome) ~ monitoring + strata(pairing), hospital_monitoring1)
summary(monitor_model1)
confint(monitor_model1)
exp(confint(monitor_model1))

monitor_model2 <- clogit(as.numeric(outcome) ~ monitoring + bmi + strata(pairing), hospital_monitoring1)
summary(monitor_model2)

monitor_model3 <- clogit(as.numeric(outcome) ~ monitoring * bmi + strata(pairing), hospital_monitoring1)
summary(monitor_model3)


















number_data$casecontrol_numeric2 <- number_data$casecontrol_numeric '0=1', '1=0'
number_data$casecontrol_numeric2 <- NA

number_data$casecontrol_numeric2 <- Recode(number_data$casecontrol_numeric, '0=1', '1=0', as.factor.result=FALSE) 

matchTab(number_data$casecontrol_numeric, number_data$monitoring_numeric,  strata=number_data$matched)
# this gives the odds against the required odds ratio is  1/ the value above
# need 1/ the result above
detach(wide_data)





matchTab(number_data$casecontrol_numeric, number_data$monitoring_numeric,  strata=number_data$matched)
# this gives the odds against the required odds ratio is  1/ the value above
# need 1/ the result above
detach(wide_data)

##############
hospital_monitoring1 <- 
  read.spss("D:/web_sites_mine/HIcourseweb new/book2data/condit_log_reg_campbell3_added.sav",
   use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
colnames(hospital_monitoring1) <- tolower(colnames(hospital_monitoring1))



############### now develop the model
# case.status~exposure+strata(matched.set).
# clogit(formula, data, weights, subset, na.action,
# method=c("exact", "approximate", "efron", "breslow"),

attach(hospital_monitoring1)

library(survival)
monitor_model1 <- clogit(as.numeric(outcome) ~ monitoring + strata(pairing), hospital_monitoring1)
summary(monitor_model1)
confint(monitor_model1)
exp(confint(monitor_model1))

#################



names(hospital_monitoring1)
# [1] "pairing"     "outcome"     "monitoring"  "casecontrol" "bmi"

# case.status~exposure+strata(matched.set).
# clogit(formula, data, weights, subset, na.action,
# method=c("exact", "approximate", "efron", "breslow"),

library(survival)
clogit(as.numeric(outcome) ~ monitoring + strata(pairing), hospital_monitoring1)  
read.spss("D:/web_sites_mine/HIcourseweb new/book2data/condit_log_reg_campbell3_added.sav",
   use.value.labels=TRUE, max.value.labels=Inf, to.data.frame=TRUE)
colnames(hospital_monitoring1) <- tolower(colnames(hospital_monitoring1))


##########
saving as spss
library(foreign)
write.foreign(hospital_monitoring1,
datafile = "hospital_monitoring1.txt",
codefile = "hospital_monitoring1.sps",
package = "SPSS")

