OnlineNursingOwl
← All nursing posts

Biology · Studypool

RStudio Discussion

In this discussion, you will download R and RStudio, a data set, and perform basic manipulations of the data in R.

We will work through the following exercise in class. You will be directed on what you must submit during our class time.

Detailed Steps for Data Analysis in R:

Step 1: Install and Load Necessary Packages; Download R and RStudio, by visiting: R and RStudioLinks to an external site.

    • To successfully download RStudio, you must first download R. The provided link contains download links for both, so you do not need to go to two different locations to download them. RStudio is basically the more user-friendly and visually aesthetic version of R, where most professionals and students perform statistical analysis.

Step 2: Open R or RStudio:

  • If you don't have R or RStudio installed, download and install them from the official website (Step 1).

Step 3: Install the tidyverse package: This package includes several useful tools for data manipulation and visualization.

    • How to Install:
    • In the R console or script editor, type install.packages("tidyverse") and press Enter.
    • This command installs the tidyverse package from CRAN (Comprehensive R Archive Network).
    • Script Editor:

Figure 1: Script Editor

    • You must click the “Run” button in the console to run the command. This will only run the command that the cursor is on, as shown in Figure 1.
    • This time, both commands are highlighted in the console, so when you click run, it will run both commands, as shown in Figure 2.

Figure 2: Console with both commands highlighted

    • Console: In the console, pressing enter will run the command, unlike the script editor, where pressing enter will go to the next line. In the console, even if the cursor is only on one line, pressing enter will run that commands all commands before that have not yet been run.
    • Installing the package: the messages following the command may vary, but the package should be downloaded regardless unless there is an issue with your version of RStudio.

Figure 3: Install tidyverse

    • Checking for success: you can confirm a package is downloaded correctly by clicking on the packages tab on the right side of the screen and scrolling down the alphabetical package list to find the package's name. Figure 4 shows tidyverse is in the list, so we know it has been read.

Figure 4: tidyverse is on the list

Step 4: Load the tidyverse package:

    • After installation, load the package by typing library(tidyverse)and pressing Enter.
    • This command makes the functions from tidyverse available for use.
    • You know the package has been loaded if the box next to the package has been checked—for example, in Figure 4 above. It is also important to note that sometimes you might have messages coming after the library command and sometimes not depending on the package. So again, it is best to confirm by going to the packages tab and seeing if the box has been checked.

Figure 5: Libary tidyverse packages

Step 5: “Additional Libraries”

    • Download the three libraries using the commands below and confirm they were installed by checking the packages tab illustrated in Figure 6. Boxes have not been checked, meaning they have not been loaded. This was done intentionally and did not load to provide an example.

Figure 6: Three packages to install

Figure 7: Unchecked "corrplot"

Figure 8: Unchecked ggpubr

Figure 9: Unchecked broom

Step 6: Load the Dataset

    • Save the CSV file path.
    • Type and run: data <- read_csv("/path/to/your/prostateSurvival (2).csv").
    • Figures 10 and 11 below show how to save the dataset to a specific name and where to find that dataset once you have named it. Also, you would use the name you assigned to run commands with that dataset.

Figure 10: Run command

Note: you can see that this run command has the user's name in the command line, yours will be different.

Figure 11: File in the R Environment

Step 7: Explore the Dataset

    • Type and run: glimpse(data).
    • Type and run: summary(data).

Figure 12: Run: glimpse(data)

Figure 13: Run: summary(data)

Step 8: Clean the Data:

    • Type and run: sum(is.na(data)) to check for missing values.
    • Type and run: data <- data %>% drop_na() to remove rows with missing values.

Figure 14: sum(is.na(data))

Figure 15: data<-data%>%drop_na()

Note: This command drops the NA values of the dataset and reassigns it to another dataset. Since the previous command showed no NA values, the command would not change the dataset. For that reason, I kept the name the same. I just decided to run the command for the assignment.

Step 9: Basic Description of Categorical variables:

    • Categorical Variables are variables that have fixed and known possible values; in R, these get changed into factors to use them. For more information, please visit R FactorsLinks to an external site.
    • Run the following command to change the categorical variables into factors:

Figure 16: Commands to change into factors

Step 10: Descriptive statistics

    • Type and run: data %>% summarise(across(where(is.numeric), list(mean = mean, median = median, sd = sd)))

Figure 17: run: data %>% summarise(across(where(is.numeric), list(mean = mean, median = median, sd = sd)))

Note: This shows the the descriptive statistics for numerical values of the data set.

Step 11: Data Visualization:

    • Let’s look at the data: Type and run: <- data %>% select(where(is.numeric)) %>% cor()
      head(df)

Figure 18: head(df)

Note: We can use head() to see the first six entries and tr() to see the types of variables read in.

Figure 17: str()

Note:

The output clearly distinguishes between categorical (Factor w/ x amount of levels) and numerical variables, which is essential for the later steps.

Step 11: Categorical Variables

    • We can call str() again to see if they were successfully converted to categorical variables:
      Type and run: str(df)

Figure 18:

## tibble [14,294 x 6] (S3: tbl_df/tbl/data.frame) ## $ rownames: num [1:14294] 1 2 3 4 5 6 7 8 9 10 ... ## $ grade : Factor w/ 2 levels "mode","poor": 1 1 2 1 1 2 2 1 1 2 ... ## $ stage : Factor w/ 3 levels "T1ab","T1c","T2": 2 1 2 3 2 3 2 1 2 3 ... ## $ ageGroup: Factor w/ 4 levels "66-69","70-74",..: 4 3 3 2 2 3 4 4 3 3 ... ## $ survTime: num [1:14294] 18 23 37 27 42 38 18 78 47 13 ... ## $ status : num [1:14294] 0 0 0 0 0 2 0 0 0 0 ...

Note: Categorical variables.

    • We can make a general plot of the kinds of survival times we should expect using ggplot() from the ggplot2
      library and setting the plot type to a histogram:
    • ggplot(df, aes(x = survTime)) +
      geom_histogram(binwidth = 5, fill = "blue", color = "black") +
      labs(title = "Histogram of Survival Time", x = "Survival Time", y = "Frequency")

Figure 19: Histogram of Survival Time

    • We also want to see if the distribution differs for any subgroups within this data. For
      example, we might suspect that the stage of cancer might cause the distribution of survival times to be
      different. We can see if that’s the case as follows:
      df %>% group_by(stage) %>% ggplot(aes(x = survTime, fill = stage)) +
      geom_density(color = "black") + labs(title = "Histogram of Survival Time",
      x = "Survival Time", y = "Frequency") + facet_wrap(~stage)

Figure 20: Differences between Subgroups

    • We can also use some functions from dplyr to find the proportion of people who are in each of the three
      statuses broken down by stage of cancer:
      df %>% group_by(stage) %>% mutate(n = n()) %>% ungroup() %>%
      group_by(status,stage) %>% mutate(count = n()) %>% mutate(prop = round(count/n,3)) %>%
      select(stage,status,prop) %>% distinct()

Figure 20: Table of the functions from dplyr

    • This can be a little hard to understand, so we can also plot it using ggplot by color code:
      df %>% group_by(stage) %>% mutate(n = n()) %>% ungroup() %>%
      group_by(status,stage) %>% mutate(count = n()) %>% mutate(prop = count/n) %>%
      select(stage,status,prop) %>% distinct() %>% ungroup() %>% group_by(status) %>%
      arrange(stage, status) %>%
      ggplot(aes(x = status, y = prop, col=stage)) + geom_line()

Figure 22: Stage of Cancer When Compared to Status

    • It looks like perhaps the stage of cancer makes a difference in the status of the individual. Does it affect
      survival times? We can build a statistical model using lm() from base R.
      stage.fit = lm(survTime ~ stage, data = df)
      summary(stage.fit)

Figure 22: Survival times and Status

    • The function summary() prints out the coefficients estimated in the lm() statement. It seems that stage T1c
      of prostate cancer does significantly better than the other stages, which do not appear significantly different
      from each other. Does age affect survival time?
      mod.fit = lm(survTime ~ ageGroup, data=df) summary(mod.fit)

Figure 23: Age and Survival Time

Note: Based on the asterisks, the age group of 70-74 does not differ significantly from the average, whereas 75-79 does considerably worse, and 80+ does much worse. See the p-values and significance codes for evidence.

    • Now, run an ANOVA: anova(mod.fit

Figure 24: Anova

Note: We can also assess the overall significance of the age group variable by using the ANOVA () function. In the ANOVA, a p-value of less than .05 is considered significant, so the given p-value of something less than 2.2 * 10−16 is very significant, so it is the case that age group makes a difference when predicting survival time

At the end of the exercise, please post the three graphs you generated in the discussion.