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
install.packages("DescTools")
trying URL 'https://cran.rstudio.com/bin/macosx/big-sur-arm64/contrib/4.4/DescTools_0.99.60.tgz'
Content type 'application/x-gzip' length 6403924 bytes (6.1 MB)
==================================================
downloaded 6.1 MB
The downloaded binary packages are in
/var/folders/f1/3b7bq38n3pvf0chk1f7c2xbr0000gn/T//RtmpmRl0dr/downloaded_packages
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
rice <- read.csv('~/Desktop/BIN510-files/rice.csv')
names(rice)
[1] "PlantNo" "Block" "RootDryMass" "ShootDryMass" "trt" "fert"
[7] "variety"
head(rice)
table(rice$variety)
gmo wt
36 36
Access Total number of cases
length(rice$variety)
[1] 72
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 = 36, n = 72, conf.level = 0.95, method = "wald")
est lwr.ci upr.ci
[1,] 0.5 0.384508 0.615492
Condfidence Interval - Agresti-Coull Method
BinomCI(x = 36, n = 72, conf.level = 0.95, method = "agresti-coull")
est lwr.ci upr.ci
[1,] 0.5 0.3874709 0.6125291
t.test(rice$RootDryMass, conf.level = 0.95)
One Sample t-test
data: rice$RootDryMass
t = 9.6778, df = 71, p-value = 1.329e-14
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
14.34656 21.79233
sample estimates:
mean of x
18.06944
Or directly access the Confidence Interval
t.test(rice$RootDryMass, conf.level = 0.95)$conf.int
[1] 14.34656 21.79233
attr(,"conf.level")
[1] 0.95
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”
rice_df <- read.csv('~/Desktop/BIN510-files/rice.csv')
Using the code chunk below, write R commands to
names(rice_df)
[1] "PlantNo" "Block" "RootDryMass" "ShootDryMass" "trt" "fert"
[7] "variety"
head(rice_df)
table(rice_df$variety)
gmo wt
36 36
variety variable, which
confidence interval would be appropriate to use: C.I. for a mean or a
C.I. for a proportion?1.We would use CI for a proportion because it is a categorical variable. 2.See code below… 3.We are 97% confident that the proportion of GMO rice is between 37-62%.
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?1.Since ShootDryMass is a numerical variable we would want a CI for a mean. 2.See code below… 3.We are 97% confident that the mean of ShootDryMass is between 50 and 68.
t.test(rice_df$ShootDryMass, conf.level = 0.97)$conf.int
[1] 50.11550 68.99561
attr(,"conf.level")
[1] 0.97
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:
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,]
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:
1.The CI for WT is between 65-89. 2.The CI for the GMO is bewteen 31-52. 3.No these CIs do not overlap. 4.Since they do not overlap the means of ShootDryMass might be different between the WT and GMO rice. The mean of WT is higher than that of the GMO.