12.74

dan = read.csv("EX12-74DANDRUFF.csv")

dan
##     OBS Treatment Flaking
## 1     1      PyrI      17
## 2     2      PyrI      16
## 3     3      PyrI      18
## 4     4      PyrI      17
## 5     5      PyrI      18
## 6     6      PyrI      16
## 7     7      PyrI      17
## 8     8      PyrI      20
## 9     9      PyrI      17
## 10   10      PyrI      17
## 11   11      PyrI      15
## 12   12      PyrI      18
## 13   13      PyrI      17
## 14   14      PyrI      17
## 15   15      PyrI      17
## 16   16      PyrI      15
## 17   17      PyrI      17
## 18   18      PyrI      16
## 19   19      PyrI      20
## 20   20      PyrI      17
## 21   21      PyrI      17
## 22   22      PyrI      17
## 23   23      PyrI      18
## 24   24      PyrI      18
## 25   25      PyrI      17
## 26   26      PyrI      18
## 27   27      PyrI      18
## 28   28      PyrI      19
## 29   29      PyrI      16
## 30   30      PyrI      17
## 31   31      PyrI      18
## 32   32      PyrI      20
## 33   33      PyrI      19
## 34   34      PyrI      18
## 35   35      PyrI      19
## 36   36      PyrI      17
## 37   37      PyrI      19
## 38   38      PyrI      15
## 39   39      PyrI      19
## 40   40      PyrI      18
## 41   41      PyrI      19
## 42   42      PyrI      16
## 43   43      PyrI      19
## 44   44      PyrI      17
## 45   45      PyrI      18
## 46   46      PyrI      17
## 47   47      PyrI      16
## 48   48      PyrI      16
## 49   49      PyrI      17
## 50   50      PyrI      18
## 51   51      PyrI      17
## 52   52      PyrI      17
## 53   53      PyrI      15
## 54   54      PyrI      18
## 55   55      PyrI      19
## 56   56      PyrI      17
## 57   57      PyrI      18
## 58   58      PyrI      18
## 59   59      PyrI      18
## 60   60      PyrI      18
## 61   61      PyrI      18
## 62   62      PyrI      20
## 63   63      PyrI      16
## 64   64      PyrI      18
## 65   65      PyrI      19
## 66   66      PyrI      15
## 67   67      PyrI      18
## 68   68      PyrI      16
## 69   69      PyrI      16
## 70   70      PyrI      17
## 71   71      PyrI      19
## 72   72      PyrI      15
## 73   73      PyrI      17
## 74   74      PyrI      18
## 75   75      PyrI      17
## 76   76      PyrI      18
## 77   77      PyrI      18
## 78   78      PyrI      17
## 79   79      PyrI      18
## 80   80      PyrI      16
## 81   81      PyrI      17
## 82   82      PyrI      17
## 83   83      PyrI      17
## 84   84      PyrI      17
## 85   85      PyrI      17
## 86   86      PyrI      17
## 87   87      PyrI      17
## 88   88      PyrI      18
## 89   89      PyrI      18
## 90   90      PyrI      17
## 91   91      PyrI      17
## 92   92      PyrI      18
## 93   93      PyrI      18
## 94   94      PyrI      18
## 95   95      PyrI      18
## 96   96      PyrI      17
## 97   97      PyrI      17
## 98   98      PyrI      17
## 99   99      PyrI      17
## 100 100      PyrI      18
## 101 101      PyrI      16
## 102 102      PyrI      19
## 103 103      PyrI      19
## 104 104      PyrI      17
## 105 105      PyrI      19
## 106 106      PyrI      17
## 107 107      PyrI      15
## 108 108      PyrI      16
## 109 109      PyrI      18
## 110 110      PyrI      18
## 111 111      PyrI      17
## 112 112      PyrI      17
## 113 113     PyrII      17
## 114 114     PyrII      17
## 115 115     PyrII      18
## 116 116     PyrII      19
## 117 117     PyrII      16
## 118 118     PyrII      18
## 119 119     PyrII      18
## 120 120     PyrII      16
## 121 121     PyrII      18
## 122 122     PyrII      19
## 123 123     PyrII      19
## 124 124     PyrII      17
## 125 125     PyrII      19
## 126 126     PyrII      18
## 127 127     PyrII      17
## 128 128     PyrII      18
## 129 129     PyrII      16
## 130 130     PyrII      19
## 131 131     PyrII      16
## 132 132     PyrII      16
## 133 133     PyrII      19
## 134 134     PyrII      17
## 135 135     PyrII      19
## 136 136     PyrII      14
## 137 137     PyrII      17
## 138 138     PyrII      18
## 139 139     PyrII      17
## 140 140     PyrII      19
## 141 141     PyrII      18
## 142 142     PyrII      16
## 143 143     PyrII      16
## 144 144     PyrII      15
## 145 145     PyrII      16
## 146 146     PyrII      18
## 147 147     PyrII      16
## 148 148     PyrII      14
## 149 149     PyrII      18
## 150 150     PyrII      17
## 151 151     PyrII      17
## 152 152     PyrII      17
## 153 153     PyrII      16
## 154 154     PyrII      18
## 155 155     PyrII      19
## 156 156     PyrII      16
## 157 157     PyrII      16
## 158 158     PyrII      16
## 159 159     PyrII      17
## 160 160     PyrII      17
## 161 161     PyrII      19
## 162 162     PyrII      18
## 163 163     PyrII      19
## 164 164     PyrII      18
## 165 165     PyrII      18
## 166 166     PyrII      17
## 167 167     PyrII      16
## 168 168     PyrII      18
## 169 169     PyrII      19
## 170 170     PyrII      14
## 171 171     PyrII      16
## 172 172     PyrII      18
## 173 173     PyrII      16
## 174 174     PyrII      16
## 175 175     PyrII      15
## 176 176     PyrII      19
## 177 177     PyrII      17
## 178 178     PyrII      19
## 179 179     PyrII      17
## 180 180     PyrII      20
## 181 181     PyrII      16
## 182 182     PyrII      16
## 183 183     PyrII      17
## 184 184     PyrII      16
## 185 185     PyrII      16
## 186 186     PyrII      16
## 187 187     PyrII      16
## 188 188     PyrII      16
## 189 189     PyrII      18
## 190 190     PyrII      19
## 191 191     PyrII      17
## 192 192     PyrII      17
## 193 193     PyrII      15
## 194 194     PyrII      17
## 195 195     PyrII      19
## 196 196     PyrII      17
## 197 197     PyrII      20
## 198 198     PyrII      17
## 199 199     PyrII      19
## 200 200     PyrII      15
## 201 201     PyrII      17
## 202 202     PyrII      18
## 203 203     PyrII      16
## 204 204     PyrII      17
## 205 205     PyrII      18
## 206 206     PyrII      17
## 207 207     PyrII      16
## 208 208     PyrII      16
## 209 209     PyrII      17
## 210 210     PyrII      18
## 211 211     PyrII      18
## 212 212     PyrII      18
## 213 213     PyrII      16
## 214 214     PyrII      16
## 215 215     PyrII      18
## 216 216     PyrII      17
## 217 217     PyrII      21
## 218 218     PyrII      17
## 219 219     PyrII      19
## 220 220     PyrII      16
## 221 221     PyrII      17
## 222 222      Keto      17
## 223 223      Keto      17
## 224 224      Keto      17
## 225 225      Keto      15
## 226 226      Keto      14
## 227 227      Keto      16
## 228 228      Keto      16
## 229 229      Keto      17
## 230 230      Keto      15
## 231 231      Keto      15
## 232 232      Keto      16
## 233 233      Keto      17
## 234 234      Keto      17
## 235 235      Keto      16
## 236 236      Keto      16
## 237 237      Keto      15
## 238 238      Keto      16
## 239 239      Keto      17
## 240 240      Keto      16
## 241 241      Keto      15
## 242 242      Keto      18
## 243 243      Keto      17
## 244 244      Keto      16
## 245 245      Keto      17
## 246 246      Keto      15
## 247 247      Keto      15
## 248 248      Keto      16
## 249 249      Keto      15
## 250 250      Keto      18
## 251 251      Keto      16
## 252 252      Keto      16
## 253 253      Keto      18
## 254 254      Keto      16
## 255 255      Keto      16
## 256 256      Keto      17
## 257 257      Keto      16
## 258 258      Keto      15
## 259 259      Keto      15
## 260 260      Keto      16
## 261 261      Keto      17
## 262 262      Keto      16
## 263 263      Keto      18
## 264 264      Keto      18
## 265 265      Keto      16
## 266 266      Keto      17
## 267 267      Keto      15
## 268 268      Keto      15
## 269 269      Keto      16
## 270 270      Keto      16
## 271 271      Keto      14
## 272 272      Keto      15
## 273 273      Keto      17
## 274 274      Keto      16
## 275 275      Keto      15
## 276 276      Keto      17
## 277 277      Keto      16
## 278 278      Keto      16
## 279 279      Keto      15
## 280 280      Keto      16
## 281 281      Keto      14
## 282 282      Keto      17
## 283 283      Keto      16
## 284 284      Keto      16
## 285 285      Keto      15
## 286 286      Keto      16
## 287 287      Keto      16
## 288 288      Keto      16
## 289 289      Keto      16
## 290 290      Keto      17
## 291 291      Keto      15
## 292 292      Keto      16
## 293 293      Keto      16
## 294 294      Keto      16
## 295 295      Keto      15
## 296 296      Keto      17
## 297 297      Keto      16
## 298 298      Keto      17
## 299 299      Keto      16
## 300 300      Keto      16
## 301 301      Keto      16
## 302 302      Keto      16
## 303 303      Keto      18
## 304 304      Keto      15
## 305 305      Keto      17
## 306 306      Keto      16
## 307 307      Keto      15
## 308 308      Keto      15
## 309 309      Keto      15
## 310 310      Keto      16
## 311 311      Keto      17
## 312 312      Keto      15
## 313 313      Keto      15
## 314 314      Keto      14
## 315 315      Keto      16
## 316 316      Keto      17
## 317 317      Keto      16
## 318 318      Keto      16
## 319 319      Keto      15
## 320 320      Keto      17
## 321 321      Keto      17
## 322 322      Keto      17
## 323 323      Keto      16
## 324 324      Keto      16
## 325 325      Keto      17
## 326 326      Keto      15
## 327 327      Keto      16
## 328 328   Placebo      30
## 329 329   Placebo      29
## 330 330   Placebo      31
## 331 331   Placebo      31
## 332 332   Placebo      29
## 333 333   Placebo      29
## 334 334   Placebo      28
## 335 335   Placebo      29
## 336 336   Placebo      26
## 337 337   Placebo      29
## 338 338   Placebo      29
## 339 339   Placebo      28
## 340 340   Placebo      31
## 341 341   Placebo      30
## 342 342   Placebo      28
## 343 343   Placebo      28
## 344 344   Placebo      30
## 345 345   Placebo      31
## 346 346   Placebo      31
## 347 347   Placebo      31
## 348 348   Placebo      28
## 349 349   Placebo      32
## 350 350   Placebo      32
## 351 351   Placebo      29
## 352 352   Placebo      28
## 353 353   Placebo      26
## 354 354   Placebo      29
## 355 355   Placebo      31
head(dan)
##   OBS Treatment Flaking
## 1   1      PyrI      17
## 2   2      PyrI      16
## 3   3      PyrI      18
## 4   4      PyrI      17
## 5   5      PyrI      18
## 6   6      PyrI      16
res.aov_dan <- aov(Flaking ~ Treatment, data = dan )

summary(res.aov_dan)
##              Df Sum Sq Mean Sq F value Pr(>F)    
## Treatment     3   4151  1383.8   967.8 <2e-16 ***
## Residuals   351    502     1.4                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
pairwise.t.test(dan$Flaking, dan$Treatment, p.adj = "bonf")
## 
##  Pairwise comparisons using t tests with pooled SD 
## 
## data:  dan$Flaking and dan$Treatment 
## 
##         Keto    Placebo PyrI
## Placebo < 2e-16 -       -   
## PyrI    5.8e-15 < 2e-16 -   
## PyrII   2.3e-11 < 2e-16 1   
## 
## P value adjustment method: bonferroni

Bonferroni correction leads to p-values that are all significant besides the comparison between the PyrII and PyrI groups, which is supported in the contrast analysis. This shows that overall, the placebo has a significantly different mean that the other non-placebo groups.

13.11

grocery = read.csv("EX13-11SMART2.csv")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
with = filter(grocery, Smartcart == "With")
wo = filter(grocery, Smartcart == "Without")

a)

xbar_with = mean(with$TotalCost)

xbar_wo = mean(wo$TotalCost)

barplot(c(xbar_with, xbar_wo), names.arg = c("With", "Without"), 
        col = c("#eb8060", "#b9e38d"))

The plot shows that the means of the two groups are quite close. Without is slightly higher than With real-time feedback.

b)

res.aov_grocery <- aov(TotalCost ~ Smartcart, data = grocery )

summary(res.aov_grocery)
##              Df Sum Sq Mean Sq F value Pr(>F)
## Smartcart     1     58   57.91   1.026  0.312
## Residuals   192  10838   56.45

c)

The degrees of freedeomfor the residuls is 192, the F statistic if 1.026 and the p-value is 0.312.

Based on this analysis, the estimated effects may be unbalanced, but the p-value suggest that there is not enough evidence to conclude that the means are significantly different ebtween the two groups. Therefore, there is not enough evidence to suggest that receiving real-time feedback has a significant effect on the total cost of the shopping cart.

13.22

a)

biling = read.csv("EX13-22BILING.csv")
young_mon = filter(biling, Age == "Young" & Ling =="Mono")
old_mon = filter(biling, Age == "Old" & Ling =="Mono")
young_bi = filter(biling, Age == "Young" & Ling =="Bi")
old_bi = filter(biling, Age == "Old" & Ling =="Bi")
xbar_young_mon = mean(young_mon$Time)
xbar_old_mon = mean(old_mon$Time)
xbar_young_bi = mean(young_bi$Time)
xbar_old_bi = mean(old_bi$Time)
sd_young_mon = sd(young_mon$Time)
sd_old_mon = sd(old_mon$Time)
sd_young_bi = sd(young_bi$Time)
sd_old_bi = sd(old_bi$Time)
sample_sizes_biling = c(20, 20, 20, 20)
means_biling = c(xbar_young_mon, xbar_old_mon, xbar_young_bi, xbar_old_bi)
sds_biling = c(sd_young_mon, sd_old_mon, sd_young_bi, sd_old_bi)

data.frame(sample_sizes_biling, means_biling, sds_biling)
##   sample_sizes_biling means_biling sds_biling
## 1                  20       820.70   47.38543
## 2                  20       996.85   53.59524
## 3                  20       785.65   42.04293
## 4                  20       919.30   54.38663
sqrt(54.38)/sqrt(42.02)
## [1] 1.137605

The rule for examining standard deviations in a pooled states that if the largest standard deviation is less than twice the smallest standard deviation, we can use methods based on the assumption of equal standard deviations, and our results will still be approximately correct. In this case, the ratio of the largest to smallest standard deviation is \(\frac{\sqrt{54.38}}{\sqrt{42.04}} = 1.137 < 2\); therefore, we can pool the standard deviations as it meets the assumptions.

b)

par(mfrow = c(2,2))

hist(young_mon$Time, breaks = 25)
hist(old_mon$Time, breaks = 25)
hist(young_bi$Time, breaks = 25)
hist(old_bi$Time, breaks = 25)

It appears that young groups are approximately normal, but the old groups are skewed or widely dispersed. Therefore, I would not make the qualitative conclusion that the old groups are approximately Normal.

13.23

a)

interaction.plot(x.factor = biling$Age, #x-axis variable
                 trace.factor = biling$Ling, #variable for lines
                 response = biling$Time, #y-axis variable
                 fun = median, #metric to plot
                 ylab = "Time",
                 xlab = "Language Level",
                 col = c("pink", "blue"),
                 lty = 1, #line type
                 lwd = 2, #line width
                 trace.label = "Languages Known")

We might find an interaction between age and lingualism because the more we age, the hard it becomes to learn a language and the more brain function declines in general.

b)

res.aov_biling <- aov(Time ~ Age + Ling, data= biling )

summary(res.aov_biling)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## Age          1 479880  479880   188.5  < 2e-16 ***
## Ling         1  63394   63394    24.9 3.66e-06 ***
## Residuals   77 196055    2546                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

The F-value for Age and Linguistic ability is 188.5 and 24.9,r espectively. Both variables has a significant p-value of less than 0.001. Both variables have a degree of freedom of 1 and the residuls df = 77.

c)

This analysis suggests that an interaction effect between age and bilinguilism is abesnt since the p-values are significant for both. This suggests that both Age and Bilinguilism contribute to reaction times for the cognitive test.

16.18

library(dplyr)
drp_data = read.csv("EX16-18DRP.csv")
head(drp_data)
##   id group g drp
## 1  1 Treat 0  24
## 2  2 Treat 0  56
## 3  3 Treat 0  43
## 4  4 Treat 0  59
## 5  5 Treat 0  58
## 6  6 Treat 0  52
drp_data <- drp_data %>% 
mutate(group = as.factor(group)) 

a)

meandiff <- function(d, i){
  d = d[i,] 
  y = tapply(d$drp, d$group, mean) 
  y[1]-y[2] 
} 
library(boot)

drp_boot = boot(drp_data, meandiff, R = 2000, strata = drp_data$group)
drp_boot
## 
## STRATIFIED BOOTSTRAP
## 
## 
## Call:
## boot(data = drp_data, statistic = meandiff, R = 2000, strata = drp_data$group)
## 
## 
## Bootstrap Statistics :
##      original      bias    std. error
## t1* -9.954451 -0.04919565    4.274403

The bootstrap standard error is 4.159

b)

plot(drp_boot) #graphing bootstrap distribution

mean(drp_boot$t) #checking bootstrap mean
## [1] -10.00365

The bootstrap t confidence interval is appropriate because the bootstrap distribution is approximately Normal

boot.ci(boot.out = drp_boot, type = "norm") 
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 2000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = drp_boot, type = "norm")
## 
## Intervals : 
## Level      Normal        
## 95%   (-18.283,  -1.528 )  
## Calculations and Intervals on Original Scale

c)

My bootstrap results are nearly identical to what is shown in example 7.14. this suggests that this confidence interval can be captured in multiple ways depending on the structure of the data. The actual mean of the boostrapoed data does in fact lie within this interval.

16.24

theta <- function(data,i) {
  d = data[i]
  mean(d)
}
# tvtime = read.csv("EX16-24TVTIME.csv")
time = c(3, 16.5, 10.5, 40.5, 5.5, 33.5, 0, 6.5) 
tvtime = data.frame(time) 

a)

xbar_tvtime = mean(tvtime$Time,  na.rm = T)
## Warning in mean.default(tvtime$Time, na.rm = T): argument is not numeric or
## logical: returning NA
tvtime_boot = boot(data = tvtime$time, statistic = theta, R = 2000)
plot(tvtime_boot)

Rhe plot show an approximately Normal distribution with a narrow variance about the mean. The observed xbar is 14.5 and there seems to be a tight grouping of means around that value for t*.

b)

boot.ci(boot.out = tvtime_boot, type = "norm")
## BOOTSTRAP CONFIDENCE INTERVAL CALCULATIONS
## Based on 2000 bootstrap replicates
## 
## CALL : 
## boot.ci(boot.out = tvtime_boot, type = "norm")
## 
## Intervals : 
## Level      Normal        
## 95%   ( 4.84, 24.38 )  
## Calculations and Intervals on Original Scale

The 95% bootstrap confidence interval for \(\mu\) is (5.11, 23.86).

c)

t.test(tvtime$time)
## 
##  One Sample t-test
## 
## data:  tvtime$time
## t = 2.761, df = 7, p-value = 0.02806
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##   2.081702 26.918298
## sample estimates:
## mean of x 
##      14.5

The usual t interval is (2.08, 26.91), which is wider than the one capture via bootstrapping of (5.11, 23.86). Neither interval includes 0. The wider interval for the t procedure could eb due to the small sample size and the fact that bootstrapping creates a new distribution from the original data directly.