• Home
  • University of Lincoln
  • Data Skills for Life Sciences
  • Data Visualization and Statistical Analysis in Life Sciences

Data Visualization and Statistical Analysis in Life Sciences

12 June 2021 18:44 Chapter 5 Density distribution Violin plots using geom_violin() Violin plots are essentially like mini-density plots (which we encountered when plotting histograms). They show a nice overall shape to the distribution of the response (y) variable, which isn't clear from a boxplot. Many of the same customisations are available, and to use it you just swap geom_boxplot() for geom_violin() We use geom_linerange() to add error bars stat = "summary" to tell R that we want a summary of the points and fun.y = "mean_se" to tell R to that the summary we want is the standard error around the mean values. Example: Example: viol <- ggplot(data, aes(x = role, y = countries)) + geom_violin(fill = "grey", colour = "black") viol geom_point(stat = "summary", fun.y = "mean", size=3) + geom_linerange(stat = "summary", fun.data = "mean_se") + theme_minimal() Chapter 4 We can subset the data using several functions from the dplyr package: Example: o Subset rows either by ... - row numbers using the slice() function, or - selecting variable values using the filter() function o Subset columns either by ... - column numbers using the select() function, or - variable names, also using the select() function. data <- read.csv('games_data.csv') rows <- c(7, 10) dataSubset <- data %>% slice( seq(rows[1], rows[2], 1) ) dataSubset ? Adding Variables by using mutate() Example dataSubset <- data %>% mutate(log(example) = log(example2)) dataSubset We use pipe %>%, and it means whatever comes before the pipe is passed to whatever comes after the pipe. Often we need to log-transform using log(), square root transform using sqrt(), or something similar, and mutate() lets us easily add that data to the data frame. RESIDUALS (MODEL "ERROR") o The actual value of the data point is the predicted value plus the residual: DATA = PREDICTED VALUE + RESIDUAL o Therefore the residual is the difference between the data point and the predicted value: RESIDUALS = DATA - PREDICTED VALUE 20 + + Countries These residuals - the spread of the response variable data around the predicted values - characterise the ERROR of the model. This is what determines whether a relationship between two variables is STATISTICALLY SIGNIFICANT or not. RESIDUALS ARE THEREFORE VERY IMPORTANT! 10 +++ 0 + 2 4 6 Continents A negative residual is linked to a data point that's below the predicted value. A positive residual is a data point that's above the predicted value. The residuals determine the error variance, which is the variation in the data that the predicted values can't explain. This is the sum of the squared residuals, divided by the number of data points: : 02 = EN (3-4) 2 N 02 is the error variance, N is the number of data points, y; are the response (y) variable data points, IL; are the predicted values. This is the same as calculation of the overall variance in the response (y) variable, which you should be familiar with, except that we use the predicted values rather than the mean of