Created
July 28, 2016 11:04
-
-
Save ashenkin/1d7d99642e206f2200c7eaef5f08da2f to your computer and use it in GitHub Desktop.
Deviation (sum to zero) contrasts are important to use in models when one is interested in grand mean parameters and per-level deviations from that mean. However, the usual method of coding these contrasts in R ends up throwing away the associated variable names. As long as you understand what deviation contrasts do, there is no danger in mainta…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contr.sum.keepnames <- function(...) { | |
# make deviation contrasts that don't lose the names of the factors in the model results | |
# from https://stackoverflow.com/questions/10808853/why-does-changing-contrast-type-change-row-labels-in-r-lm-summary | |
conS <- contr.sum(...) | |
colnames(conS) = rownames(conS)[-length(rownames(conS))] | |
conS | |
} | |
set_dev_contrasts <- function(df, colname = "site") { | |
# Set contrasts to "deviation coding" so site effects are as compared to overall mean across all sites. I.e., sites together should have a mean 0 effect. | |
# See http://www.ats.ucla.edu/stat/r/library/contrast_coding.htm | |
# Usage: my_df = set_dev_contrasts(my_df, colname = "col_to_set") | |
col2setup = df[,colname] | |
col2setup = as.factor(col2setup)[, drop = TRUE] | |
contrasts(col2setup) <- contr.sum.keepnames(levels(col2setup)) | |
df[,colname] = col2setup | |
return(df) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment