Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit 13ed7af

Browse files
committed
Added presentation slides
Added workshop presentation slides and updated the README accordingly.
1 parent f8b29da commit 13ed7af

36 files changed

+870
-11
lines changed

.Rhistory

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
11
setwd("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices_example")
22
renv::init()
33
renv::snapshot()
4+
setwd("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example")
5+
renv::activate()
6+
install.packages("styler")
7+
styler:::set_style_transformers()
8+
styler:::style_active_file()
9+
# Libraries and setup #######
10+
#
11+
library(here)
12+
library(readr)
13+
library(stargazer)
14+
library(ggplot2)
15+
# Let `here` know what file this is using RELATIVE paths
16+
here::i_am("analysis/analysis.R")
17+
# Analysis ##################
18+
#
19+
# Can use here to access data using RELATIVE paths
20+
data <- readr::read_csv(here("data/simulated-data.csv"))
21+
# Run regression
22+
regression <- lm(y ~ x + 1, data = data)
23+
# Outputs ###################
24+
#
25+
## Save table ###############
26+
summary(regression)
27+
stargazer::stargazer(regression, out = here("report/tables/regression.tex"))
28+
## Plot results #############
29+
### Base R version ##########
30+
png(here("report/figures/regression.png"))
31+
plot(data$x, data$y)
32+
lines(data$x, predict(regression), col = "red")
33+
dev.off()
34+
# Libraries and setup #######
35+
#
36+
library(here)
37+
library(readr)
38+
library(stargazer)
39+
library(ggplot2)
40+
# Let `here` know what file this is using RELATIVE paths
41+
here::i_am("analysis/analysis.R")
42+
# Analysis ##################
43+
#
44+
# Can use here to access data using RELATIVE paths
45+
data <- readr::read_csv(here("data/simulated-data.csv"))
46+
# Run regression
47+
regression <- lm(y ~ x + 1, data = data)
48+
# Outputs ###################
49+
#
50+
## Save table ###############
51+
summary(regression)
52+
stargazer::stargazer(regression, out = here("report/tables/regression.tex"))
53+
## Plot results #############
54+
### Base R version ##########
55+
png(here("report/figures/regression.png"))
56+
plot(data$x, data$y)
57+
lines(data$x, predict(regression), col = "red")
58+
dev.off()
59+
### GGplot version ##########
60+
plotting_data <- data
61+
plotting_data$predictions <- predict(regression)
62+
ggplot2::ggplot(plotting_data, aes(x = x, y = y)) +
63+
geom_point() +
64+
geom_line(aes(y = predictions), color = "red")
65+
ggplot2::ggsave(here("report/figures/regression_ggplot.png"))
66+
debugSource("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/analysis/analysis.R")
67+
plot(data$x, data$y)
68+
lines(datas$x, predict(regression), col = "red")
69+
print(datas$x)
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"debugBreakpointsState": {
3+
"breakpoints": [
4+
{
5+
"id": 0,
6+
"path": "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/analysis/analysis.R",
7+
"line_number": 35,
8+
"function_steps": "",
9+
"function_name": "toplevel",
10+
"state": 1,
11+
"editor_state": 1,
12+
"editor_line_number": 35,
13+
"is_pending_debug_completion": false,
14+
"needs_updated_steps": true,
15+
"type": 1,
16+
"is_package_breakpoint": false,
17+
"package_name": ""
18+
},
19+
{
20+
"id": 3,
21+
"path": "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/analysis/analysis.R",
22+
"line_number": 39,
23+
"function_steps": "",
24+
"function_name": "toplevel",
25+
"state": 1,
26+
"editor_state": 1,
27+
"editor_line_number": 39,
28+
"is_pending_debug_completion": false,
29+
"needs_updated_steps": true,
30+
"type": 1,
31+
"is_package_breakpoint": false,
32+
"package_name": ""
33+
}
34+
]
35+
}
36+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"sortOrder": [
3+
{
4+
"columnIndex": 2,
5+
"ascending": true
6+
}
7+
],
8+
"path": "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example"
9+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"activeTab": 1
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"left": {
3+
"splitterpos": 331,
4+
"topwindowstate": "NORMAL",
5+
"panelheight": 778,
6+
"windowheight": 816
7+
},
8+
"right": {
9+
"splitterpos": 480,
10+
"topwindowstate": "NORMAL",
11+
"panelheight": 778,
12+
"windowheight": 816
13+
}
14+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"TabSet1": 1,
3+
"TabSet2": 0,
4+
"TabZoom": {}
5+
}

