Code
sudo dnf install 'dnf-command(copr)'
sudo dnf copr enable iucar/rstudio
sudo dnf install rstudio-desktopYou should program your statistical analysis, and we will cover some of the basics of how to do this in R. You also want your work to be replicable
You can read more about the distinction in many places, including
We focus on R because it is good for complex stats, concise figures, and coherent organization. It is built and developed by applied statisticians for statistics, and used by many in academia and industry. For students, think about labor demand and what may be good for getting a job. Do some of your own research to best understand how much to invest.
My main sell to you is that being reproducible is in your own self-interest.
First Steps…
Step 1: Some ideas and data about how variable \(X_{1}\) affects variable \(Y_{1}\), which we denote as \(X_{1}\to Y_{1}\)
Step 2: Pursuing the lead for a week or two
A Little Way Down the Road …
1 month later: someone asks about another factor: \(X_{2} \to Y\).
4 months later: someone asks about yet another factor: \(X_{3}\to Y_{1}\).
6 months later: you want to explore another outcome: \(X_{2} \to Y_{2}\).
2 years later: your boss wants you to replicate your work: \(X_{1}, X_{2}, X_{3} \to Y_{1}\).
Suppose you decided to code what you did beginning with Step 2.
It does not take much time to update or replicate your results.
Your results are transparent and easier to build on.
First Install R. Then Install Rstudio.
For help setting up, see any of the following links
For Fedora users, note that you need to first enable the repo and then install
sudo dnf install 'dnf-command(copr)'
sudo dnf copr enable iucar/rstudio
sudo dnf install rstudio-desktopMake sure you have the latest version of R and Rstudio for class. If not, then reinstall.
Rstudio is perhaps the easiest to get going with. (There are other GUI’s.)
In Rstudio, there are 4 panes. (If you do not see 4, click “file > new file > R script” on the top left of the toolbar.)
The top left pane is where you write your code. For example, type
1+1The pane below is where your code is executed. Keep you mouse on the same line as your code, and then click “Run”. You should see
> 1+1
[1] 2
If you click “Run” again, you should see that same output printed again.
You should add comments to your codes, and you do this with hashtags. For example
# This is my first comment!
1+1 # The simplest calculation I could think of
You can execute each line one-at-a-time. Or you can highlight them both, to take advantage of how R executes commands line-by-line.
As we proceed, you can see both my source code and output like this:
1+1
## [1] 2There are also special boxes
You can create “variables” that store values. For example,
x <- 1 # Make your first variable
x + 1 # The simplest calculation I could think of
## [1] 2x <- 23 #Another example
x + 1
## [1] 24y <- x + 1 #Another example
y
## [1] 24Your variables must be defined in order to use them. Otherwise you get an error. For example,
X + 1 # notice that R is sensitive to capitalization
## Error: object 'X' not foundYour variable names do not matter technically, but they should be informative
one <- 1 # good variable name
one
## [1] 1
one <- 43 # bad variable name
one
## [1] 43Good names avoid confusion later
x <- 43
x_plus_two <- x + 2 # better
x_plus_two
## [1] 45R Script file as My_First_Script.R in your folderAs you work through the material, make sure to both execute and save your scripts. Add lots of commentary to your scripts. Name your scripts systematically.
There are often many ways to accomplish the same goal. You first scripts will be very basic and rough, but you can edit them later based on what you learn. And you can always ask R for help
sum(x, 2) # x + 2
?sumWe write script in the top left so that we can edit common mistakes.
# Mistake 1: using undefined objects
Y
# Mistake 2: spelling and spacing
Y < - 43
Y_plus_z <- Y + z
# Mistake 3: half-completed code
x + y +
x_plus_y_plus_z <- x + y + z
# Seeing "+" in the bottom console?
# press "Escape" and try againThere are many good and free programming materials online.
The most common tasks can be found https://github.com/rstudio/cheatsheets/blob/main/rstudio-ide.pdf
Some of my programming examples originally come from https://r4ds.had.co.nz/ and I recommend https://intro2r.com.
I have also used online material from many places over the years, as there are many good yet free-online tutorials and courses specifically on R programming. See e.g.,
For more on why to program in R, see