Compare commits

..

2 Commits

Author SHA1 Message Date
Alex Gebben Work
19680cca7e New class slides 2025-09-08 17:21:32 -06:00
Alex Gebben Work
aaeb13661f Working on slides 2025-09-08 17:21:02 -06:00
9 changed files with 133 additions and 15 deletions

View File

@ -5,7 +5,7 @@
# so the results can reproduced more easily if the data changes.
dir.create("Data",showWarnings = FALSE)
dir.create("Data/Orig_Data",showWarnings = FALSE)
#Check if direjctory exits
#Check if directory exits
dir.exists("Data/Orig_Data")
# Help --------------------------------------------------------------------

View File

@ -83,9 +83,9 @@
\end{frame}
%%%%%%%%%%%%%%
\begin{frame}{R studio Projects}
\begin{frame}{Rstudio Projects}
\only<1-5>{
If you are using R studio it is best practice to create a project file.
If you are using Rstudio it is best practice to create a project file.
\begin{itemize}
\onslide<2->{\item Sets the working directory.}
\onslide<3->{\item Allows all code to use relative paths.}
@ -124,7 +124,7 @@
\begin{frame}{File Management}
\begin{itemize}
\onslide<1->{\item Create directories for data}
\onslide<2->{\item Create Directories for scripts}
\onslide<2->{\item Create directories for scripts}
\onslide<3->{\item Make code do most of the cleaning}
\onslide<4->{\item Clear naming of files}
\onslide<5->{\item Document all files with a Readme file}
@ -145,21 +145,20 @@
\column{0.5\textwidth}
\textbf{Useful Functions}
\begin{itemize}
\onslide<2->{\item \texttt{create.dir()}}
\onslide<2->{\item \texttt{dir.create()}}
\onslide<3->{\item\texttt{dir.exists()}}
\onslide<4->{\item\texttt{file.exists()}}
\onslide<5->{\item\texttt{list.files()}}
\onslide<6->{\item\texttt{dir.create()}}
\end{itemize}
\column{0.5\textwidth}
\onslide<7->{\textbf{Saving Data}
\onslide<6->{\textbf{Saving Data}
\begin{itemize}
\onslide<8->{\item \texttt{write\_csv()}}
\onslide<9->{\item \texttt{write\_excel\_csv()}}
\onslide<10->{\item\texttt{write\_delim()}}
\onslide<11->{\item\texttt{saveRDS()}}
\onslide<12->{\item\texttt{ggsave()}}
\onslide<13->{\item\texttt{write.csv()}}
\onslide<7->{\item \texttt{write\_csv()}}
\onslide<8->{\item \texttt{write\_excel\_csv()}}
\onslide<9->{\item\texttt{write\_delim()}}
\onslide<10->{\item\texttt{saveRDS()}}
\onslide<11->{\item\texttt{ggsave()}}
\onslide<12->{\item\texttt{write.csv()}}
\end{itemize}
}
\end{columns}
@ -175,7 +174,7 @@
Keep your main working space clean, and separate what you can.
\vspace{1.5em}
\begin{enumerate}
\onslide<2->{\item Make diffrent files for each main data step. Downloading, cleaning, various analyses.}
\onslide<2->{\item Make different files for each main data step. Downloading, cleaning, various analyses.}
\onslide<3->{\item You can save the data downloading portion as a file called \texttt{1\_EIA\_Data\_Proc.R}}
\onslide<4->{\item In the main file you could load the code with \texttt{source(``1\_EIA\_Data\_Proc.R'')}}
\onslide<5->{R will run the code in the current directory even if the script is saved elsewhere.}
@ -218,7 +217,7 @@
\onslide<3->{\item File names contain spaces}
\onslide<4->{\item Same name but different capitalization}
\onslide<5->{\item Names dont describe their contents ``temp''}
\onslide<6->{\item Outputs should be in seperate directory from inputs}
\onslide<6->{\item Outputs should be in separate directory from inputs}
\onslide<7->{\item Outputs files should have dates in the name}
\end{itemize}
}

View File

@ -0,0 +1,8 @@
DAT <- 1:8
?apply
apply(DAT,mean)
?mapply
x <- as.data.frame(cbind(x1 = c(3,NA,12,7), x2 = c(4:1, 2:5)) )
dimnames(x)[[1]] <- letters[1:8]
apply(x, 2, mean, trim = .2,na.rm=TRUE)
?mean

BIN
Class6/Slides/Function.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 51 KiB

BIN
Class6/Slides/If.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View File

@ -0,0 +1,111 @@
\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 and Loops}
\author{Alex Gebben}
\begin{document}
% Title Slide
\begin{frame}
\titlepage
\end{frame}
%%%%%%%%%%%%%%%%%%
\begin{frame}{Loops}
\only<1->{There are three types of loops in R }
\begin{enumerate}
\onslide<2->{ \item{A \emph{for} loop runs for each value in a list}}
\begin{itemize}
\onslide<3->{\item \texttt{for(i in 1:10){Code to run}}}
\end{itemize}
\begin{enumerate}
\onslide<4->{ \item{A \emph{While} loop runs if a condtion is metif a condtion is met.\emph{Can lead to infinite loops}}}
\begin{itemize}
\onslide<5->{\item \texttt{while(a==b){Code to run}}}
\end{itemize}
\onslide<4->{apply functions }
\begin{itemize}
\onslide<5->{\item \texttt{while(a==b){Code to run}}}
\end{itemize}
\end{enumerate}
\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}

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB