18
0

Merge branch 'master' of code.communitydata.science:cdsc_examples_repository

This commit is contained in:
Benjamin Mako Hill 2024-02-16 09:53:30 -05:00
commit 1154314041
4 changed files with 165 additions and 0 deletions

View File

@ -0,0 +1,81 @@
library(data.table)
library(parallel)
## how remember works:
## pass it a variable you want to keep
## it will store it in a list
## then at the bottom of your R document
## add the following:
##
##
##
# if (!nosave) {
## setup for the next thing to save
#r <- list()
## save the output and clean up
#remember(core.reg.table, "core.reg.table.tex")
#remember(model.sums, silent=TRUE)
## also save a copy of editsweeks that we used to fit these models
#remember(d, "editweeks")
#save(r, file="paper/knitr_rdata/02-models_for_presentation.RData")
#rm(r)
#}
##
##
##
remember <- function (v, k, silent=FALSE) {
if (missing(k)) {
k <- deparse(substitute(v))
}
## save to the global r variable/list
r[[k]] <<- v
if (!silent) {
print(r[[k]])
flush.console()
}
invisible(r[[k]])
## return(r[[k]])
}
## function help with building reduced summaries
reduced.summary <- function (m) {
magic.pattern <- "(wikia.com|jedipedia.de)"
if (class(m)[1] == "coeftest") {
x <- do.call(cbind, lapply(1:4, function (i) {m[,i]}))
rownames(x) <- names(m[,1])
colnames(x) <- c("Estimate","Std. Error","z value","Pr(>|z|)")
} else {
x <- coef(summary(m))
}
if (!class(x) == "list") {
x <- list(x)
}
out <- lapply(x, function (y) { y[!grepl(magic.pattern, rownames(y)),] })
## it's possible that this will only include one line/parameter (blocked)
if (class(out[[1]]) == "numeric") {
out <- lapply(out, function (y) {
y <- as.data.frame(rbind(y))
rownames(y) <- "blocked"
return(y)
})
}
Lower.95 <- round(out[[1]][,1] - (1.96*out[[1]][,2]),2)
Upper.95 <- round(out[[1]][,1] + (1.96*out[[1]][,2]),2)
cbind(out[[1]], Lower.95, Upper.95)
}
## make sure that appendix and nosave are always defined
if (!exists("appendix")) { appendix <- FALSE }
if (!exists("nosave")) { nosave <- FALSE }

View File

@ -0,0 +1,84 @@
## replace the below paths with your actual path to dropbox
## e.g., '/home/kaylea/Dropbox/Apps/Overleaf/WhateverICalledMyProject/figures/'
figPath = '../InOverleaf/figures/'
## e.g., '/home/kaylea/Dropbox/Apps/Overleaf/WhateverICalledMyProject/knitr_rdata/'
knitrPath = '../InOverleaf/knitr_rdata/'
source('lib-00-utils.R') # enables the remember command
set.seed(424242) # always!
library('texreg')
library('ggplot2')
magicNumberOne <- rnorm(1, 0, 1)
magicNumberOne
magicNumberTwo <- rnorm(1, 0, 1)
magicNumberTwo
Importance <- data.frame(value=rbeta(100,3,6)+0.1, source='Importance')
Quality <- data.frame(value=cumsum(rbeta(10,1,4)/10), source='Quality')
sampleDF <- rbind(Importance, Quality)
head(sampleDF)
head(Quality)
head(Importance)
g <- ggplot(sampleDF, aes(value, color=source)) +
geom_density(data=Importance, alpha=0.25, size=1) +
stat_ecdf(data=Quality, aes(x=value), geom='step') +
theme_classic() +
xlab("Time") +
ylab("Standard Deviation Units") +
theme(axis.ticks.x=element_blank(), axis.text.x=element_blank(),
axis.ticks.y=element_blank(), axis.text.y=element_blank(),
axis.title.x=element_text(size=8),
axis.title.y=element_text(size=8),
legend.position="bottom", legend.title=element_blank(),
legend.key.size= unit(.1, 'cm'), legend.key.height = unit(.1, 'cm'),
legend.margin=margin(0,0,0,0),
legend.key.width = unit(.1, 'cm'), legend.text = element_text(size=8)) +
annotate("text", size=3, label="Risk", x=.32, y =1.4) +
geom_segment(aes(x=.275, y=1.3, xend=.4, yend=1.3), color='black', size=0.13) +
geom_segment(aes(x=.275, y=1.33, xend=.275, yend=1.27), color='black', size=0.14) +
geom_segment(aes(x=.4, y=1.33, xend=.4, yend=1.27), color='black', size=0.14)
g
png(paste0(figPath, "simCurve.png"), units = "in", res = 300, width = 3, height = 3)
g
dev.off()
m1 <- lm(Importance ~ Quality, data=sampleDF)
summary(m1)
m2 <- lm(Importance ~ poly(Quality, degree=2, raw=2), data=sampleDF)
summary(m2)
con <- textConnection("model.texreg", "w") #remembered
sink(con, split=TRUE, type="output")
texreg(list(m1,m2), omit.coef = 'factor', stars=NULL, digits=2,
custom.model.names=c('M1 Description', 'M2 Description'),
#custom.coef.names=c('(Intercept)', 'Quality', 'Quality Squared'), # run without specifying first, just to be sure these are right!
use.packages=FALSE, table=FALSE, ci.force = TRUE)
sink()
close(con);rm(con)
m1.coefs <- summary(m1)$coefficients ## for easy reference within the latex document
m2.coefs <- summary(m1)$coefficients ## for easy reference within the latex document
if (!nosave) {
r <- list()
remember(magicNumberOne) #put everything you want to have access to inside latex here
remember(magicNumberTwo) #put everything you want to have access to inside latex here
remember(model.texreg)
remember(m1.coefs)
remember(m2.coefs)
save(r, file=paste0(knitrPath, "main.RData"), version=2) ## this creates the file, and we need to specify the version
print("Remembrances complete")
}