HM4 Coding

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

HM4

Due date:April 26th, 2020

Coding Howework (submmite the pdf file to Canvas)

Here we are going to test a couple of hypotheses about the Old Faithful data in R. Remember, this is the
faithful data frame that is built in to R. You can use data(faithful) to load data set. First split faithful
into two separate data frames: (1) those entries with eruption times less than 3 minutes (eruptions < 3)
and (2) those entries with eruption times greater than or equal to 3 minutes (eruptions >= 3). Answer the
following about the entry wait time (waiting):
(a). For the entries with short eruption times, you want to test the hypothesis that the associated waiting
last on average less than 60 minutes. What is the null hypothesis? What is the alternative hypothesis?
(Write your own code)
(b). Give R commands to compute the statistic that you used in (a) and the resulting p-value. What values
did you get? Would you reject the null hypothesis at the α = 0.05 level?
(c). For the entries with long eruption times, you want to test the hypothesis that the associated waiting time
last on average shorter than 80 minutes. What is the null hypothesis? What is the alternative hypothesis?
(Write your own code)
(d). Give R commands to compute the statistic you used in (c) and the resulting p-value to test the hypothesis
you came up with in part (c).What values did you get? Would you reject the null hypothesis at the α = 0.05
level?

data(faithful)
short_eruption_times <- subset(faithful, eruptions < 3)
head(short_eruption_times)

## eruptions waiting
## 2 1.800 54
## 4 2.283 62
## 6 2.883 55
## 9 1.950 51
## 11 1.833 54
## 14 1.750 47

long_eruption_times <- subset(faithful, eruptions >= 3)


head(long_eruption_times)

## eruptions waiting
## 1 3.600 79
## 3 3.333 74
## 5 4.533 85
## 7 4.700 88
## 8 3.600 85
## 10 4.350 85

1
(a)

# input your r code here


# H0: \mu = 60
# H1: \mu < 60

(b)

# input your r code here


waiting_time_less_than_60mins <- subset(short_eruption_times, waiting < 60)
waiting_time_less_than_60mins

## eruptions waiting
## 2 1.800 54
## 6 2.883 55
## 9 1.950 51
## 11 1.833 54
## 14 1.750 47
## 16 2.167 52
## 19 1.600 52
## 21 1.800 51
## 22 1.750 47
## 27 1.967 55
## 36 2.017 52
## 37 1.867 48
## 39 1.833 59
## 42 1.883 58
## 44 1.750 58
## 48 2.100 53
## 50 2.000 59
## 53 1.833 54
## 55 1.733 54
## 61 2.233 59
## 63 1.750 48
## 72 1.967 56
## 89 2.167 48
## 93 1.867 50
## 99 1.867 51
## 103 2.100 49
## 106 1.867 47
## 108 1.783 52
## 112 2.300 59
## 115 1.700 59
## 117 2.317 50
## 119 1.817 59
## 121 2.617 53
## 124 1.967 56
## 127 1.917 45
## 129 2.267 55
## 131 1.867 45
## 133 2.800 56
## 135 1.833 46
## 137 1.883 51

2
## 139 2.033 53
## 146 1.983 59
## 148 2.017 49
## 150 1.800 53
## 159 1.800 53
## 161 2.200 45
## 163 2.000 58
## 169 1.933 52
## 171 1.917 49
## 172 2.083 57
## 178 2.417 50
## 181 1.883 55
## 185 2.033 51
## 188 1.833 46
## 190 2.183 55
## 192 1.833 57
## 199 2.250 51
## 204 1.867 53
## 206 1.783 46
## 209 1.933 49
## 213 1.867 49
## 217 2.400 53
## 219 2.000 55
## 221 1.867 50
## 223 1.750 54
## 232 2.417 54
## 234 2.217 50
## 236 1.883 54
## 237 1.850 54
## 242 2.350 47
## 247 2.083 57
## 251 2.200 54
## 259 2.000 56
## 263 1.850 58
## 265 1.983 43
## 269 2.150 46
## 271 1.817 46

waiting <- subset(waiting_time_less_than_60mins, select = "waiting")


summary(waiting)

