Computational_Econ/Class5/Slides/Functions_Manipulation.tex
2025-09-05 16:50:56 -06:00

115 lines
4.3 KiB
TeX

\documentclass{beamer}
\usepackage{graphicx}
\usepackage{multicol}
\usepackage{hyperref}
\usepackage{lipsum} % for placeholder text
\graphicspath{{pdf_images/}}
\title{ECON 4530/5530 \\ Computational Economics}
\subtitle{Data transformation}
\author{Alex Gebben}
\begin{document}
% Title Slide
\begin{frame}
\titlepage
\end{frame}
%%%%%%%%%%%%%%%%%%
\begin{frame}{Commenting Code}
\only<1-3>{
\onslide<1-3>{Comments are lines of code which do not run. These are \textbf{\emph{very}} important. }
\newline
\onslide<2-3>{ In R these are created by starting a line with ``\#''}
\onslide<3>{
\begin{enumerate}
\item{You should add a comment to any line of code which is not highly simple.}
\item{Keep a clean comment style}
\item{Comment above a section of code. Indent the following code}
\item{Always include the why of your code. The what and how of your code is less critical}
\item{Think about what you would need to know if you forgot everything about the project}
\end{enumerate}
}
}
\only<4>{
\includegraphics[width=0.7\textwidth]{What_Comments}
\includegraphics[width=\textwidth]{Why_Comments}
}
\end{frame}
\begin{frame}{Section Comments}
Section comments visually separate chunks of code. It is an excellent practice to distinguish types of analysis, or groups of code.
\newline
\vspace{1em}
\onslide<2->{\includegraphics[width=\textwidth]{Section_comments.png}}
\newline
\vspace{1em}
\onslide<3->{RStudio provides a keyboard shortcut to create these headers \texttt{(Cmd/Ctrl + Shift + R)}}
\end{frame}
%%%%%%%%%%%%
\begin{frame}{\emph{if} statements}
\only<1-8>{
\emph{If} only run code when certain condtions are met
\newline
\onslide<2->{ \textbf{Possible uses}}
\begin{enumerate}
\onslide<3->{\item Load a file only if it does not exist }
\onslide<4->{\item Switch the value of an entry}
\onslide<5->{\item Create a new dummy variable}
\onslide<6->{\item Account for changes in column names on servers}
\end{enumerate}
\onslide<7->{Can be combined with \emph{else}}
\newline
\onslide<8->{The R function \texttt{ifelse()} can be a good shortcut}
}
\only<9>{\includegraphics[width=0.75\textwidth]{If.png}}
\end{frame}
%%%%%%%%%%%%%%%%
\begin{frame}{Class Exercise}
Use an if statement to write a csv file in a ``Data'' directory if the folder exists, otherwise it creates the directory first.
\end{frame}
%%%%%%%%%%%%
\begin{frame}{functions}
\only<1-6>{
\emph{functions} save a set of code to use later.
\newline
\onslide<2->{They are exactly the same as the functions you already use in R}
\onslide<3->{Must define function inputs, code and output}
\begin{enumerate}
\onslide<4->{\item Use if you repeat code more than twice }
\onslide<4->{\item Stores complex code avoids typos}
\onslide<5->{\item Can be loaded in script files}
\onslide<6->{\item Could be used in your own library}
\end{enumerate}
}
\only<7>{\includegraphics[width=\textwidth]{Function.png}}
\only<8>{The \texttt{try()} function can be useful when creating your own functions. It prevents the code from stopping after a failure. Use with care.}
\end{frame}
%%%%%%%%%%%%%%%%
\begin{frame}{Class Exercise}
Turn the previous if statement into a function. Allow the user to pass in the name of the folder that is created.
\end{frame}
%%%%%%%%%%%%%%%%%%%
\begin{frame}{Class Exercise}
\only<1>{\LARGE Remember proper naming of files, data and scripts. Provide clear comments.}
\only<2>{
\begin{enumerate}
\item Identify three data set you are interested in on FRED
\item Load one data set using a URL saved as a character variable
\item Create a raw data subdirectory and save the raw data into it
\item Clean the data, at least update the column names
\item Save the cleaned data as a CSV and an RDS file in a cleaned data subdirectory with proper names
\item Include a comment on the first line of the file explain what the code is used for and why.
\item Comment any other code as needed
\item Turn this code into a function where the user passes in the URL, and column names
\item Save function in a separate Rscript in a directory for scripts. Pay attention to the name
\item In the main directory create a Rscript and in that script load script
\item Use the function to clean all three data sets
\item After this add a section of code to find the mean, and summary of the data
\end{enumerate}
}
\end{frame}
\end{document}