Unlike the last chapter, where I buried you in stuff, there is not a lot of code in Chapter 4. I will cover what is there and add some additional stuff that I hope you will find useful. The first set of data came from the study by Katz et al. (1990) on performance on a test where the passage that the questions refered to was not read by the participant. The code is below. This code shows how to calculate means, trimmed means, and medians.
NumCorrect <- c(54, 52, 51, 50, 36, 55, 44, 46, 57, 44, 43, 52, 38, 46, 55, 34, 44, 39, 43, 36, 55, 57, 36, 46, 49, 46, 49, 47) xbar <- mean(NumCorrect) xbar.trim <- mean(NumCorrect, trim = .10) med <- median(NumCorrect) # In the next line the "\n" causes the printing to enter a line break # It always comes within a section in quotation marks. cat("The mean is = ", xbar,"\nThe 10% trimmed mean is ", xbar.trim, "\nThe median is = ", med) hist(NumCorrect, main = "Number of Items Correct", breaks = 10, col = "green") # For a more complete description of a variable or data frame # install.packages("psych") # You have probably already installed this package. library(psych) describe(NumCorrect) # ________________________________________________________ The mean is = 46.57143 The 10% trimmed mean is 46.66667 The median is = 46 vars n mean sd median trimmed mad min max range skew kurtosis se 1 28 46.57 6.83 46 46.67 8.15 34 57 23 -0.2 -1.1 1.29
In the section in Chapter 4 on "Seeing Statistics" I collected data over six trials of the color matching study. These data were then stored in a file named Tab4-2.dat. I read those data in and played with them in a number of ways. I think that you can figure that out. Then I went back to the Katz data and did some more stuff that you should look at. Notice that the "prettyR" library allows us to calculate the mode, but only as long as there is only one mode. (The code near the end of the Chapter 3 page shows a more efficient way.
dch:bright.match <- read.table("http://www.uvm.edu/~dhowell/fundamentals9/DataFiles/Tab4-2.dat", header = TRUE) names(bright.match) # To find out what the variables were named. ### Now break the data down into subgroups based on background. attach(bright.match) ### First make the independent variables into factors Stimulus <- factor(Stimulus) Background <- factor(Background) Replication <- factor(Replication) ### The following line says to combine the Difference scores whenever the Stimulus is ### equal to 2, 5, or 6 Lighter <- c(Difference[Stimulus == 2], Difference[Stimulus == 5], Difference[Stimulus == 6]) Darker <- c(Difference[Stimulus == 1], Difference[Stimulus == 4], Difference[Stimulus == 8]) Equal <- c(Difference[Stimulus == 3], Difference[Stimulus == 7], Difference[Stimulus == 9]) mean(Lighter); mean(Darker); mean(Equal) # install.packages("psych") library("psych") describe(Difference) ### tapply is a handy function. I can get the mean ### difference score broken down by Background tapply(Difference, Background, mean) ### Now for some graphics ### A boxplot--nicely illustrates the effect plot(Difference ~ Background) ### The "prettyR" library offers us a number of extra things, including "Mode." #install.packages("prettyR") library("prettyR") describe(NumCorrect) theMode <- Mode(NumCorrect) cat("The mode is = ", theMode, "\n") breaks <- seq(33.75, 59.75, 2.5) # Took forever to get the breaks right. hist(NumCorrect, breaks = breaks) # Note slight differences in displays. # ____________________________________________________________________________