### FUNCTION TO STANDARDIZE VARIABLES

# Within each group (treatment/control)
scale.group <- function(x, treat) {
  out <- NULL
  out[treat==1] <- scale(x[treat==1], center=mean(x[treat==1], na.rm=TRUE), scale=sd(x[treat==1], na.rm=TRUE))
  out[treat==0] <- scale(x[treat==0], center=mean(x[treat==0], na.rm=TRUE), scale=sd(x[treat==0], na.rm=TRUE))
  return(out)
}
