Introduction to R and Rstudio
Educational Statistics and Research Methods (ESRM) Program*
University of Arkansas
2024-08-18
Introduce ourselves
Go through the syllabus
Introduce R and Rstudio
Tell me:
I believe that everyone is capable and can significantly benefit from learning varied types of multivariate analysis
Philosophy: Focus on accessibility + learning-by-doing
The AMS class heavily emphasize on hands-on task-oriented practices
No anxiety-prone tasks (e.g., hand calculations, memorizing formulas)
No anxiety-prone methods of evaluation (e.g., timed tests)
Materials:
Lecture slides present concepts—the what and the why
Example documents: reinforce the concepts and demonstrate the how using software—R packages
All available at the course website (hosted outside of Blackboard)
8 in-class quiz (16 points): formative assessments aiming for refreshing our memory in last week
An opportunity for you to request topics for further clarification and review
Typically 2-3 questions that can be finished in 15 minutes
4 homework assignments (HW1-4, 84 points): Practice doing data analysis
Based directly on example given (no googling or ChatGPT needed, ever)
Homework 1-3: unique canned dataset
Homework 4: individual data analysis + written results section
Extra points (10 points, HW0)
My job (besides providing materials and assignments):
Answer questions via email, in individual meetings, or in group-based zoom office hours—you can each work on homework during office hours and get immediate assistance (and then keep working)
Your job (in descending order of timely importance):
Ask questions—preferably in class, but any time is better than none
Frequently review the class material, focusing on mastering the vocabulary, logic, and procedural skills
Don’t wait until the last minute to start homework, and don’t be afraid to ask for help if you get stuck on one thing for more than 15 minutes
Practice using the software to implement the techniques you are learning on data you care about
Do the readings for a broader perspective and additional example (best after the lecture)
Attendance: Strongly recommended but not required
Please do not attend in-person if you might be sick!
You can also join the class via Zoom
You won’t miss out: I will post YouTube recordings (audio + screenshare) by requested at the course website.
Changes will be sent via email by 9 am on class days
I will update the homework and in-class quiz links on class days. If not uploaded, then there are two situations: (1) I forget to do that. I will re-upload later and notify you by emails. (2) I decide not to upload it or remove it.
I may change to zoom-only for dangerous weather or if I am sick.
I will show examples primarily using R and R packages. Some important R packages include:
Tidyverse: a comprehensive R package including multiple mini packages for multiple data cleaning, data transformation.
ggplot2: a popular package for data visualization
Why not SPSS?
SPSS could only be used for some—but not all–of our content
More importantly, it doesn’t have as much room to grow; R has many new packages being developed via CRAN and GitHub
Why not SAS?
SAS is not open-sourced, meaning that we cannot check source code if something goes wrong
SAS is also commercial, but R is free
Why not Python, Julia, STAT or other software (e.g., matlab)?
They are not so popular in the relevant fields of educational statistics, which means you cannot get much support from the community.
But I think Python and Julia are worth learning! If you have Python / Julia code or tips that related to our course, please share them with me! I will give you extra points
My story: I am a heavy-duty R user:
I use R for almost everything (like course/conference slides, website building, data analysis, paper writing)
I am also learning Julia and Python but not plan to add them to my classes
I know how to use Mplus but do not use it very often
There are some point to consider
R packages are only as good as their authors (so little quality control)
Syntax and capabilities are idiosyncratic to the packages
The good things are:
If you really master R, you can do by yourself (write your own algorithm for complex model)
You can check the source code of R packages and know where issues come from
You can communicate with R package authors and provide some suggestions
You can be R package author yourself and be famous
If you feel the class is not so challenging for you, you can see materials for my other class, ESRM 6553: Advanced Multivariate Analysis
I DO NOT need you to memorize syntax
Instead, you can do exactly what I (still) do:
Find the example source file for what you need to do
Figure out how to modify it to work for your homework
Copy (CTRL+C), paste (CTRL+V), and find and replace are your friends
Don’t hesitate to ask for help (i.e., email me a screenshot)
Recommended prerequisite:
ESRM 64103. Experimental Design in Education or similar classes
ESRM 64203. Multiple Regression Techniques for Education or similar classes
What we will cover this semester:
Basic usage of R and RStudio (i.e., R download, package installation, package loading)
Descriptive statistics and bivariate associations (e.g., correlation)
Statistical concepts (e.g., null hypothesis testing, maximum likelihood estimation)
Generalized linear models (i.e., regression, ANOVA, path analysis, factor analysis)
This class will focus on multivariate analysis… so what is that?
- Multivariate statistics is a subdivision of statistics encompassing the simultaneous observation and analysis of more than one outcome variable (Dependent Variables / DVs).
– Wikipedia
Many different multivariate analysis models used multivariate statistics:
Generalized linear regression
Principal components analysis (PCA)
Path analysis
Factor analysis (FA)
Network analysis (NA)
Compared to univariate analysis, Multivariate Statistics provides a more thorough picture of the reality.
Imagine our theory of depression support following two models:
Note: Not all theoretical models can be estimated via statistics
Stanford-Washington University Eating Disorder Screen: Measurement Model is one special type of multivariate model
R is an comprehensive statistical and graphical programming language
We can use R language via multiple graphical user inferences or IDE, i.e., terminal, VS Code or RStudio.
We will mainly focus on RStudio because of its convenience
Rstudio is a product of posit company and is free to use for personal use
You can download and install R base via r-project.org (currently R-4.4.1)
Then, after the installation of R, you can download RStudio via posit.co (currently)
After installation of R and RStudio, you can open up the RStudio to start your R programming.
however, your R only has the base package
To enhance its utility, most users will install R packages for certain purposes
R packages are uploaded to some platforms (i.e., CRAN or Github) by researchers or companies
Those R packages typically have their version numbers. Some functions may be available for some version (like Ver. 1.1) but not be available in other versions.
Do not upgrade your packages if you code is running well
R users are free to download and use those R packages
To download certain package, you should know package name
For example, if you want to download the latest version of tidyverse
package, you can type in following command in the console panel of Rstudio
It contains the up-to-date version of R which may potentially be unstable
You can download the package from Github using pak
package
You can update the package and its dependencies
To operate certain tasks, you need to use functions contained in R packages
There are two ways of using R functions
Direct way: you don’t have to load your package first
Use-after-load way: Package is loaded in your session before you can call the function name without specifying the package name
How do you know you already load the package or not
You can use sessionInfo
function
R version 4.2.1 (2022-06-23)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS 15.0.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] htmlwidgets_1.6.4 compiler_4.2.1 fastmap_1.2.0 cli_3.6.3
[5] tools_4.2.1 htmltools_0.5.8.1 parallel_4.2.1 rstudioapi_0.16.0
[9] yaml_2.3.8 rmarkdown_2.26 knitr_1.46 jsonlite_1.8.8
[13] xfun_0.43 digest_0.6.35 rlang_1.1.4 evaluate_0.23
It outputs multiple info:
R version, Operations System, Matrix operation package, Locale
Attached packages (you can call the functions of those package)
Loaded package via a namespace (and not attached), which you cannot call functions and need to library
or require
them
After you finish R script, you have multiple ways of running the code:
Method 1: you can click Run
button in the top right-head of Rstudio
Method 2: you can select certain code and press Ctrl + Enter
(Win) or Command + Return
(Mac)
Method 3: you can Rscript [FILENAME].r
to run the whole script
Method 4: you can using R notebook to interactively run R code
Script file is .R | Script file is .rmd or .qmd | |
---|---|---|
Run the whole script |
|
|
Run the partial script |
|
|
Homework online portal will be delivered on the website
The homework system is based on Microsoft Form. You can answer the questions via Laptop, PC or Mobile Phone.
Let me use Homework 0 as one example.
We will:
ESRM 64503: Lecture01