.Rproj.user/D8C8E36F/persistent-state

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
build-last-errors="[]"
2+
build-last-errors-base-dir=""
3+
build-last-outputs="[]"
4+
compile_pdf_state="{\"tab_visible\":false,\"running\":false,\"target_file\":\"\",\"output\":\"\",\"errors\":[]}"
5+
files.monitored-path=""
6+
find-in-files-state="{\"handle\":\"\",\"input\":\"\",\"path\":\"\",\"regex\":false,\"ignoreCase\":false,\"results\":{\"file\":[],\"line\":[],\"lineValue\":[],\"matchOn\":[],\"matchOff\":[],\"replaceMatchOn\":[],\"replaceMatchOff\":[]},\"running\":false,\"replace\":false,\"preview\":false,\"gitFlag\":false,\"replacePattern\":\"\"}"
7+
imageDirtyState="0"
8+
saveActionState="0"

.Rproj.user/D8C8E36F/rmd-outputs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
4+
5+
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"active_set":"","sets":[]}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"id": "38D9610B",
3+
"path": "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/simulation/create-data.R",
4+
"project_path": "simulation/create-data.R",
5+
"type": "r_source",
6+
"hash": "1521040682",
7+
"contents": "",
8+
"dirty": false,
9+
"created": 1637353896550.0,
10+
"source_on_save": false,
11+
"relative_order": 1,
12+
"properties": {
13+
"source_window_id": "",
14+
"Source": "Source"
15+
},
16+
"folds": "",
17+
"lastKnownWriteTime": 1637247593,
18+
"encoding": "UTF-8",
19+
"collab_server": "",
20+
"source_window": "",
21+
"last_content_update": 1637247593,
22+
"read_only": false,
23+
"read_only_alternatives": []
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Libraries and setup #######
2+
#
3+
library(here)
4+
library(readr)
5+
# Let `here` know what file this is using RELATIVE paths
6+
here::i_am("simulation/create-data.R")
7+
8+
9+
# Generate simple data ######
10+
#
11+
number_of_samples <- 1000
12+
x <- rnorm(number_of_samples, 0, 1)
13+
# Beta = [1, 2]^T
14+
y <- 1 + 2 * x + rnorm(number_of_samples, 0, 1)
15+
data <- data.frame(x = x, y = y)
16+
# Save the data using RELATIVE paths
17+
readr::write_csv(data, here("data/simulated-data.csv"))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"id": "5A302946",
3+
"path": "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/analysis/analysis.R",
4+
"project_path": "analysis/analysis.R",
5+
"type": "r_source",
6+
"hash": "1305533474",
7+
"contents": "",
8+
"dirty": false,
9+
"created": 1637354100717.0,
10+
"source_on_save": false,
11+
"relative_order": 2,
12+
"properties": {
13+
"source_window_id": "",
14+
"Source": "Source"
15+
},
16+
"folds": "",
17+
"lastKnownWriteTime": 1637355540,
18+
"encoding": "UTF-8",
19+
"collab_server": "",
20+
"source_window": "",
21+
"last_content_update": 1637355540387,
22+
"read_only": false,
23+
"read_only_alternatives": []
24+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Libraries and setup #######
2+
#
3+
library(here)
4+
library(readr)
5+
library(stargazer)
6+
library(ggplot2)
7+
# Let `here` know what file this is using RELATIVE paths
8+
here::i_am("analysis/analysis.R")
9+
10+
11+
# Analysis ##################
12+
#
13+
# Can use here to access data using RELATIVE paths
14+
data <- readr::read_csv(here("data/simulated-data.csv"))
15+
# Run regression
16+
regression <- lm(y ~ x + 1, data = data)
17+
18+
19+
# Outputs ###################
20+
#
21+
## Save table ###############
22+
summary(regression)
23+
stargazer::stargazer(regression, out = here("report/tables/regression.tex"))
24+
25+
26+
## Plot results #############
27+
### Base R version ##########
28+
png(here("report/figures/regression.png"))
29+
plot(data$x, data$y)
30+
print(datas$x)
31+
lines(datas$x, predict(regression), col = "red")
32+
dev.off()
33+
34+
### GGplot version ##########
35+
plotting_data <- data
36+
plotting_data$predictions <- predict(regression)
37+
ggplot2::ggplot(plotting_data, aes(x = x, y = y)) +
38+
geom_point() +
39+
geom_line(aes(y = predictions), color = "red")
40+
ggplot2::ggsave(here("report/figures/regression_ggplot.png"))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"id": "8300BEF5",
3+
"path": "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/analysis/analysis_UGLY.R",
4+
"project_path": "analysis/analysis_UGLY.R",
5+
"type": "r_source",
6+
"hash": "2735120548",
7+
"contents": "",
8+
"dirty": false,
9+
"created": 1637354107270.0,
10+
"source_on_save": false,
11+
"relative_order": 3,
12+
"properties": {
13+
"source_window_id": "",
14+
"Source": "Source"
15+
},
16+
"folds": "",
17+
"lastKnownWriteTime": 1637354729,
18+
"encoding": "UTF-8",
19+
"collab_server": "",
20+
"source_window": "",
21+
"last_content_update": 1637354729522,
22+
"read_only": false,
23+
"read_only_alternatives": []
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
library(readr)
2+
library(ggplot2)
3+
library(stargazer)
4+
5+
simdata <- read_csv("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/data/simulated-data.csv")
6+
reg <- lm(y ~ x + 1, data = simdata)
7+
summary(reg)
8+
# stargazer(reg, out = "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/report/tables/regression.tex")
9+
10+
# png("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/report/figures/regression.png")
11+
plot(simdata$x, simdata$y)
12+
lines(simdata$x, predict(reg), col = "red")
13+
# dev.off()
14+
15+
simdata$preds <- predict(reg)
16+
ggplot(simdata, aes(x = x, y = y)) +
17+
geom_point() +
18+
geom_line(aes(y = preds), color = "red")
19+
# ggsave("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/report/figures/regression_ggplot.png")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"id": "98DB3E73",
3+
"path": "C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices/PML_Good_coding_practices_example/report/report.Rmd",
4+
"project_path": "report/report.Rmd",
5+
"type": "r_markdown",
6+
"hash": "150609384",
7+
"contents": "",
8+
"dirty": false,
9+
"created": 1637354772574.0,
10+
"source_on_save": false,
11+
"relative_order": 4,
12+
"properties": {
13+
"source_window_id": "",
14+
"Source": "Source"
15+
},
16+
"folds": "",
17+
"lastKnownWriteTime": 1637249661,
18+
"encoding": "UTF-8",
19+
"collab_server": "",
20+
"source_window": "",
21+
"last_content_update": 1637249661,
22+
"read_only": false,
23+
"read_only_alternatives": []
24+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
title: "A Fancy Regression"
3+
author: "Adam Kaplan"
4+
date: "11/19/2021"
5+
output: pdf_document
6+
---
7+
8+
```{r setup, include=FALSE}
9+
library(knitr)
10+
library(here)
11+
library(readr)
12+
here::i_am("report/report.Rmd")
13+
knitr::opts_chunk$set(echo = TRUE)
14+
```
15+
16+
## Data creation
17+
18+
I created the data using the following code:
19+
```{r create-data, eval = FALSE, code = readr::read_lines(here("simulation/create-data.R"), skip = 10, n_max = 5)}
20+
```
21+
22+
## Results
23+
24+
Here is my fancy table:
25+
\input{`r here("report/tables/regression.tex")`}
26+
27+
And here is the super fancy plot:
28+
\begin{figure}[!h]
29+
\centering
30+
\includegraphics[width=\textwidth]{`r here("report/figures/regression.png")`}
31+
\end{figure}
32+
33+
And here is the even fancier ggplot:
34+
\begin{figure}[!h]
35+
\centering
36+
\includegraphics[width=\textwidth]{`r here("report/figures/regression_ggplot.png")`}
37+
\end{figure}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"source_window_id": "",
3+
"Source": "Source"
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"source_window_id": "",
3+
"Source": "Source"
4+
}

0 commit comments

Comments
 (0)