Skip to content

Instantly share code, notes, and snippets.

View n8thangreen's full-sized avatar

Dr Nathan Green n8thangreen

View GitHub Profile
@n8thangreen
n8thangreen / LifeTable
Last active December 20, 2015 06:48
Life table calculation.
##e.g. data from kmi in LOS_toy_example.R
##non-infected only
#data <- data[-173,]
## (death) count at each event time
nDx <- table(data$exit)
## event times
times <- as.numeric(names(table(data$exit)))
@n8thangreen
n8thangreen / WriteToWord
Created July 26, 2013 12:56
Write from R to Word.
library("R2wd")
require(R2wd)
wdGet(T) # If no word file is open, it will start a new one - can set if to have the file visiable or not
wdNewDoc("H:\\This.doc") # this creates a new file with "this.doc" name
wdApplyTemplate("H:\\This.dot") # this applies a template
@n8thangreen
n8thangreen / Charlson comorbidity score
Last active December 21, 2015 19:39
This is a streamlined version of the charlson function in the charlson package. This uses ICD9 or ICD10 codes.
charlson2 <- function (Patient, icd)
{
##
##
##
## foo <- charlson2(Patient=survData$Spell, icd=survData$Prim.Diag.Code)
d <- cbind.data.frame(Patient, icd)
nobs <- nrow(d)
d$code <- rep(NA, nobs)
############################
###GUM2011AllPositivity#####
############################
summary(dat$totalPositivity2011GUM)
hist(dat$totalPositivity2011GUM)
modpos<-glm(totalPositivity2011GUM~ratioWhite2011+ratioBlack2011+ratioAsian2011+
conception.rate.per.1000women.Under18+MarriedCivilCohabitCouples2011+SingleParents2011+
ratioNoQual_2011+ratioMales25to34_2011+RatioFemales16to24_2011,
family=binomial,data=dat,weights=sum.NtestsGUM2011All)
@n8thangreen
n8thangreen / Aggregated Cox regression functions
Created November 6, 2013 11:58
Aggregated Cox regression functions
#####################################
#
# Cox regression aggregated functions
#
# Nathan Green
# 11-2012
#
#####################################
@n8thangreen
n8thangreen / time-dept_array
Created November 6, 2013 12:03
Create time-dependent long format time to event data for 3-state Death-Disease multistate model
tvCox <- function(survData, covariates, type){
##
## time-dependent hazard array representation
## can specify which time to use: cause specific or subdistributions
##
## survData: survival data subset of total.data with indicators
## covariates: e.g. c("age", "agegr", "cath", "surgical", "gender")
## type: type of hazard functions to use
## "" - Cause-specific
## "death" - subdistribution time-to-death
@n8thangreen
n8thangreen / subdistn.R
Last active December 15, 2016 09:55
Generate subdistribution hazard time values
subdistn <- function(survData){
## substitute in later times to change risk sets
## time representation appropriate for subdistribution hazards
##
## Accounts for administrative censoring
## note that within a specific organism subset the maximum time
## may be from a patient infected by a different organism
## default entries
@n8thangreen
n8thangreen / multinom_lik
Created November 11, 2013 14:14
Likelihood calculation using a multinomial logistic regression
multinom_loglik <- function(props, size.melt, cov){
##
## multinomial logistic regression likelihood
## over ALL groups in one batch
##
## props: array of covariate by group probabilities
## size.melt: e.g. agesize.melt
## cov: covariate name (string) e.g. "age"
ncol.size.melt <- ncol(size.melt)
@n8thangreen
n8thangreen / probs.logit
Created November 11, 2013 14:15
Stochastic array of multinomial logistic regression parameter fits
probs.logit <- function(grps.seq, cov, coeff, x){
##
## multinomial logistic regression model fit for a given covariate
## e.g. probability sums per age =1
##
## x: covariate value e.g. age=16
## grps: vector of class labels for NATSAL groups e.g. het5yrs
## cov: covariate name to regress against (string)
## coeff: coefficients from the mlogit() fit
@n8thangreen
n8thangreen / probs.logit.covs
Created November 11, 2013 14:17
Vector of conditional probabilities from a multinomial logistic regression given particular covariate values
probs.logit_covs <- function(groups, cov, data){
##
## given a (set of) covariate values, calculates the class membership probabilities
##
## groups: response field name of interest in NATSAL e.g. het1yr (string)
## cov: explanatory covariate name e.g. age_shift (string)
## data: e.g. NATSAL.dat (array)
## ordered sequence of covariate levels
## copy those covered in data but could include intermediate values too