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

Commit 83fc99e

Browse files
author
Adam Kaplan
committed
renv project added
1 parent cd52a61 commit 83fc99e

16 files changed

+2816
-0
lines changed

.Rhistory

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
setwd("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices_example")
2+
renv::init()
3+
renv::snapshot()

.Rprofile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source("renv/activate.R")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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

analysis/analysis.R

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
lines(data$x, predict(regression), col = "red")
31+
dev.off()
32+
33+
### GGplot version ##########
34+
plotting_data <- data
35+
plotting_data$predictions <- predict(regression)
36+
ggplot2::ggplot(plotting_data, aes(x = x, y = y)) +
37+
geom_point() +
38+
geom_line(aes(y = predictions), color = "red")
39+
ggplot2::ggsave(here("report/figures/regression_ggplot.png"))

0 commit comments

Comments
 (0)