├── .gitignore ├── README.md ├── figures └── screenshot.png ├── hayesmacros.sty ├── rmarkdown_homework_template.Rproj ├── template.Rmd └── template.pdf /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | .Rhistory 3 | .RData 4 | .Ruserdata 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## An rmarkdown template for stats homework 2 | 3 | Additional recommendations: 4 | - Create an RStudio project for the class 5 | - Keep each assignment in a separate folder 6 | - Have a separate folder for: 7 | - `data` 8 | - `figures` 9 | 10 | I tend to keep the LaTeX macro file at the top level so I only have a single copy. 11 | 12 | ## What the current template looks like 13 | 14 | ![](figures/screenshot.png) 15 | -------------------------------------------------------------------------------- /figures/screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexpghayes/rmarkdown_homework_template/4e976d1f2e4b445082baad9cb333df5ca1b42cb3/figures/screenshot.png -------------------------------------------------------------------------------- /hayesmacros.sty: -------------------------------------------------------------------------------- 1 | \ProvidesPackage{hayesmacros}[2017/09/16 macros for personal use] 2 | \RequirePackage{amsmath, amsfonts, amsthm, mathtools, graphicx} 3 | 4 | % GENERAL environments 5 | 6 | \newtheorem*{theorem}{Theorem} 7 | \newtheorem*{cor}{Corollary} 8 | \newtheorem*{lemma}{Lemma} 9 | 10 | \newtheorem*{prop}{Proposition} 11 | \newtheorem*{claim}{Claim} 12 | \newtheorem*{example}{Example} 13 | 14 | \theoremstyle{definition} 15 | \newtheorem*{defin}{Definition} 16 | \newtheorem*{notation}{Notation} 17 | 18 | \theoremstyle{remark} 19 | \newtheorem*{remark}{Remark} 20 | 21 | % ALGEBRA SHORTCUTS mostly from MATH 306 22 | 23 | % \newcommand \gcd {\mathrm{gcd}} 24 | \newcommand{\gen}[1]{\ensuremath{\langle #1\rangle}} 25 | 26 | % ANALYSIS SHORTCUTS, mostly from MATH 322 27 | 28 | \newcommand \f [1] [x] {f(#1)} 29 | \newcommand \fn [1] [x] {f_n(#1)} 30 | \newcommand \fm [1] [x] {f_m(#1)} 31 | \newcommand \gn [1] [x] {g_n(#1)} 32 | 33 | \newcommand \an {a_n} 34 | \newcommand \bn {b_n} 35 | 36 | \newcommand \R {\mathbb{R}} 37 | \newcommand \Rn {\mathbb{R}^n} 38 | \newcommand \Q {\mathbb{Q}} 39 | \newcommand \Z {\mathbb{Z}} 40 | \newcommand \Zp {\mathbb{Z}^+} 41 | \newcommand \cL {\mathcal{L}} 42 | 43 | % CALCULUS SHORTCUTS 44 | 45 | \newcommand \dx {\, \mathrm{d}x} 46 | \newcommand \grad {\nabla} 47 | \newcommand \ptl {\partial} 48 | \newcommand \dth {\frac{\ptl}{\ptl \th}} 49 | \newcommand \idth {\, \mathrm{d} \th} 50 | \newcommand \ddth {\frac{\ptl^2}{\ptl \th^2}} 51 | \newcommand \pto {\propto} 52 | \renewcommand \. {\cdot} 53 | \newcommand \x {\times} 54 | \newcommand \half {\frac 1 2} 55 | \newcommand \trans {{}^T} 56 | \newcommand \inv {{}^{-1}} 57 | \newcommand \aeq {\thickapprox} 58 | 59 | % PROBABILITY SHORTCUTS, mostly from STAT 525 60 | 61 | % TODO: common distributions, distributional params 62 | \newcommand \E [1] {\mathbb{E} \paren*{#1}} 63 | \newcommand \V [1] {\mathbf{Var} \paren*{#1}} 64 | \newcommand \Cov [2] {\mathbf{Cov} \paren*{#1, #2}} 65 | \newcommand \Cor [2] {\mathbf{Cor} \paren*{#1, #2}} 66 | 67 | 68 | \newcommand \N [2] {\mathrm{N} \paren*{#1, #2}} 69 | 70 | \newcommand \h [1] {\mathcal{H}_{#1}} 71 | \newcommand \BF {\mathrm{BF}} 72 | 73 | \newcommand \PoissonD [1] {\mathrm{Poisson} \paren*{#1}} 74 | \newcommand \UniformD [2] {\mathrm{Uniform} \paren*{#1, #2}} 75 | \newcommand \BetaD [2] {\mathrm{Beta} \paren*{#1, #2}} 76 | \newcommand \BernD [1] {\mathrm{Bernoulli} \paren*{#1}} 77 | \newcommand \ExpD [1] {\mathrm{Exp} \paren*{#1}} 78 | \newcommand \GammaD [2] {\mathrm{Gamma} \paren*{#1, #2}} 79 | \newcommand \InvGammaD [2] {\mathrm{InvGamma} \paren*{#1, #2}} 80 | \newcommand \BinomD [2] {\mathrm{Binomial} \paren*{#1, #2}} 81 | \newcommand \NegBinomD [2] {\mathrm{NegBinom} \paren*{#1, #2}} 82 | \newcommand \NormalD [2] {\mathrm{Normal} \paren*{#1, #2}} 83 | \newcommand \MVND [2] {\mathrm{MVN} \paren*{#1, #2}} 84 | \newcommand \HalfCauchyD [2] {\mathrm{C}^+ \paren*{#1, #2}} 85 | 86 | % parameterized in terms of variance, not standard deviation! 87 | \newcommand \NormalPDF [3] {\frac{1}{\sqrt{2 \pi #3}} \exp \paren*{-\frac{\paren*{#1 - #2}^2}{2 #3}}} 88 | \newcommand \CenteredNormalPDF [2] {\frac{1}{\sqrt{2 \pi #2}} \exp \paren*{-\frac{#1^2}{2 #2}}} 89 | \newcommand \MVNPDF [3] {\vert 2 \pi #3 \vert^{-\half} \exp \paren*{\paren*{#1 - #2}^T {#3}^{-1} \paren*{#1 - #2}}} 90 | \newcommand \CenteredMVNPDF [2] {\vert 2 \pi #2 \vert^{-\half} \exp \paren*{#1^T {#2}^{-1} #1}} 91 | 92 | \newcommand \GammaPDF [3] {\frac{\paren*{#3}^{#2}}{\Gamma \paren*{#2}} {#1}^{#2 - 1} \exp \paren*{-#3 #1}} 93 | 94 | 95 | % equations 96 | \def \be #1\ee {\begin{align} #1 \end{align}} 97 | 98 | % indexing variable 99 | \renewcommand \ij {i} 100 | 101 | % delimiters 102 | \DeclarePairedDelimiter{\paren}{(}{)} 103 | \DeclarePairedDelimiter{\set}{\{}{\}} 104 | \DeclarePairedDelimiter{\abs}{\lvert}{\rvert} 105 | \DeclarePairedDelimiter{\norm}{\lVert}{\rVert} 106 | \DeclarePairedDelimiterX{\ip}[2]{\langle}{\rangle}{#1, #2} 107 | 108 | 109 | 110 | % probability related, following BDA3 (Gelman) notation mostly 111 | \newcommand \p {p \paren} 112 | \newcommand \logp {\log p \paren} 113 | 114 | \newcommand \post {\th \vert y} 115 | \newcommand \llh {y \condth} 116 | \newcommand \pred {\yt \vert y} 117 | 118 | \newcommand \pprior {\p \th} 119 | \newcommand \pllh {\p {y \condth}} 120 | \newcommand \ppost {\p {\th \condy}} 121 | \newcommand \ppred {\p {\yt \condy}} 122 | 123 | \newcommand \logppost {\logp {\post}} 124 | \newcommand \logpprior {\logp \th} 125 | \newcommand \logpllh {\logp {\llh}} 126 | 127 | \newcommand \obs [1] [y] {{#1}_\ij} 128 | \newcommand \samp [1] [y] {{#1}_1, ..., {#1}_n} 129 | 130 | \newcommand \cond {\vert} 131 | \newcommand \condth {\vert \th} 132 | \newcommand \condy {\vert y} 133 | \newcommand \condobs [1] [y] {{#1}_\ij \condth} 134 | \newcommand \condsamp [1] [y] {{#1}_1, ..., {#1}_n \condth} 135 | 136 | \newcommand \dist {\sim} 137 | \newcommand \diid {\stackrel{\mathrm{iid}}{\sim}} 138 | \newcommand \dind {\stackrel{\mathrm{indep}}{\sim}} 139 | 140 | \newcommand \yt {\tilde{y}} 141 | \newcommand \yrep {y^\mathrm{rep}} 142 | \newcommand \yhat {\hat{y}} 143 | \newcommand \thhat {\hat{\th}} 144 | \newcommand \tauy {\tau_y} 145 | 146 | % things that go from 1 to n 147 | \newcommand \nsum {\sum_{\ij=1}^n} 148 | \newcommand \nprod {\prod_{\ij=1}^n} 149 | \newcommand \tsum {\sum_{\ij=1}^t} 150 | 151 | \newcommand \isum {\sum_{\ij=1}^\infty} 152 | \newcommand \iprod {\prod_{\ij=1}^\infty} 153 | 154 | % greek shortcuts 155 | \renewcommand \a {\alpha} 156 | \renewcommand \b {\beta} 157 | \newcommand \g {\gamma} 158 | \renewcommand \G {\Gamma} 159 | \newcommand \de {\delta} 160 | \newcommand \De {\Delta} 161 | \newcommand \e {\epsilon} 162 | \newcommand \ve {\varepsilon} 163 | \renewcommand \l {\lambda} 164 | \renewcommand \L {\Lambda} 165 | \renewcommand \r {\rho} 166 | \newcommand \vf {\varphi} 167 | \newcommand \s {\sigma} 168 | \newcommand \w {\omega} 169 | \renewcommand \O {\Omega} 170 | \renewcommand \th {\theta} 171 | \newcommand \ka {\kappa} 172 | \newcommand \z {\zeta} 173 | -------------------------------------------------------------------------------- /rmarkdown_homework_template.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: Default 4 | SaveWorkspace: Default 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: knitr 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | -------------------------------------------------------------------------------- /template.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "STAT XXX HW 1" 3 | author: "Your Name Here" 4 | date: "`r Sys.Date()`" 5 | output: 6 | pdf_document: default 7 | html_document: 8 | highlight: tango 9 | urlcolor: blue 10 | header-includes: \usepackage{hayesmacros} 11 | --- 12 | 13 | ```{r include = FALSE} 14 | knitr::opts_chunk$set( 15 | echo = FALSE, # don't show code 16 | warning = FALSE, # don't show warnings 17 | message = FALSE, # don't show messages (less serious warnings) 18 | cache = FALSE, # set to TRUE to save results from last compilation 19 | fig.align = "center", # center figures 20 | attr.source = '.numberLines' # add line numbers to code 21 | ) 22 | 23 | library(tidyverse) # load libraries you always use here 24 | 25 | set.seed(27) # make random results reproducible 26 | ``` 27 | 28 | ## Notes on the YAML header 29 | 30 | - Be sure to change the title and author fields 31 | - The date is automatically set to the current day when knitting 32 | - The `header-includes` portion loads a personal sheet of LaTeX macros. I recommend making one of these as well as it can greatly speed up the time to input math. See my personal macro file `hayesmacros.sty` as a starting point. If you aren't going to load a \LaTeX macro file, remove those lines. 33 | - `hayesmacros.sty` needs to be in the same folder as this file 34 | 35 | # Problem 1 36 | 37 | Here is a [link](https://adv-r.hadley.nz/). Your homework goes here. 38 | 39 | Briefly, some of the custom latex macros: $y = \b x + \e$. Also, some code: 40 | 41 | ```{r} 42 | fit <- lm(hp ~ ., mtcars) 43 | au <- broom::augment(fit) 44 | 45 | au %>% 46 | gather(x, val, -contains(".")) %>% 47 | ggplot(aes(val, .resid)) + 48 | geom_point() + 49 | facet_wrap(~x, scales = "free") 50 | ``` 51 | 52 | Yet more text, and another code section: 53 | 54 | ```{r} 55 | knitr::kable(head(select(au, 1:5))) 56 | ``` 57 | 58 | # Code 59 | 60 | ```{r ref.label = knitr::all_labels(), echo = TRUE, eval = FALSE} 61 | # this R markdown chunk generates a code appendix 62 | ``` 63 | -------------------------------------------------------------------------------- /template.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/alexpghayes/rmarkdown_homework_template/4e976d1f2e4b445082baad9cb333df5ca1b42cb3/template.pdf --------------------------------------------------------------------------------