Ted Kwartler

Sports Analytics in Practice with R


Скачать книгу

The code executed in this book should work for either cloud or local but installation of base-R and R-Studio on a server is not covered. Therefore, please download the R-Studio desktop IDE by navigating to https://www.rstudio.com/products/rstudio.

      Essentially R-Studio sits on top of base-R. The IDE provides a modern GUI expected of today’s computer users while also adding functionality including the use of version control, terminal access and perhaps most importantly an easy way to create and view visualizations for easy export and saving to disk. Figure 1.1 illustrates the basic relationship for base-R and R-Studio. As you can see without base-R, the IDE will not function because none of the computational functions exist in the IDE itself.

      Figure 1.1 The relationship between base-R and R-studio.

      Now that you have both base-R and R-Studio, let’s start to explore the programming environment. Think of an R environment as a relatively generic statistical piece of software. Once downloaded it can perform all tasks programmatically found in many of the popular spread sheet programs either online or for a laptop. The advantage of R is its extensibility mentioned earlier. R can be specialized from a generic statistical set of tools into a more interesting and nuanced piece of software. This is done through the download of specialized packages and called in the console by loading the package for the task at hand.

      Figure 1.2 The R-Studio IDE console.

      Figure 1.3 The upper left R script with basic commands and comments.

      The first code to be executed, beginning on a non-commented line, is a simple arithmetic operation shown below.

      2 + 2

      Figure 1.4 Showing the code execution on line 2 of the script being transferred to the console where the result 4 is printed.

      Next, let’s execute another command which will illustrate another pane of the IDE. If your cursor is on line 5 of the R script, `plot(x = 1, y = 2)` and you click the “run” icon you will now see a simple scatter plot visual appear in the lower right utility pane titled “Plots.” Each tab of the utility pane is described below:

       Files—This is a file navigation view, where you can review folders and files to be used in analysis or saved to disk.

       Plots—For reviewing any static visualizations the R code creates. This pane can also be used for resizing the image using a graphical user interface (GUI) and saving the plots to disk.

       Packages—Since R needs to be specialized for a particular task, this pane lists your local package library with official documentation and accompanying examples, vignettes, and tutorials.

       Help—Provides various resources for obtaining help with R and its many tasks.

       Viewer—This pane allows you to view the small webpages and dynamic interactive plots which R can create.

      Figure 1.5 The basic scatter plot is instantiated in the lower right, “Plots” plane.

      # Create an object xVal <- 1