## waiting
## Min. :43.00
## 1st Qu.:49.00
## Median :53.00
## Mean :52.31
## 3rd Qu.:55.00
## Max. :59.00

t.test(waiting, y = NULL,
alternative = c( "less"),
mu = 60, paired = FALSE, var.equal = FALSE,
conf.level = 0.95)

3
##
## One Sample t-test
##
## data: waiting
## t = -16.052, df = 76, p-value < 2.2e-16
## alternative hypothesis: true mean is less than 60
## 95 percent confidence interval:
## -Inf 53.10925
## sample estimates:
## mean of x
## 52.31169

#As the mean = 52.31 < 52.31169, we reject H0

(c)

# input your r code here


# H0: \mu = 80
# H1: \mu < 80

(d)

# input your r code here


waiting_time_shorter_than_80mins <- subset(short_eruption_times, waiting < 80)
waiting_time_shorter_than_80mins

## eruptions waiting
## 2 1.800 54
## 4 2.283 62
## 6 2.883 55
## 9 1.950 51
## 11 1.833 54
## 14 1.750 47
## 16 2.167 52
## 17 1.750 62
## 19 1.600 52
## 21 1.800 51
## 22 1.750 47
## 27 1.967 55
## 36 2.017 52
## 37 1.867 48
## 39 1.833 59
## 42 1.883 58
## 44 1.750 58
## 48 2.100 53
## 50 2.000 59
## 53 1.833 54
## 55 1.733 54
## 58 1.667 64
## 61 2.233 59
## 63 1.750 48
## 65 1.817 60

4
## 69 2.067 65
## 72 1.967 56
## 75 1.983 62
## 77 2.017 60
## 84 2.633 65
## 89 2.167 48
## 91 2.200 60
## 93 1.867 50
## 95 1.833 63
## 99 1.867 51
## 101 2.483 62
## 103 2.100 49
## 106 1.867 47
## 108 1.783 52
## 112 2.300 59
## 115 1.700 59
## 117 2.317 50
## 119 1.817 59
## 121 2.617 53
## 124 1.967 56
## 127 1.917 45
## 129 2.267 55
## 131 1.867 45
## 133 2.800 56
## 135 1.833 46
## 137 1.883 51
## 139 2.033 53
## 142 2.233 60
## 146 1.983 59
## 148 2.017 49
## 150 1.800 53
## 153 2.400 65
## 159 1.800 53
## 161 2.200 45
## 163 2.000 58
## 167 2.367 63
## 169 1.933 52
## 171 1.917 49
## 172 2.083 57
## 178 2.417 50
## 181 1.883 55
## 185 2.033 51
## 188 1.833 46
## 190 2.183 55
## 192 1.833 57
## 199 2.250 51
## 201 2.100 60
## 204 1.867 53
## 206 1.783 46
## 209 1.933 49
## 211 2.383 71
## 213 1.867 49
## 217 2.400 53
## 219 2.000 55

5
## 221 1.867 50
## 223 1.750 54
## 232 2.417 54
## 234 2.217 50
## 236 1.883 54
## 237 1.850 54
## 240 2.333 64
## 242 2.350 47
## 244 2.900 63
## 247 2.083 57
## 249 2.133 67
## 251 2.200 54
## 259 2.000 56
## 263 1.850 58
## 265 1.983 43
## 266 2.250 60
## 269 2.150 46
## 271 1.817 46

waiting <- subset(waiting_time_shorter_than_80mins, select = "waiting")


summary(waiting)

## waiting
## Min. :43.00
## 1st Qu.:50.00
## Median :54.00
## Mean :54.49
## 3rd Qu.:59.00
## Max. :71.00

t.test(waiting, y = NULL,
alternative = c( "less"),
mu = 80, paired = FALSE, var.equal = FALSE,
conf.level = 0.95)

##
## One Sample t-test
##
## data: waiting
## t = -43.012, df = 96, p-value < 2.2e-16
## alternative hypothesis: true mean is less than 80
## 95 percent confidence interval:
## -Inf 55.4797
## sample estimates:
## mean of x
## 54.49485

#As the mean = 54.49 < 54.49485, we reject H0

You might also like