|
1 | 1 | setwd("C:/Users/adamk/Dropbox (MIT)/Current/PML_Good_coding_practices_example") |
2 | 2 | renv::init() |
3 | 3 | 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) |
0 commit comments