\documentclass[aspectratio=169,ignorenonframetext,table,dvipsnames]{beamer}
% \documentclass[ignorenonframetext,xcolor=pdflatex,table,dvipsnames]{beamer}

% For handouts:
%\usepackage{pgfpages}
%\pgfpagesuselayout{4 on 1}[a4paper,border shrink=5mm,landscape]

% Slides i fuld størrelse:
% \usepackage{pgfpages}
% \pgfpagesuselayout{resize to}[a4paper,border shrink=5mm,landscape]


%\usetheme[nat,en,style=alternative, footstyle=high, TPomitframeno,wide, totalframes=show]{Frederiksberg}

\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\usepackage{multimedia}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{mathptmx}
\usepackage{multirow}
\usepackage{hyperref}

\newcommand{\ols}{\mathrm{OLS}}

\definecolor{kucolor}{rgb}{0,0.5,0}
\newcommand{\kualert}[1]{{\bf \color{kucolor}{#1}}}

%% \definecolor{kucolor}{rgb}{0.5,0,0}
%% \newcommand{\kualert}[1]{{\bf \color{kucolor}{#1}}}
%% \newcommand{\myalert}[1]{{{\bf {#1}}}}


\title[]{Introduction to R}
\author[]{Bo Markussen \\ Department of Mathematical Sciences}
\institute[]{Data Science Lab \\ \url{https://datalab.science.ku.dk}}
\date[]{}


\begin{document}

\frame[plain]{\titlepage}


\section{Introduction}

\begin{frame}{What is Data Science Lab?} 


\begin{itemize}
\item Collaboration between Math and Computer Science. 
\item Funding from SCIENCE.
\item Overall aim: Enhance quality of scientific data analyses in research carried out at SCIENCE. 
\item Activities: Consultancy meetings for students and researchers, research collaborations, workshops.
\item More info and contact details: \alert{\url{https://datalab.science.ku.dk/english/}}
\end{itemize}
\end{frame}


\begin{frame}{Who are you? And what's the purpose of this event?}

We hope that you

\begin{itemize}
\item are interested in tools for data exploration, data analysis, and data visualization,
\item are open-minded and ready to play with R in a moment.
\end{itemize}

\smallskip

We don't expect that you have any experience with R.

\bigskip

\pause

By this course, we hope to
\begin{itemize}
\item get you started with R, so you can start using R ``at home'',
\item give you an impression about the possibilities with R.
\end{itemize}
\end{frame}


\section{R and RStudio}

\begin{frame}{What is R and RStudio?}

What is R and RStudio?
\begin{itemize}
\item R is a software environment for statistics and graphics.
\item RStudio is a user interface to R (but you can  use R without it).
%\item R Markdown is a file format for making dynamic documents that integrate code, output, graphs and text.
\end{itemize}

\bigskip

A lot of things has happened with R over the last decade, in particular  regarding data handling and graphics. Buzzword: \emph{Data Science} (but not so much AI). 

%\bigskip

%We try to incorporate (some of) this development in this course.
\end{frame}


\begin{frame}{Why use R?}

\begin{itemize}
\item Easy to manipulate data (in the good sense) and explore data.
\item All ``standard statistical tools/methods'' are available in R --- and many, many non-standard ones, too.
\item Graphics: Quite easy to make high quality figures as you want them.
\item Programming language: All steps in your analysis may be saved in a script (reproducible research) ---  and you can implement your own methods.
\item Simulation (making artificial data to examine your methods).
\item Open-source, free, runs on all platforms.
\item Large community, R-packages made available by other users.
\end{itemize}
\end{frame}


\begin{frame}{How to access R}
\framesubtitle{Two possibilities: (1) Locally on your own laptop, 
  (2) in the ``cloud''.}

\begin{enumerate}
\item To use R on your own laptop:
\begin{itemize}
\item Install R from \alert{\url{https://www.r-project.org}}
\item Install RStudio from \alert{\url{https://posit.co/products/open-source/rstudio/}}
\item Use data and script files placed on your own laptop.
\item For further info on how to install R see the
  \emph{Welcome email} we sent to you.
\end{itemize}

\medskip

\item Cloud solutions, for example

\begin{itemize}
\item The ERDA platform at UCPH, see \alert{\url{http://www.erda.dk}}.
\item UCloud from SDU eScience Center that support HPC computing in
  Denmark, see \alert{\url{https://docs.cloud.sdu.dk}}. and
%  \alert{\url{https://escience.sdu.dk/index.php/ucloud/}}.
%  \alert{\url{https://docs.cloud.sdu.dk/intro/interactivity.html}} and
    
  
  
%\item UCLOUD, see how to get access at \alert{\url{https://cloud.sdu.dk}}.
\end{itemize}
\end{enumerate}

% The ERDA platform at UCPH, see \alert{\url{http://www.erda.dk}}
% \begin{itemize}
% \item Sign-up and login at \alert{\url{http://www.erda.dk}}
% \item Start RStudio by clicking: Jupyter $\rightarrow$ Start DAG $\rightarrow$ Start My Server $\rightarrow$ R Notebook with R-Studio $\rightarrow$ Start $\rightarrow$ Rstudio
% \item Data files etc.\ need to be uploaded to ERDA before ERDA can use them.
% \item If wanted, generated files (e.g.\ data or graphics files) afterwards can be download to your local laptop.
% \end{itemize}
% \item An alternative and more potent cloud solution is UCLOUD, see how to get access at \alert{\url{https://cloud.sdu.dk}}.
% \end{enumerate}

\end{frame}


\section{Course plan and material}

\begin{frame}{What is going to happen?}
\framesubtitle{Mix of lecture-type presentations and hands-on exercises.}

Tentative plan:
\begin{itemize}
\item $\, $ 9.30--10.30: $\ \ $I. Working with R and RStudio.
\item 10.30--12.00: $\ $II. Working with data in R.
\item 12.00--12.30: $\ \ \ \ $ Lunch break.
\item 12.30--14.00: III. Graphics in R.
\item 14.00--15.30: IV. Quarto and R markdown.
\item Not done: $\ \ \ \ $V. Statistical analysis in R.
\end{itemize}
\end{frame}


\begin{frame}{Course material}

Available in a ZIP-file at
\begin{equation*}
\hspace{-0.2cm}
\text{\small \url{https://datalab.science.ku.dk/english/course/r-intro/}}
\end{equation*}

\begin{itemize}
\item These slides.  
\item R presentations as R scripts, and as Rmd, pdf and html.
\item Exercise sheets as Rmd, pdf and html.
\item Data for presentations and exercises (xlsx).
\item Solutions for exercises as Rmd, pdf and html.
\end{itemize}
\end{frame}


\begin{frame}{The exercises}

\begin{itemize}
\item There are presumable far too many questions in the exercises! Not important that you get through everything. 
\item Do whatever you feel like, according to  interests and experience.
\item The exercises do not explain much statistics --- this is a course on R rather than statistics.
\item Often: ``Try the following commands, and  explain what happens''. You are encouraged to play!
\end{itemize}

\medskip

Best: Work together in groups and discuss whatever comes up. Ask for help, clarifications, and discussions.
 
\end{frame}

% \begin{frame}{Material about R}

% ***

% Books:
% \begin{itemize}
% \item Dalgaard: {\em Introductory Statistics with R}
% \item Ekstrøm and Sørensen: {\em Introdunction to Statistical Data Analysis for the Life Scienes}
% \item Venables and Ripley: {Modern Applied Statistics with S} 
% \item Ekstrøm: {\em The R Primer}
% \item Martinussen, Skovgaard, Sørensen: {\em A First Guide to
%   Statistical Computations in R}
% %\item Two course notes: {\em Getting started with R} and {\em
% %  Statistical Analysis with R}
% % \item Help pages for any function (can be difficult to read, though)
% % \item Many, many guides available online and many, many books
% % \item google: Search 'R what-ever-topic-you-need' (without quotes)  
% \end{itemize}

% \medskip

% Other ressources:
% \begin{itemize}
% \item Help pages for any function (can be difficult to read, though)
% \item Many, many guides available online 
% \item google: Search 'R what-ever-topic-you-need' (without quotes)  
% \end{itemize}

% \end{frame} 


\begin{frame}{Material about R}

\begin{itemize}
\item Many statistics textbooks use R (also online guides and videos):
  \begin{itemize}
  \item Wickhan and Grolemund: \emph{R for Data Science} (2nd ed). Available online at \alert{\url{https://r4ds.hadley.nz}}.
  \item Wickham: \alert{\url{https://ggplot2-book.org/}}
  \item Venables and Ripley: \emph{Modern Applied Statistics with S}. 
  \end{itemize}
\item Help pages inside R and RStudio: ?'function name', ??'topic'
\item RStudio: Links to cheat sheets in the Help-menu.
\item google: Search \texttt{R whatever-topic-you-need}.
\item Chatbots.
\item RStudio now comes with an internal AI: \textbf{Posit Assistant}.
  \begin{itemize}
  \item There is a user fee, and I haven't yet tried this.
  \end{itemize}
\end{itemize}

\end{frame} 


\section{Lesson I}

\begin{frame}{I.\ Working with R and RStudio}

  \begin{itemize}
\item Make folders on your computer for files associated to R work.
\item Work with R scripts (or Quarto scripts); then your work is saved for later use.
\item Save your files often. It happens rarely, but R may crash if you ask it to do too weird things.
\item Write comments to yourself about the code.
%\item Markdown: Don't knit all the time. Instead, run code lines without knitting until you know how they should be. 
%\item Markdown: Organize your document, and put commands into smaller R chunks instead of one large R chunk.

\bigskip

\item Default RStudio set-up has four windows:
\medskip
\begin{itemize}
\item[Upper-left:] Editor for R scripts (and other programs).
\item[Lower-left:] Console for executing R code.
\item[Upper-right:] Show data objects in R session (also history of executed code).
\item[Lower-right:] Files, graphical output, package manager, help pages, \ldots
\end{itemize}
\end{itemize}

\end{frame}



  


\section{Lesson II}

\begin{frame}{II.\ Working with data in R}

\begin{itemize}
\item Many (newer) tools for working with data: selection of sub-datasets, transformation, sorting, \ldots
\item It is important to distinguish between data files (often xlsx), R datasets, variables, observations.
\item Datasets (either \texttt{`data frame'} or \texttt{`tibble'} inside R):
\begin{itemize}
\item Rows are called observations (or datalines).
\item Columns are called variables.
\end{itemize}
\item Variables can be extracted from datasets, but it is better to work on them {\bf in} the dataset.
\item Data types (categorical/numerical/ordinal/...) are important.
\end{itemize}
\end{frame}


\section{Lesson III}

\begin{frame}[fragile]
  \frametitle{III. Graphical systems available in R} 
\begin{itemize}
\item Base R: 
\begin{itemize}
\item Pen on paper system.
\item Functions: \verb+plot()+, \verb+hist()+, \verb+boxplot()+, \ldots
\item Produces nice graphics usable for scientific publications.
\end{itemize}

\medskip
 
\item \verb+ggplot2+:
\begin{itemize}
\item Add-on package.
\item Syntactical description of plots (\emph{Grammer of Graphics, Wilkinson 1999}).
\item Extended and implemented by \emph{Wickham (2010)}.
\item Produces even nicer graphics --- probably the best graphics in town.
\end{itemize}

\medskip

\item Other add-on packages:
\begin{itemize}
\item Previous to \verb+ggplot2+ the \verb+lattice+ package was a good choice, and it is still popular (but not recommendable any more).
\item Extensions to \verb+ggplot2+: \verb+ggfortity+,
  \verb+gridExtra+,  \verb+patchwork+, \ldots
\end{itemize}  
\end{itemize}
\end{frame}


\begin{frame}[fragile]
  \frametitle{Basic structure}
  
  \begin{columns}
  \begin{column}{0.55\textwidth}
  \begin{itemize}
  \item An \kualert{empty plot}
  \begin{verbatim}
  ggplot()
  \end{verbatim}
  \item with \kualert{data} and \kualert{aesthetics}
  \begin{verbatim}
  data=..., mapping=aes(...)
  \end{verbatim}
  \item is extended by \kualert{adding}
  \begin{verbatim}
  +
  \end{verbatim}
  \item \kualert{graphical objects}, e.g.\ points
  \begin{verbatim}
  geom_point()
  \end{verbatim}
  \end{itemize}
  \end{column}  
  \begin{column}{0.45\textwidth}
  \includegraphics[width=6.5cm]{yield.png}
  \end{column}  
  \end{columns}

\medskip

Example gallery: 
\alert{\url{https://r-graph-gallery.com/}}

%Cheat Sheet:
%\alert{\tiny\url{https://github.com/rstudio/cheatsheets/raw/master/data-visualization-2.1.pdf}}
 
 
% Cheat Sheet kræver login til GitHub!?
  
 % Let's look at examples in \verb+presentations3.Rmd+, and get hands on by working on \verb+exercises3.pdf+.
\end{frame}


\section{Lesson IV}

\begin{frame}{IV.\ Quarto and R Markdown}

Quarto is a file format for making dynamic documents. 

\begin{itemize}
\item Input: Text  and R code (in socalled chuncks)
\item Output, as html/docx/pdf: Formatted text, R code, R output, graphs
\end{itemize}

\bigskip

Benefits: 

\begin{itemize}
\item Higher  degree of reproducibility of research because ``everything'' is collected in one place.
\item Nice for communication, also with people who don't use R.
\item Easy to control the content of the final product, fx.\ should code and/or output be included?
\item Possibly using a ``visual'' editor (WYSIWYG).
\end{itemize}


%\medskip
%
%Cheatsheet: 
%\alert{\tiny\url{https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf}}




\end{frame}



\section{Lesson V}

\begin{frame}[fragile]
  \frametitle{V.\ Statistical analyses in R}

\begin{itemize}
\item  All standard analyses are built into R. Workhorses for this
\begin{verbatim}
    lm(), glm(), lmer(), glmer(), nls(), ...
\end{verbatim}

\medskip

\item A model is fitted and saved to an object. Information from this object may be extracted or processed further, e.g.\ by
\begin{verbatim}
 print(), summary(), anova(), confint(), predict(), 
 plot(), drop1(), update(), step(), ...
\end{verbatim}

\medskip

\item \verb+emmeans+-package provides estimated marginal means (a powerful method of presenting the results of a model) via
\begin{verbatim}
 emmeans(), pairs(), ...
\end{verbatim} 
\end{itemize}
\end{frame}


% \begin{frame}{Manipulation with data}


% \begin{itemize}
% \item Transformations of variables
% \item Subsets of data
% \item Identification and removal of outliers (take care, though)
% \item Merging datasets
% \item ...
% \end{itemize}

% \end{frame}

% \begin{frame}{A bit about graphics}
% \begin{itemize}
% \item Commands for scatterplots, histograms, barplots, boxplots, and
%   many, many other types of plots
% \item Usually the default plot is quite nice, but it is also easy to
%   change limits on axes, labels, text size, ...
% \item Easy to change colours, line types, symbols, ...
% \item Some functions, fx.\ {\tt plot}, works differently in different
%   situations (depending on the type of argument). 
% \end{itemize}
% \end{frame}

% \begin{frame}{Statistical analysis}

% Different types of models use different functions:
% \begin{itemize}
% \item Linear regression, ANOVA model: Use {\tt lm}
% \item Logistic regression, Poisson regression: Use {\tt glm}
% \item Non-linear regression: Use {\tt nls}
% \item Mixed models, i.e.\ models with random effects: Use {\tt lmer} (or {\tt
%     lme}) for continuous data and {\tt glmer} for discrete data 
% \item ...
% \end{itemize}

% \medskip

% In any case:
% \begin{itemize}
% \item The call to one of these functions gives an R object
% \item Useful functions for such objects: {\tt summary}, {\tt anova},
%   {\tt confint}, {\tt plot} (model validation)
% \end{itemize}

% \end{frame}

\end{document}
