sdiazpa / epi590r-final-project

0 stars 2 forks source link

Regressions, tables, figures #3

Open arianaparquetteemory opened 1 day ago

arianaparquetteemory commented 1 day ago

None of these required debugging to run, and they look good! Btw, there's like a million ways you can make tables and regression models. The cool thing about R is that since it is open source, people will code R-packages to do anything and everything, and all of those packages are available to you. I'll share some code from my thesis to provide you with alternative options- but again, what you have works and looks great!

However, I believe you may have included only one inline code for R in your QMD, which would be the 1st missing half-point. Something that I want to empathsize, and you probably are aware of this, but the different file types (e.g. ".R"/R-scripts, ".rmd"/".qmd", ".lock", ".json") Etc. actually use different coding languages. So, any R-script is going to ONLY be receptive to R code. It's like taking a sheet of paper and writing a mathematical proof for an exam on it. You're not going to write anything except math on that piece of paper, and you're not going to write anything except R on R scripts. so inline code is not valid on R scripts, because it is ALL R anyways. An RMD/QMD can use several languages. the `{language} can be any language. RMD/QMD documents are more like reports that you can include aforementioned mathematical prompt within, but you also get context for it. RMD/QMD are great report documents because they can be formatted into literally any format. The little blurb at the top is written in what is called YAML, or "Yet Another Markdown Language" and sets the scene for your xyzMD document. You can add so many fun little thingies in this blurb. My personal favorite is code folding, which allows your output file to include or not include code blocks at the will of the report viewer. You can research these further if youre interested. i'm including my YAML code from my thesis as an example. Note that your output document can go into literally whatever format you want - PDF, .docx, html, textbook formats, presentations, etc. Note if you want to do PDF, you may have to adjust your coding to include the Tinytex and Latex packages in order for it to correctly format. RMD/QMDs are written in Markdown language, which is really just plain language. So you can write whatever you want, include code blocks and their results ( tables, figures, regressions, etc). Also imagine this situation: you're working on a project and you managed coding for this project's preliminary results. Months or years later, the data has updated. Your boss asks you to update your code. Do you want to run your analyses again and go through EVERY single piece of data in your report and manually change it to your new results, or would you prefer to re-run your analyses and your report automatically updates? The ladder, right? That's why these documents are sooooo convenient, and why inline text is one of many cool and convenient tools. So your surrounding text wont change, but if your data changes, so does the report numbers, because they are literally code! Another really cool tool is running your R scripts directly in your RMD/QMD document. I'll provide an example in another issue. It makes it so much easier, fr fr.

YAML example:

title: "Redacted" author: "Ariana Parquette" date: "2024-07-18" output: html_document: df_print: paged toc: true toc_float: true number_sections: true code_folding: hide highlight: tango theme: cerulean

arianaparquetteemory commented 1 day ago

Sorry to write that all in bold that was not intentional at all, lol.

Alternative Regression example:

Create a regression table

x_y_reg_base <- lm(redactedvariable1 ~ redactedvariable2 + age + gender, data = redactedds) xyregtbl<- stargazer(x_y_reg_base, x_y_reg_adj, type = "text", title = "Comparison of Regression Results for Base/Adjusted redacted, column.labels = c("Base Model","Fully Adjusted"), model.names = FALSE, digits = 2, summary = TRUE)

arianaparquetteemory commented 1 day ago

How to run an R script independently within a RMD document ( this MUST be in a code chunk)

run your script

source("code/02_prelim_analysis.R")

my directory in R in this scenario is thesiscode and code is the name of the folder underneath the thesiscode directory where my R scripts are located. This is dependent on your directory and is an example of a relative filepath. an absolute file path would be users/ariana/desktop/thesis/thesiscode/code/02_prelim_analysis.R. Hopefully you understand why it is important to use relative filepaths, especially in collaborations and ur projects. It is all dependent on ur source. This will update ur figures and whatnot in the scripts automatically within the report files. I also recommend using the "here::iam("code/02_prelim_analysis.R") in literally all of your documents with R in it. It doesnt sound helpful, but it highkey is.

arianaparquetteemory commented 1 day ago

alternative table code: ( insert label and unit formatting here) str(gidxa$b)

table_1 <- table1(~ age + gender + race + bmi + x + y + z + a | b, data = redacted) table_1 table_df <- as.data.frame(table_1) library(kableExtra)

Create a colorful table using kableExtra

table_1<- kable(table_df, format = "html", table.attr = "style='width:100%;'")%>% kable_styling(full_width = F, position = "left", bootstrap_options = c("striped", "hover", "condensed")) %>% row_spec(0, bold = TRUE, color = "white", background = "#0073B2") %>% # Header row row_spec(1:nrow(table_df), color = "black", background = ifelse(1:nrow(table_df) %% 2 == 0, "#f2f2f2", "white")) %>% row_spec(c(2, 5, 8, 12, 15,18,21,26), bold = TRUE) %>% footnote(general = "uwu", general_title = "Note:") table_1 saveRDS( table_1, file = "output/table_1.rds" )

print(table_1)

obviously this code may change due to ur personal preferences and dataset. Again, your tables were totally fine and any of this code should work- just want you to be aware of your options.