sample.n <- 20 
sample.mean <- 11.5
sample.sd <- 3
sample.se <- sample.sd/sqrt(sample.n)

#confidence_interval = 0.95
#formula1
lower_bound <- sample.mean - (sample.se *1.96) 
upper_bound <- sample.mean + (sample.se *1.96) 
print(lower_bound)
## [1] 10.18519
print(upper_bound)
## [1] 12.81481
answer <- 1 - pt(1.96, df = sample.n - 1)
answer
## [1] 0.03241449
#The new teacher's teaching method is effective in improving math scores.

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

ToothGrowth
##     len supp dose
## 1   4.2   VC  0.5
## 2  11.5   VC  0.5
## 3   7.3   VC  0.5
## 4   5.8   VC  0.5
## 5   6.4   VC  0.5
## 6  10.0   VC  0.5
## 7  11.2   VC  0.5
## 8  11.2   VC  0.5
## 9   5.2   VC  0.5
## 10  7.0   VC  0.5
## 11 16.5   VC  1.0
## 12 16.5   VC  1.0
## 13 15.2   VC  1.0
## 14 17.3   VC  1.0
## 15 22.5   VC  1.0
## 16 17.3   VC  1.0
## 17 13.6   VC  1.0
## 18 14.5   VC  1.0
## 19 18.8   VC  1.0
## 20 15.5   VC  1.0
## 21 23.6   VC  2.0
## 22 18.5   VC  2.0
## 23 33.9   VC  2.0
## 24 25.5   VC  2.0
## 25 26.4   VC  2.0
## 26 32.5   VC  2.0
## 27 26.7   VC  2.0
## 28 21.5   VC  2.0
## 29 23.3   VC  2.0
## 30 29.5   VC  2.0
## 31 15.2   OJ  0.5
## 32 21.5   OJ  0.5
## 33 17.6   OJ  0.5
## 34  9.7   OJ  0.5
## 35 14.5   OJ  0.5
## 36 10.0   OJ  0.5
## 37  8.2   OJ  0.5
## 38  9.4   OJ  0.5
## 39 16.5   OJ  0.5
## 40  9.7   OJ  0.5
## 41 19.7   OJ  1.0
## 42 23.3   OJ  1.0
## 43 23.6   OJ  1.0
## 44 26.4   OJ  1.0
## 45 20.0   OJ  1.0
## 46 25.2   OJ  1.0
## 47 25.8   OJ  1.0
## 48 21.2   OJ  1.0
## 49 14.5   OJ  1.0
## 50 27.3   OJ  1.0
## 51 25.5   OJ  2.0
## 52 26.4   OJ  2.0
## 53 22.4   OJ  2.0
## 54 24.5   OJ  2.0
## 55 24.8   OJ  2.0
## 56 30.9   OJ  2.0
## 57 26.4   OJ  2.0
## 58 27.3   OJ  2.0
## 59 29.4   OJ  2.0
## 60 23.0   OJ  2.0
head(ToothGrowth)
##    len supp dose
## 1  4.2   VC  0.5
## 2 11.5   VC  0.5
## 3  7.3   VC  0.5
## 4  5.8   VC  0.5
## 5  6.4   VC  0.5
## 6 10.0   VC  0.5
str(ToothGrowth)
## 'data.frame':    60 obs. of  3 variables:
##  $ len : num  4.2 11.5 7.3 5.8 6.4 10 11.2 11.2 5.2 7 ...
##  $ supp: Factor w/ 2 levels "OJ","VC": 2 2 2 2 2 2 2 2 2 2 ...
##  $ dose: num  0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 0.5 ...
summary(ToothGrowth)
##       len        supp         dose      
##  Min.   : 4.20   OJ:30   Min.   :0.500  
##  1st Qu.:13.07   VC:30   1st Qu.:0.500  
##  Median :19.25           Median :1.000  
##  Mean   :18.81           Mean   :1.167  
##  3rd Qu.:25.27           3rd Qu.:2.000  
##  Max.   :33.90           Max.   :2.000

Including Plots

You can also embed plots, for example:

library(ggplot2)

# Boxplot of tooth length
ggplot(ToothGrowth, aes(y = len)) +
  geom_boxplot(fill = "skyblue") +
  labs(title = "Boxplot of Tooth Length (len)",
       y = "Tooth Length") +
  theme_minimal()

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

library(ggplot2)
# Histogram
ggplot(ToothGrowth, aes(x = len)) +
  geom_histogram(binwidth = 2, fill = "steelblue", color = "black") +
  labs(title = "Histogram of Tooth Length (len)",
       x = "Tooth Length",
       y = "Frequency") +
  theme_minimal()

#Null hypotheses (H0:The length of guinea pig teeth is not greater that or equal to 17.)
#Alternative hypothese (H1:The length of guinea pig teeth is greater than or equal to 17.)
#One-tailed test: because we want to specifically test whether the tooth length is less than 17, not just different from 17. 
#It should test the t-value because it is using a sample not the population and the population standard deviation is unknown.

# Data 
data <- ToothGrowth$len
sample.n <- length(data)
sample.mean <- mean(data)
sample.sd <- sd(data)
sample.se <- sample.sd/sqrt(sample.n)
pop_mean <- 17

# t-statistic
t_value <- (sample.mean - pop_mean) / (sample.sd / sqrt(sample.n))
#p-value(left-tailed test)
p_value <- pt(t_value, df = sample.n - 1)
# Output
t_value
## [1] 1.836245
p_value
## [1] 0.9643194
#The p-value should be lower than 0.05
#Since the p-value 0.96, we accept the null hypothesis.
#The length of guinea pig teeth is not greater than or equal to 17.