############################################### ## Author: Joshua M. Tebbs ## Date: 27 July 2016 ## Update: 15 March 2024 ## STAT 110 course notes: R Code Chapter 11 ############################################### # Figure 11.1 # Page 92 # Find data web site # Cut and paste data set into a notepad file # Save this file (e.g., birthweights.txt) to a folder on your laptop or desktop # Import the data from the folder # This creates a table (with each row corresponding to one infant) birth.weights = read.table("C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat110\\s24\\data\\birthweights.txt",header=TRUE) # This creates a univariate data set (ignoring infant case number) birth.weights = birth.weights[,1] # extracts data from the table # Make histogram of the data hist(birth.weights,xlab="Birth weight (in grams)",ylab="Count",main="",col="lightblue",ylim=c(0,120)) # Figure 11.2 # Page 93 # Birthweight data # Change interval (class/bin) widths # Figure 11.2 (left) # interval width = 100 grams bins = seq(0,6000,100) hist(birth.weights,breaks=bins,xlab="Birth weight (in grams)",ylab="Count",main="",col="lightblue") # Figure 11.2 (right) # interval width = 2000 grams bins = seq(0,6000,2000) hist(birth.weights,breaks=bins,xlab="Birth weight (in grams)",ylab="Count",main="",col="lightblue") # Figure 11.3 # Page 94 # Birthweight data # Make histogram of the data hist(birth.weights,prob=TRUE,xlab="Birth weight (in grams)",ylab="Density",main="",col="lightblue") # Add estimate of population density curve lines(density(birth.weights),col="red",lwd=2) # Figure 11.4 # Page 96 # Find data web site # Cut and paste data set into a notepad file # Save this file (e.g., bmi.txt) to a folder on your laptop or desktop # Import the data from the folder # This creates a table (with each row corresponding to one child) bmi = read.table("C:\\Users\\tebbs\\OneDrive - University of South Carolina\\Documents\\texfiles\\Classes\\USC\\stat110\\s24\\data\\bmi.txt",header=TRUE) bmi = bmi[,1] # Make histogram of the data (left) bins = seq(0,50,2.5) # select intervals and lengths hist(bmi,breaks=bins,xlab="Body mass index",ylab="Count",main="",col="lightblue",xlim=c(0,50),ylim=c(0,100)) # Add estimate of population density curve (right) hist(bmi,prob=TRUE,breaks=bins,xlab="Body mass index",ylab="Density",main="",col="lightblue",xlim=c(0,50),ylim=c(0,0.125)) lines(density(bmi),col="red",lwd=2) # Figure 11.5 # Page 97 # Construct histogram of IQ data (simulated) IQ = rnorm(2200,100,15) # Make histogram of the data (left) bins = seq(40,160,5) hist(IQ,breaks=bins,xlab="IQ score",ylab="Count",main="",col="lightblue") # Add estimate of population density curve (right) hist(IQ,prob=TRUE,breaks=bins,xlab="IQ score",ylab="Density",main="",col="lightblue") lines(sort(IQ),dnorm(sort(IQ),mean(IQ),sd(IQ)),col="red",lwd=2) # Figure 11.6 # Page 98 # Long jump distance data # Enter data distance = c(8.11,8.11,8.09,8.08,8.06,8.03,8.02,7.99,7.99,7.97,7.95,7.92,7.92,7.92,7.89, 7.87,7.84,7.79,7.79,7.77,7.76,7.72,7.71,7.66,7.62,7.61,7.59,7.55,7.53,7.50,7.50,7.42, 7.38,7.38,7.26,7.25,7.08,6.96,6.84,6.55) # Make histogram of the data bins = seq(6.0,8.6,0.2) hist(distance,breaks = bins, xlab="Distance (in meters)",ylab="Count",main="",col="lightblue") # Figure 11.7 # Page 99 # Old Faithful geyser data # These data are available in R data(faithful) faithful = faithful[,2] # Make histogram of the data hist(faithful,prob=TRUE,xlab="Time between eruptions (in minutes)",ylab="Density ",main="",col="lightblue") # Add estimate of population density curve lines(density(faithful),col="red",lwd=2) # Figure 11.8 # Page 101 # OSU final exam data # Enter data final.exam = c(95,98,93,91,95,90,90,96,98,89,93,92,88,79,91,83,85,90,81,87,79,87, 88,83,86,80,77,81,81,78,79,82,78,76,79,76,73,81,78,84,70,71,77,65,69,70,63,77,74, 82,69,74,67,44,63,70,57,63,51,52,22,57,47,54,52,76) # Make stem plot of the data (Page 100) stem(final.exam) # Make histogram of the data (Page 101) bins = seq(20,100,5) hist(final.exam,breaks=bins,xlab="Final exam score",ylab="Count",main="",col="lightblue") # Make stem plot of the data (Page 101) stem(final.exam,scale=2)