Practical Predictive Analytics
上QQ阅读APP看书,第一时间看更新

Code description

Here are some more details on what the code has accomplished:

  • Line 1 of the code contains the function require, which is just a way of saying that R needs a specific package to run. In this case require(graphics) specifies that the graphics package is needed for the analysis, and it will load it into memory. If it is not available, you will get an error message.  However, graphics is a base package and should be available.
  • Line 2 of the code loads the Women data object into memory using the data(women) function.
  • Lines 3-5 of the code display the raw data in three different ways:
    • View(women): This will visually display the DataFrame. Although this is part of the actual R script, viewing a DataFrame is a very common task, and is often issued directly as a command via the R Console. As you can see in the previous figure , the Women dataframe has 15 rows, and 2 columns named height and weight.
    • plot(women$height,women$weight): This uses the native R plot function, which plots the values of the two variables against each other. It is usually the first step one does to begin to understand the relationship between two variables. As you can see, the relationship is very linear.
    • head(women): This displays the first N rows of the Women dataframe to the console. If you want no more than a certain number of rows, add that as a second argument of the function. For example, Head(women,99) will display up to 99 rows in the console. The tail() function works similarly, but displays the last rows of data.
The utils:View(women) function can also be shortened to just View(women). I have added the prefix utils:: to indicate that the View() function is part of the utils package. There is generally no reason to add the prefix unless there is a function name conflict. This can happen when you have identically named functions sourced from two different packages which are loaded in memory. We will see these kind of function name conflicts in later chapters. But it is always safe to prefix a function name with the name of the package that it comes from.