############################################### ## Author: Joshua M. Tebbs ## Date: 27 July 2016 ## Update: 15 March 2024 ## STAT 110 course notes: R Code Chapter 12 ############################################### # Example 12.1 # Page 102 # TTF data # Enter data TTF = c(0.8,7.5,13.4,1.4,0.5,68.9,16.1,20.4,15.6,4.2,2.4,8.2,5.3,14.0) # Sort data from low to high sort(TTF) median(TTF) # median quantile(TTF,type=2) # 5-number summary # Make boxplot of the data (Figure 12.1) boxplot(TTF,range=0,xlab="",ylab="Time until treatment failure (in months)",col="lightblue") # Boxplot with outlier identified (Figure 12.2) boxplot(TTF,range=1.5,xlab="",ylab="Time until treatment failure (in months)",col="lightblue") # Example 12.2 # Page 107 # Arsenic data # Enter data arsenic = c(17.6,10.4,13.5,4,19.9,16,12,12.2,11.4,12.7,3,10.3,21.4,19.4,9,6.5,10.1,8.7,9.7,6.4, 9.7,63,15.5,10.7,18.2,7.5,6.1,6.7,6.9,0.8,73.5,12,28,12.6,9.4,6.2,15.3,7.3,10.7,15.9, 5.8,1,8.6,1.3,13.7,2.8,2.4,1.4,2.9,13.1,15.3,9.2,11.7,4.5,1,1.2,0.8,1,2.4,4.4,2.2,2.9, 3.6,2.5,1.8,5.9,2.8,1.7,4.6,5.4,3,3.1,1.3,2.6,1.4,2.3,1,5.4,1.8,2.6,3.4,1.4,10.7,18.2, 7.7,6.5,12.2,10.1,6.4,10.7,6.1,0.8,12,28.1,9.4,6.2,7.3,9.7,62.1,15.5,6.4,9.5) # Histogram (Figure 12.3, left) bins = seq(0,80,5) hist(arsenic,breaks = bins,xlab="Arsenic concentration (pp billion)",ylab="Count",main="",col="lightblue") # Boxplot (Figure 12.3, right) boxplot(arsenic,range=0,xlab="",ylab="Arsenic concentration (pp billion)",col="lightblue") quantile(arsenic,type=2) # 5-number summary Example 12.3 # Page 108 # SSHA score data # Enter data female = c(154,109,137,115,152,140,154,178,101,103,126,126,137,165,165,129,200,148) male = c(108,140,114,91,180,115,126,92,169,146,109,132,75,88,113,151,70,115,187,104) # Make boxplot of the data (Figure 12.4) # You do not need to specify "range=1.5" option (this is the default value) boxplot(female,male,xlab="",names=c("Female","Male"),ylab="SSHA score",col="lightblue") # Figure 12.5 # Page 110 # Side-by-side boxplots of birth weight by nutrition type # Read in data nec = read.csv(file = "C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Collaboration\\Christina Piro\\Data\\Caffeine_data_updated_062811_CLEAN.csv", header=T) # Extract birthweights according to which nutrition was used breastmilk = nec$Birth.weight[which(nec$Nutrition==1)] fluids = nec$Birth.weight[which(nec$Nutrition==2)] TPN = nec$Birth.weight[which(nec$Nutrition==3)] formula = nec$Birth.weight[which(nec$Nutrition==4)] # Make boxplot of the data boxplot(breastmilk,fluids,TPN,formula,xlab="",names=c("Breastmilk","Fluids","TPN","Formula"),ylab="Birth weight (in grams)",col="lightblue") # Example 12.4 # Page 111 # Clay model data # Enter data indentation = c(22.6,25.9,34.9,35.6,45.4,46.5,47.7,49.4,50.7,51.3) # Histogram (Figure 12.6) hist(indentation,xlab="Deepest indentation (mm)",ylab="Count",main="",col="lightblue") points(x=mean(indentation),y=0,pch=19,cex=1.5) # "Dotchart" (Figure 12.7) dotchart(indentation,xlab="Deepest indentation (mm)",ylab="Individual",lcolor="black") abline(v=41,lty=2) mean(indentation) # mean sd(indentation) # standard devation # Example 12.5 # Page 114 # Mice data # Enter data conventional = c(159,189,191,198,200,207,220,235,245,250,256,261,265,266,280,343,356,383,403,414,428, 432,317,318,399,495,525,536,549,552,554,337,558,571,586,594,596,605,612,621,628,631,636,643,647, 648,649,661,663,666,670,695,697,700,705,712,713,738,748,753,40,42,51,62,163,179,206,222,228,252, 249,282,324,333,341,366,385,407,420,431,441,462,482,517,517,524,564,567,586,619,620,621,622,647, 651,686,761,763,461) germ.free = c(158,192,193,194,195,202,212,215,229,230,237,240,244,247,259,300,301,321,337,415, 434,444,485,496,529,537,624,707,800,430,590,606,638,655,679,691,693,696,747,752,760,778, 821,986,136,246,255,376,421,565,516,517,652,655,658,660,662,675,681,734,736,737,757,769, 777,800,807,825,855,857,864,868,870,870,873,882,895,910,934,942,1015,1019) # Boxplots (Figure 12.8) boxplot(conventional,germ.free,xlab="",names=c("Conventional","Germ-free"),ylab="Time to death (in days)",col="lightblue") # Calculate mean and standard devation for each group mean(conventional) mean(germ.free) sd(conventional) sd(germ.free) # Figure 12.9 # Page 118 # This is the same figure as above in Example 12.2 (Figure 12.3, left)