In this lab we will:
t.test() and BinomCI()NOTE: as we discussed in lectures, depending on the TYPE of variable you have (categorical vs. numerical) there are different calculations for confidence intervals. Below is some introductory/sample code for determining CIs for different variable types:
For either data type, when asked for “desired confidence” this is going to be a value from 0-1, if I ask for 90% confidence, you need to enter 0.9
FOR CATEGORICAL (this means you are essentially creating proportional data): ** Install and load the package in the code chunk below
library(DescTools)
FOR ALL THE CODE BELOW, things have been commented out, if you copy and paste them, please make sure the # is removed. ### Condfidence Interval for Proportions
You will need the number of cases that are positive (x) from your data set as well as the total number of observations (n)
Access number of positive cases
#table(dataframe_name$categorical_column)
Access Total number of cases
#length(dataframe_name$categorical_column)
NOTE: there are two common methods for calculating CIs for proportions, the Wald method and the Agresti-Coull method, the only difference is a slight change in the calculation. The Wald method is a good default, Agresti-Coull is better for smaller sample sizes.
Confidence Interval - Wald Method
#BinomCI(x = number of positive cases, n = total cases, conf.level = desired confidence, method = "wald")
Condfidence Interval - Agresti-Coull Method
#BinomCI(x = number of positive cases, n = total cases, conf.level = desired confidence, method = "agresti-coull")
#t.test(dataframe_name$quantitative_column, conf.level = desired confidence)
Or directly access the Confidence Interval
#t.test(dataframe_name$quantitative_column, conf.level = desired confidence)$conf.int
In a study involving how rice grows across different nutrient treatments, researchers randomly selected plots where there was a mix of wild-type and gmo rice growing. Please bring in the “rice.csv” file and call it “rice_df”
# place your code here.
rice_df <- read.csv("rice.csv", header = TRUE)
Using the code chunk below, write R commands to
# place your code here
str(rice_df)
## 'data.frame': 72 obs. of 7 variables:
## $ PlantNo : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Block : int 1 1 1 1 1 1 2 2 2 2 ...
## $ RootDryMass : int 56 66 40 43 55 66 41 67 40 35 ...
## $ ShootDryMass: int 132 120 108 134 119 125 98 122 114 82 ...
## $ trt : chr "F10" "F10" "F10" "F10" ...
## $ fert : chr "F10" "F10" "F10" "F10" ...
## $ variety : chr "wt" "wt" "wt" "wt" ...
names(rice_df)
## [1] "PlantNo" "Block" "RootDryMass" "ShootDryMass" "trt"
## [6] "fert" "variety"
variety variable, which
confidence interval would be appropriate to use: C.I. for a mean or a
C.I. for a proportion?# place your code here.
table(rice_df$variety)
##
## gmo wt
## 36 36
length(rice_df$variety)
## [1] 72
BinomCI(x = 36, n = 72, conf.level = 0.97, method = "wald")
## est lwr.ci upr.ci
## [1,] 0.5 0.3721262 0.6278738
ShootDryMass variable,
which confidence interval would be appropriate to use: C.I. for a mean
or a C.I. for a proportion?# place your code here.
t.test(rice_df$ShootDryMass, conf.level = 0.97)
##
## One Sample t-test
##
## data: rice_df$ShootDryMass
## t = 13.971, df = 71, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 0
## 97 percent confidence interval:
## 50.11550 68.99561
## sample estimates:
## mean of x
## 59.55556
In this final section, we will continue to work with the
rice_df. We are now interested in whether the variety of
rice (wt or gmo) influences the growth of the rice plants.
Recall that the summary() function will compute the
five-number summary for a quantitative data set. Using
tapply() compute the “summary” of the ShootDryMass for each
plant broken up by variety:
#place your code here
tapply(rice_df$ShootDryMass, rice_df$variety, summary)
## $gmo
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.00 9.50 40.00 41.81 62.75 100.00
##
## $wt
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 26.00 47.50 76.00 77.31 108.25 134.00
varietyUsing the code chunk below, slice your rice_df into two
dataframes: wt and gmo. The wt
dataframe should include the ShootDryMass data for only the wild-type in
the sample, and the gmo dataframe should do the same for
gmo rice
The basic way to do this is to utilize the following steps, notice the first step highlights rows in your dataframe that contain a value your are looking to isolate, you need to look for the exact phrase! The second step takes your isolated rows and creates a new dataframe (which you control the name of)
### create a variable for desired rows
#desired_rows <- dataframe_name$categorical_column_name == "desired_level"
### use the desired rows to create a new dataframe
#new_dataframe_name <- dataframe_name[desired_rows,]
### I have done created a dataframe for wild-type rice below, you need to do the same for the gmo rice
wt_rows <- rice_df$variety == "wt"
wt_df <- rice_df[wt_rows,]
gmo_rows <- rice_df$variety == "gmo"
gmo_df <- rice_df[gmo_rows,]
dim(wt_df)
## [1] 36 7
dim(gmo_df)
## [1] 36 7
Lastly, using the code chunk below, compute a confidence interval for the ShootDryMass of wt, and then compute another for the ShootDryMass of gmo Use a confidence level of 96% in both cases.
t.test(wt_df$ShootDryMass, conf.level = 0.96)$conf.int
## [1] 65.60515 89.00596
## attr(,"conf.level")
## [1] 0.96
t.test(gmo_df$ShootDryMass, conf.level = 0.96)$conf.int
## [1] 31.00591 52.60520
## attr(,"conf.level")
## [1] 0.96
Answer the following:
65.605 - 89.006
31.006 - 52.605
There is no overlap
Based on the lack of overlap between the confidence intervals, I would say that, yes, the mean ShootDryMass is different for wt vs. gmo. The complete lack of overlap suggests a different mean, therefore we fail to reject the null hypothesis.