Introduction to the R package n.net



BiostatisticsWorksheet 1: Data exploration and plotting in R Data exploration should encourage you to be thorough with your analysis and spend time getting to know the data. You are advised to keep a note of your code (save the file in the top left hand window of RStudio and keep all of your clean commented code here). You may find it useful to save some of the commands you use here for later exercises. The aim of this exercise is to visualise and explore the various relationships between the variables in the dataset.Mammal sleep data:This dataset consists of sleep times and weights of mammals. name. common namegenus.vore. carnivore, omnivore or herbivore?order.conservation. the conservation status of the animalsleep_total. total amount of sleep, in hourssleep_rem. rem sleep, in hourssleep_cycle. length of sleep cycle, in hoursawake. amount of time spent awake, in hoursbrainwt. brain weight in kilogramsbodywt. body weight in kilogramsThe data are in the csv file called 'msleep.csv' in the weekly resources section. You will need to download and save this file to a folder on your computer. You must ensure this is the folder (or working directory) that R is pointing to. To check type getwd()to change this location you can use the tab ‘Session >Set working directory>Choose Directory’ to navigate to the correct folder. Alternatively you can type in the filepath using:setwd(“Z: my documents/ rdata/teaching”)change the text in parentheses to your file path.R will only be able to load data from the current working directoryCopy the R code and comments below into RStudio and work through the code to explore the dataset.If you are already familiar with R you may wish to use the ggplot2 package ( ) to explore more tools for plotting data.~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#load the data on mammal sleep patterns.msleep<- read.csv("msleep.csv")# to see what the column headings are called use the function namesnames(msleep)#look at the top 6 rows of the data with the function headhead(msleep)?#you may like to see a table summary of the data to see where there are missing #values (NA)summary(msleep)# plot sctterplots of the continuous variablesplot(msleep$awake)plot(msleep$brainwt)# note that two individuals have very high brain weights. You can continue the code here copying the syntax above for the other columns.# body and brain weight may benefit from a log transformation stored it in the dataframe as an new column# called lbodywt and lbrainwtmsleep$lbodywt<-log(msleep$bodywt)msleep$lbrainwt<-log(msleep$brainwt)head(msleep)plot(msleep$lbodywt)plot(msleep$lbrainwt)?# plot the same continuous variables with box plots.boxplot(msleep$awake)boxplot(msleep$lbrainwt)boxplot(msleep$lbodywt)?# are there any outliers in the data set??#use histograms to look at the distribution of the variableshist(msleep$awake)hist(msleep$lbrainwt)hist(msleep$lbodywt)?#you can adjust the number of bins by adjusting the 'breaks' argumenthist(msleep$lbodywt, breaks =10)?# plot the variables against each other to look for relationships, e.g. colinearityplot(msleep$lbrainwt, msleep$lbodywt)?# and against factors to look for differences.plot(msleep$vore,msleep$awake)# repeat this for some of the other variables.?# create a bar plot, you need to create a table of counts first.counts<- table (msleep$vore)barplot(counts)??#use the pairs function to plot all the continuous variables against each other.pairs (msleep[7:14])Take some time to explore the data set further and improve your R skills. Can you add a title to a plot? Now you can use the code you have learnt to explore a second data set. Tooth Growth Data Here the response variable is the length of odontoblasts (teeth) in each of 10 guinea pigs at each of three dose levels of Vitamin C (0.5, 1, and 2 mg) with each of two delivery methods (orange juice or ascorbic acid).[,1] len numeric Tooth length[,2] supp factor Supplement type (VC or OJ).[,3] dose numeric Dose in milligrams. This data is in the Weekly resources section and is called ToothGrowth.csv Download and save the data as before and write your own code to load and explore (summarise and plot) the data. ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download