Commit 8afaa873 authored by Simon Pintarelli's avatar Simon Pintarelli

noweb R template added

parents
% Emacs hint: use M-x ess-noweb-set-code-mode, then type R-mode
\documentclass[a4paper]{article}
\usepackage{subfig}
\usepackage[compact,center,bf]{titlesec}
\usepackage[sc]{mathpazo}
\usepackage[margin=1.5cm, top=2.5cm]{geometry}
\usepackage{placeins}
\usepackage{multicol}
\usepackage{fancyhdr}
\usepackage{caption}
\usepackage{adjustbox}
\captionsetup{labelformat=empty}
<<<<<<< variant A
>>>>>>> variant B
======= end
\pagestyle{fancy}
<<<<<<< variant A
\chead{Komplexe Analysis FS16}
\lhead{DMATH ETHZ}
\rhead{Prof. Muster}
>>>>>>> variant B
======= end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ==================== USER SETTINGS ==================== %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chead{LECTURE NAME}
\lhead{DMATH ETHZ}
\rhead{LECTURER}
<<usersettings, echo=FALSE>>=
maxpoints <- 52 # max. points
dp <- 0.5 # point interval
dg <- 0.25 # grade interval
## piecewise linear grading scale, make sure to include maxpoints
int_points <- c(0, 21, 46, maxpoints)
int_grades <- c(1, 4, 6, 6)
@
\begin{document}
<<setup, include=FALSE, purl=FALSE, cache=FALSE>>=
# from http://stackoverflow.com/questions/11030898/knitr-how-to-align-code-and-plot-side-by-side
partWidth <- 45
fullWidth <- 80
options(width = fullWidth)
## option() settings, just for the current chunk
knit_hooks$set(r.opts=local({
ropts <- NA
function(before, options, envir) {
if (before) {
ropts <<- options(options$r.opts)
} else {
options(ropts)
}
}
}))
## (2) OUTPUT HOOK FUNCTION
## Define a custom output hook function. This function processes _all_
## evaluated chunks, but will return the same output as the usual one,
## UNLESS a 'codefig' argument appeared in the chunk's header. In that
## case, wrap the usual textual output in LaTeX code placing it in a
## narrower adjustbox environment and setting the graphics that it
## produced in another box beside it.
defaultChunkHook <- environment(knit_hooks[["get"]])$defaults$chunk
codefigChunkHook <- function (x, options) {
main <- defaultChunkHook(x, options)
before <-
"\\vspace{1em}\n
\\adjustbox{valign=t}{\n
\\begin{minipage}{.59\\linewidth}\n\\centering"
after <-
paste("\\end{minipage}}
\\hfill
\\adjustbox{valign=t}{\\centering",
paste0("\\includegraphics[width=.4\\linewidth]{figure/",
options[["label"]], "-1.pdf}}"), sep="\n")
## Was a codefig option supplied in chunk header?
## If so, wrap code block and graphical output with needed LaTeX code.
if (!is.null(options$codefig)) {
return(sprintf("%s %s %s", before, main, after))
} else {
return(main)
}
}
knit_hooks[["set"]](chunk = codefigChunkHook)
## (3) TEMPLATE
## codefig=TRUE is just one of several options needed for the
## side-by-side code block and a figure to come out right. Rather
## than typing out each of them in every single chunk header, we
## define a _template_ which bundles them all together. Then we can
## set all of those options simply by typing opts.label="codefig".
opts_template[["set"]](
codefig = list(codefig=TRUE, fig.show = "hide",
r.opts = list(width=partWidth),
tidy = TRUE,
tidy.opts = list(width.cutoff = partWidth)))
@
<<libaries, echo=FALSE>>=
library(ggplot2)
library(reshape)
library(dplyr)
library(xtable)
@
<<prepare data, echo=FALSE>>=
dset <- read.csv('data.csv', encoding='ISO-8859-1')
dset <- dset[, !(names(dset) %in% c("Familienname", "Vorname"))]
dset <- melt(dset, id=c("Nummer"))
dset$value <- as.numeric(dset$value)
total_points <- group_by(dset, Nummer) %>% summarize(s=sum(value))
linscale <- data.frame(approx(int_points, int_grades, seq(0, maxpoints, by=dp)))
scale <- data.frame(grade=floor(linscale$y*1/dg)*dg, s=linscale$x)
grades <- data.frame(left_join(x=total_points, y=scale, by="s"))
colnames(grades) <- c("legi", "points", "grade")
grades$legi <- as.factor(grades$legi)
# plot grading scale (linear interpolation and projection to existing marks)
scale_plot <- cbind(linscale, col=rep("linear interpolation", nrow(linscale)))
colnames(scale_plot) <- c("s", "grade", "col")
scale_plot <- rbind(scale_plot, cbind(scale, col=rep("rounded", nrow(linscale))))
@
\section*{Grades}
\label{sec:grades}
<<grades, echo=FALSE, warning=TRUE, fig.width=5, fig.height=5, out.width="0.49\\linewidth", fig.show='hold', fig.env="figure*", fig.subcap=c("Histogram", "CDF"), fig.pos='h!'>>=
ggplot(grades, aes(x=grade)) + geom_histogram(binwidth = 0.25) + scale_x_continuous(breaks=seq(1,6, by=1)) + ylab("Num. Stud")
ggplot(grades, aes(x=grade)) + stat_ecdf() + scale_x_continuous(breaks=seq(1,6, by=1)) + ylab("Percentage")
@ %def
<<violing, echo=FALSE, fig.height=1, fig.width=3, fig.show='hold'>>=
ogrades <- cbind.data.frame(x=rep("grade", nrow(grades)), y=grades$grade)
opoints <- cbind.data.frame(x=rep("points", nrow(grades)), y=grades$points)
ggplot(ogrades, aes(x=x,y=y)) + geom_violin() + geom_boxplot(width=.1, fill="black", outlier.colour=NA) +
stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=2.5) + coord_flip() + ylab("") + xlab("")
ggplot(opoints, aes(x=x,y=y)) + geom_violin() + geom_boxplot(width=.1, fill="black", outlier.colour=NA) +
stat_summary(fun.y=median, geom="point", fill="white", shape=21, size=2.5) + coord_flip() + ylab("") + xlab("")
@
<<grading_scale_plot, opts.label="codefig", echo=FALSE, results='asis', cache=FALSE, fig.width=5, fig.height=5>>=
I <- match(seq(1, 6, by=dp), scale$grade)
plot1 <- ggplot(scale_plot, aes(x=s, y=grade, col=col)) + geom_line() + scale_y_continuous(breaks=seq(1,6,by=0.5)) + scale_x_continuous(breaks=seq(0, maxpoints, by=ceiling(maxpoints/10))) + xlab("points") + theme(legend.position="bottom")
plot1 + theme(legend.title=element_blank())
print(xtable(data.frame(Grade=scale$grade[I], Points=scale$s[I])), include.rownames=FALSE, floating=FALSE)
@
{\Large \Sexpr{ passed } of \Sexpr{ncand} candidates have passed.}
\pagebreak
\FloatBarrier
\section*{Statistics per Task}
\label{sec:point-statistics}
<<out,echo=FALSE,warning=FALSE, fig.width=5, fig.height=5, out.width="0.49\\linewidth",fig.cap="Point distribution (per task)",fig.show='hold',fig.subcap=c("Histogram","CDF"), fig.env="figure*", fig.pos='h!'>>=
ggplot(dset, aes(x=value)) + geom_histogram(binwidth=1) + facet_wrap(~variable, scales="free") + ylab("Num. Stud.") + xlab("Points")
ggplot(dset, aes(x=value)) + stat_ecdf() + facet_wrap(~variable, scales="free") + xlab("Points") + ylab("Percentage")
@ %def
<<total, echo=FALSE, warning=FALSE, fig.width=5, fig.height=5, out.width="0.49\\linewidth", fig.cap="Point distribution (total)", fig.show='hold', fig.subcap=c("Histogram", "CDF"), fig.env='figure*', fig.pos='h!'>>=
ggplot(total_points, aes(x=s)) + geom_histogram(binwidth=2) + ylab("Num. stud.") + xlab("Total points") + xlim(0, maxpoints)
ggplot(total_points, aes(x=s)) + stat_ecdf() + xlim(0, maxpoints) + xlab("Total points")
@ %def
<<density, echo=FALSE, warning=FALSE, out.width="0.49\\linewidth", fig.cap="Total", fig.show='hold', fig.pos='h!', fig.height=3>>=
#ggplot(total_points, aes(x=s)) + geom_line(stat="density")
@ %def
\end{document}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment