Different objects are used and show up in differnt contexts.
We’ll use the following data to explore R objects.
Motulsky 2nd Ed, Chapter 30, page 220, Table 30.1. Maximal relaxaction of muscle strips of old and young rat bladders stimualted w/ high concentrations of nonrepinephrine (Frazier et al 2006). Response variable is %E.max
The code above has made two objects. We can use several commands to learn about these objects.
What is our “old.E.max” object?
is(old.E.max)
## [1] "numeric" "vector"
Its a vector, containing numeric data.
What if we made a vector like this?
cat.variables <- c("old","old","old","old","old","old","old","old","old")
And used is()
is(cat.variables)
## [1] "character" "vector" "data.frameRowLabels"
## [4] "SuperClassMethod"
It tells us we have a vector, containing chracter data. Not sure why it feels the need to tell us all the other stuff…
Our vector has 9 elements, or is 9 elements long.
length(old.E.max)
## [1] 9
Note that dim(), for dimension, doens’t work with vector
dim(old.E.max)
## NULL
It would be nice if it said somethign like “1 x 9” for 1 row tall and 9 elements long. So it goes.
str() stands for “structure”.
It summarizes info about an object; I find it most useful for looking at lists.
* If our vector here was really really long, str() would only show the first part of the vector
str(old.E.max)
## num [1:9] 20.8 2.8 50 33.3 29.4 38.9 29.4 52.6 14.3
Use c() to combine two objects
old.plus.new <- c(old.E.max, young.E.max)
Look at the legnth
length(old.plus.new)
## [1] 17
Note that str() just shows us the first few digits, not all 17
str(old.plus.new)
## num [1:17] 20.8 2.8 50 33.3 29.4 38.9 29.4 52.6 14.3 45.5 ...
We can run a t.test like this, and the output goes to R’s “console”
t.test(old.E.max, young.E.max)
##
## Welch Two Sample t-test
##
## data: old.E.max and young.E.max
## t = -3.6242, df = 13.778, p-value = 0.002828
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -37.501081 -9.590586
## sample estimates:
## mean of x mean of y
## 30.16667 53.71250
We can save the output to an object using the assignment operator
rat.t.test <- t.test(old.E.max, young.E.max)
Then call up the output like this
rat.t.test
##
## Welch Two Sample t-test
##
## data: old.E.max and young.E.max
## t = -3.6242, df = 13.778, p-value = 0.002828
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -37.501081 -9.590586
## sample estimates:
## mean of x mean of y
## 30.16667 53.71250
Let’s use is() and str() to see what this rat.t.test object really is
is(rat.t.test)
## [1] "htest"
Let’s use str()
str(rat.t.test)
## List of 9
## $ statistic : Named num -3.62
## ..- attr(*, "names")= chr "t"
## $ parameter : Named num 13.8
## ..- attr(*, "names")= chr "df"
## $ p.value : num 0.00283
## $ conf.int : atomic [1:2] -37.5 -9.59
## ..- attr(*, "conf.level")= num 0.95
## $ estimate : Named num [1:2] 30.2 53.7
## ..- attr(*, "names")= chr [1:2] "mean of x" "mean of y"
## $ null.value : Named num 0
## ..- attr(*, "names")= chr "difference in means"
## $ alternative: chr "two.sided"
## $ method : chr "Welch Two Sample t-test"
## $ data.name : chr "old.E.max and young.E.max"
## - attr(*, "class")= chr "htest"
If I run just the name of the object, I get all of the output of the list
rat.t.test
##
## Welch Two Sample t-test
##
## data: old.E.max and young.E.max
## t = -3.6242, df = 13.778, p-value = 0.002828
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -37.501081 -9.590586
## sample estimates:
## mean of x mean of y
## 30.16667 53.71250
Say I want to just see the p-value. I can use the dollar sign like this
rat.t.test$p.value
## [1] 0.002828427
What about the “test statisic?”
rat.t.test$statistic
## t
## -3.624246
The means of each group (old vs yound)
rat.t.test$estimate
## mean of x mean of y
## 30.16667 53.71250
Try to get the confidence intervals (“confint”; answer below) …
#Type your attempt here:
Confidence interval from t.test object:
rat.t.test$conf.int
## [1] -37.501081 -9.590586
## attr(,"conf.level")
## [1] 0.95
Do you know what this confidence interval means in this context?
30.16667 - 53.71250
## [1] -23.54583
Or like this
mean(old.E.max) - mean(young.E.max)
## [1] -23.54583
Challenge: can you save the output from the mean() commands to objects, then do math on those objects? Answer below…
#Type your attempt here:
Make objects holding the means
#old
old.mean <- mean(old.E.max)
#young
young.mean <- mean(young.E.max)
Subtract them
old.mean - young.mean
## [1] -23.54583
Instead of calcuating the mean and their difference by hands let’s get the info from the t.test output.
First, get the means. Recall that for some reason they are called the “estimate” Answer below…
#Type your attempt here:
rat.t.test$estimate
## mean of x mean of y
## 30.16667 53.71250
Save this to an object
rat.means <- rat.t.test$estimate
Take a look with length(), is() and str()
#what is this object?
is(rat.means)
## [1] "numeric" "vector"
#how long is the vector?
length(rat.means)
## [1] 2
#take a peek at it
str(rat.means)
## Named num [1:2] 30.2 53.7
## - attr(*, "names")= chr [1:2] "mean of x" "mean of y"
Aside: This vector also happens to have labels attached to it, which we can access with the names() command
names(rat.means)
## [1] "mean of x" "mean of y"
Now let’s use this object to calcualte the difference between the means.
The first element
rat.means[1]
## mean of x
## 30.16667
The 2nd element
rat.means[2]
## mean of y
## 53.7125
Note that it gets angry if we try to access the 5th elemetn
rat.means[5]
## <NA>
## NA
Can you figure out how to calcualte the difference using our rat.means objects and square brackets?
#Type your answer here:
rat.means[1] - rat.means[2]
## mean of x
## -23.54583
Let’s save that to an object
diff.means <- rat.means[1] - rat.means[2]
How would we take the results of this t.test and plot it?
We already have our difference between means
diff.means
## mean of x
## -23.54583
Let’s make an object with the confidence interval
ci <- c(-37.501081,-9.590586)
We can load the barplot2() function from gplots() and plot the data.
* Use library(gplots) to load the package * you can get so called “help” for barplot2 using ?barplot2 * we want to use the height =.., plot.ci = TRUE, ci.l=, and ci.u= “arguements” of the function
A very very ugly plot of just the mean
library(gplots)
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
barplot2(height = diff.means)
Try to add the rest of the info. Hint: to get the lower ci access the 1st elemtn of our confidence itnerval object, ci, using square brackets
#Your attempt:
barplot2(height = diff.means,
plot.ci = TRUE,
ci.l=ci[1],
ci.u = ci[2])
Ok, this is pretty ugly but its technically correct.
plotCI(x = diff.means,
li=ci[1], #use "li", not "ci.l"!
ui = ci[2])
plotCI(x = diff.means,
li=ci[1], #use "li", not "ci.l"!!!!
ui = ci[2],
ylim = c(-40,5)) #y limits
Note that we can use a vector here with the ylim = arguement instead of nubmers.
#make a vector containing the plotting limits
ylimits <- c(-40,0)
#plot
plotCI(x = diff.means,
li=ci[1],
ui = ci[2],
ylim = ylimits) #use vector of values for limits
Make a plot with line
#make the plot
plotCI(x = diff.means,
li=ci[1],
ui = ci[2],
ylim = ylimits)
#add the line
abline(h = 0)
To change the colors of thigns we can use the col = arguement within abline. col = 2 makes a red line Try it
# Your attempt
Dot plot with red line at zero
#make the plot
plotCI(x = diff.means,
li=ci[1],
ui = ci[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2)
#Try it
Answer:
#make the plot
plotCI(x = diff.means,
li=ci[1],
ui = ci[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2, lty = 2)
Recall all of this stuff is in a list from the t.test
rat.t.test
##
## Welch Two Sample t-test
##
## data: old.E.max and young.E.max
## t = -3.6242, df = 13.778, p-value = 0.002828
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -37.501081 -9.590586
## sample estimates:
## mean of x mean of y
## 30.16667 53.71250
We can acceess components of lists using dollar signs $
rat.t.test$conf.int
## [1] -37.501081 -9.590586
## attr(,"conf.level")
## [1] 0.95
What if we want jsut the first confidence limt? We can combine using a dollar sign and brackets
rat.t.test$conf.int[1]
## [1] -37.50108
<br.
We can therefore access everything directly from out t.test output when we plot. First, let’s use our diff.means for the mean difference and access the confidence intervals directly
#make the plot
plotCI(x = diff.means,
li= rat.t.test$conf.int[1],
ui = rat.t.test$conf.int[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2, lty = 2)
This will get a big ugly, but let’s even get our mean difference from the t.test object. We’ll do this by using rat.t.test\(estimate[1] - rat.t.test\)estimate[2]
#make the plot
plotCI(x = rat.t.test$estimate[1] -
rat.t.test$estimate[2],
li= rat.t.test$conf.int[1],
ui = rat.t.test$conf.int[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2, lty = 2)
#make the plot
plotCI(x = rat.t.test$estimate[1] -
rat.t.test$estimate[2],
li= rat.t.test$conf.int[1],
ui = rat.t.test$conf.int[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2, lty = 2)
# ADD THE TEXT
text(x = 0.627, #x coordiante
y = -2, #y coord
"p = 0.002") #text: the p values
Since we know how to access stuff from R objects we can do this also be
#make the plot
plotCI(x = rat.t.test$estimate[1] -
rat.t.test$estimate[2],
li= rat.t.test$conf.int[1],
ui = rat.t.test$conf.int[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2, lty = 2)
# ADD THE TEXT
text(x = 0.627, #x coordiante
y = -2, #y coord
rat.t.test$p.value) #text: the p values
Note that its plotted off center.
* This is annoying deafult of text(). * this is fixed using pos = 4 to plot to the right of the coordinates you define
#make the plot
plotCI(x = rat.t.test$estimate[1] -
rat.t.test$estimate[2],
li= rat.t.test$conf.int[1],
ui = rat.t.test$conf.int[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2, lty = 2)
# ADD THE TEXT
text(x = 0.627, #x coordiante
y = -2, #y coord
pos = 4,
rat.t.test$p.value) #text: the p values
Now maybe we should round this using the round() command * round(…, 3) will rond to 3 decimal places
#make the plot
plotCI(x = rat.t.test$estimate[1] -
rat.t.test$estimate[2],
li= rat.t.test$conf.int[1],
ui = rat.t.test$conf.int[2],
ylim = ylimits)
#add the line
abline(h = 0, col = 2, lty = 2)
# ADD THE TEXT
text(x = 0.627, #x coordiante
y = -2, #y coord
pos = 4,
round(rat.t.test$p.value,3)) #text: the p values
We can…