R Markdown

5. In Chapter 4, we used logistic regression to predict the probability of default using income and balance on the Default data set. We will now estimate the test error of this logistic regression model using the validation set approach. Do not forget to set a random seed before beginning your analysis.

(a) Fit a logistic regression model that uses income and balance to predict default.

(b) Using the validation set approach, estimate the test error of this model. In order to do this, you must perform the following steps: i. Split the sample set into a training set and a validation set. ii. Fit a multiple logistic regression model using only the training observations. iii. Obtain a prediction of default status for each individual in the validation set by computing the posterior probability of default for that individual, and classifying the individual to the default category if the posterior probability is greater than 0.5. iv. Compute the validation set error, which is the fraction of the observations in the validation set that are misclassified.

(c) Repeat the process in (b) three times, using three different splits of the observations into a training set and a validation set. Comment on the results obtained.

(d) Now consider a logistic regression model that predicts the probability of default using income, balance, and a dummy variable for student. Estimate the test error for this model using the validation set approach. Comment on whether or not including a dummy variable for student leads to a reduction in the test error rate.

Answer:

(a) Fit a logistic regression model using income and balance to predict default.

set.seed(123)
library(ISLR)
## Warning: package 'ISLR' was built under R version 4.3.2
library(MASS)
## Warning: package 'MASS' was built under R version 4.3.2
library(boot)
## Warning: package 'boot' was built under R version 4.3.3
library(ISLR2)
## Warning: package 'ISLR2' was built under R version 4.3.2
## 
## Attaching package: 'ISLR2'
## The following object is masked from 'package:MASS':
## 
##     Boston
## The following objects are masked from 'package:ISLR':
## 
##     Auto, Credit
data(Default)
model <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(model)
## 
## Call:
## glm(formula = default ~ income + balance, family = "binomial", 
##     data = Default)
## 
## Coefficients:
##               Estimate Std. Error z value Pr(>|z|)    
## (Intercept) -1.154e+01  4.348e-01 -26.545  < 2e-16 ***
## income       2.081e-05  4.985e-06   4.174 2.99e-05 ***
## balance      5.647e-03  2.274e-04  24.836  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 2920.6  on 9999  degrees of freedom
## Residual deviance: 1579.0  on 9997  degrees of freedom
## AIC: 1585
## 
## Number of Fisher Scoring iterations: 8

(b) Validation Set Approach

i. Split the dataset into a training set and a validation set

set.seed(123)  # Setting the random seed for reproducibility
train_indices <- sample(1:nrow(Default), 0.7 * nrow(Default))
train_set <- Default[train_indices, ]
validation_set <- Default[-train_indices, ]

ii. Fit a logistic regression model using only the training observations

model_train <- glm(default ~ income + balance, data = train_set, family = "binomial")

iii. Obtain predictions for the validation set

predictions <- predict(model_train, newdata = validation_set, type = "response")

iv. Compute the validation set error

predicted_classes <- ifelse(predictions > 0.5, "Yes", "No")
validation_error <- mean(predicted_classes != validation_set$default)
cat("Validation Set Error:", validation_error, "\n")
## Validation Set Error: 0.02633333

(c) Repeat the process three times

for (i in 1:3) {
  set.seed(i * 123)  # Vary the random seed for each iteration
  train_indices <- sample(1:nrow(Default), 0.7 * nrow(Default))
  train_set <- Default[train_indices, ]
  validation_set <- Default[-train_indices, ]

  model_train <- glm(default ~ income + balance, data = train_set, family = "binomial")

  predictions <- predict(model_train, newdata = validation_set, type = "response")

  predicted_classes <- ifelse(predictions > 0.5, "Yes", "No")
  validation_error <- mean(predicted_classes != validation_set$default)

  cat("Iteration", i, "- Validation Set Error:", validation_error, "\n")
}
## Iteration 1 - Validation Set Error: 0.02633333 
## Iteration 2 - Validation Set Error: 0.02566667 
## Iteration 3 - Validation Set Error: 0.02566667

(d) Logistic regression model with a dummy variable for student

model_student <- glm(default ~ income + balance + student, data = Default, family = "binomial")
set.seed(456)
train_indices_student <- sample(1:nrow(Default), 0.7 * nrow(Default))
train_set_student <- Default[train_indices_student, ]
validation_set_student <- Default[-train_indices_student, ]
model_train_student <- glm(default ~ income + balance + student, data = train_set_student, family = "binomial")
predictions_student <- predict(model_train_student, newdata = validation_set_student, type = "response")
predicted_classes_student <- ifelse(predictions_student > 0.5, "Yes", "No")
validation_error_student <- mean(predicted_classes_student != validation_set_student$default)
cat("Validation Set Error with Student:", validation_error_student, "\n")
## Validation Set Error with Student: 0.02533333

6. We continue to consider the use of a logistic regression model to predict the probability of default using income and balance on the Default data set. In particular, we will now compute estimates for the standard errors of the income and balance logistic regression coefficients in two different ways: (1) using the bootstrap, and (2) using the standard formula for computing the standard errors in the glm() function. Do not forget to set a random seed before beginning your analysis.

(a) Using the summary() and glm() functions, determine the estimated standard errors for the coefficients associated with income and balance in a multiple logistic regression model that uses both predictors.

(b) Write a function, boot.fn(), that takes as input the Default dataset as well as an index of the observations, and that outputs the coefficient estimates for income and balance in the multiple logistic regression model.

(c) Use the boot() function together with your boot.fn() function to estimate the standard errors of the logistic regression coefficients for income and balance.

(d) Comment on the estimated standard errors obtained using the glm() function and using your bootstrap function.

Answer:

(a) Estimated standard errors using glm() function

set.seed(123)
model <- glm(default ~ income + balance, data = Default, family = "binomial")
summary(model)$coef[, "Std. Error"]
##  (Intercept)       income      balance 
## 4.347564e-01 4.985167e-06 2.273731e-04

(b) Write a function boot.fn() to obtain coefficient estimates

boot.fn <- function(data, indices) {

  data_subset <- data[indices, ]

  model <- glm(default ~ income + balance, data = data_subset, family = "binomial")

  return(coef(model))
}

boot.fn(Default, 1:nrow(Default))
##   (Intercept)        income       balance 
## -1.154047e+01  2.080898e-05  5.647103e-03

(c) Use boot() function with boot.fn() to estimate standard errors

set.seed(123)

boot_results <- boot(data = Default, statistic = boot.fn, R = 1000)

boot_results[["t"]]
##              [,1]         [,2]        [,3]
##    [1,] -10.88213 1.624170e-05 0.005346078
##    [2,] -11.31709 1.632167e-05 0.005602701
##    [3,] -11.34847 2.071484e-05 0.005466396
##    [4,] -11.64477 1.834447e-05 0.005696808
##    [5,] -12.23103 1.658822e-05 0.006046286
##    [6,] -11.60049 2.272420e-05 0.005682856
##    [7,] -11.39460 2.173336e-05 0.005652672
##    [8,] -12.17640 3.085104e-05 0.005751027
##    [9,] -11.60617 2.583625e-05 0.005630166
##   [10,] -11.44963 1.967535e-05 0.005642654
##   [11,] -11.62174 2.709303e-05 0.005519567
##   [12,] -11.94310 2.761515e-05 0.005721410
##   [13,] -11.18424 1.795521e-05 0.005484970
##   [14,] -11.63493 2.279378e-05 0.005651825
##   [15,] -11.59603 2.230022e-05 0.005640176
##   [16,] -11.53970 2.144295e-05 0.005691915
##   [17,] -11.94775 2.546096e-05 0.005776500
##   [18,] -11.68317 2.602188e-05 0.005596255
##   [19,] -11.56951 2.446273e-05 0.005607766
##   [20,] -11.26251 2.059369e-05 0.005444158
##   [21,] -11.21913 2.745115e-05 0.005262065
##   [22,] -11.68421 1.721525e-05 0.005783921
##   [23,] -11.72833 2.944327e-05 0.005593401
##   [24,] -11.73315 2.568800e-05 0.005598737
##   [25,] -11.49227 2.434756e-05 0.005533615
##   [26,] -10.56844 1.301311e-05 0.005253106
##   [27,] -11.96601 2.429456e-05 0.005738360
##   [28,] -11.62041 2.604321e-05 0.005543644
##   [29,] -11.76906 2.195311e-05 0.005724069
##   [30,] -11.25959 1.807839e-05 0.005542065
##   [31,] -11.61846 1.363730e-05 0.005781797
##   [32,] -11.53881 2.096366e-05 0.005656486
##   [33,] -11.36678 2.817511e-05 0.005424790
##   [34,] -11.60510 1.945403e-05 0.005732876
##   [35,] -11.86399 2.265852e-05 0.005809918
##   [36,] -12.48718 2.681598e-05 0.006064964
##   [37,] -11.46977 1.873684e-05 0.005724292
##   [38,] -12.30329 2.484537e-05 0.006067166
##   [39,] -12.01088 2.419941e-05 0.005833213
##   [40,] -12.17938 2.830642e-05 0.005884831
##   [41,] -11.88401 2.807125e-05 0.005766403
##   [42,] -11.75685 2.168575e-05 0.005754445
##   [43,] -11.24693 1.482022e-05 0.005559502
##   [44,] -11.09461 1.534469e-05 0.005498700
##   [45,] -11.68570 2.377186e-05 0.005646547
##   [46,] -11.23185 2.165277e-05 0.005479465
##   [47,] -11.64035 2.150963e-05 0.005743585
##   [48,] -10.75407 1.431492e-05 0.005327792
##   [49,] -11.23492 1.976103e-05 0.005485831
##   [50,] -11.67604 2.312627e-05 0.005671992
##   [51,] -11.39133 1.593353e-05 0.005743267
##   [52,] -10.79773 1.259884e-05 0.005411179
##   [53,] -11.28298 2.207670e-05 0.005470376
##   [54,] -11.36937 1.483483e-05 0.005636961
##   [55,] -11.41508 1.817521e-05 0.005666105
##   [56,] -11.53475 2.205136e-05 0.005630763
##   [57,] -10.85317 1.638217e-05 0.005364644
##   [58,] -11.97535 2.469769e-05 0.005831288
##   [59,] -11.43962 2.092828e-05 0.005594249
##   [60,] -11.90197 1.841097e-05 0.005893433
##   [61,] -11.90607 1.466145e-05 0.005999701
##   [62,] -11.16937 1.531919e-05 0.005559126
##   [63,] -11.71375 3.471246e-05 0.005545058
##   [64,] -12.16971 2.114536e-05 0.005962340
##   [65,] -10.94923 1.723486e-05 0.005358094
##   [66,] -11.62507 2.164676e-05 0.005646186
##   [67,] -11.52658 1.582453e-05 0.005689573
##   [68,] -11.62142 2.132932e-05 0.005680878
##   [69,] -12.34309 2.677297e-05 0.006013030
##   [70,] -11.13761 1.854772e-05 0.005419907
##   [71,] -11.85341 2.827348e-05 0.005705731
##   [72,] -11.94055 2.477100e-05 0.005780151
##   [73,] -11.55769 2.468425e-05 0.005612707
##   [74,] -11.72427 2.589529e-05 0.005599678
##   [75,] -11.37640 1.876517e-05 0.005628669
##   [76,] -11.59724 2.677803e-05 0.005573944
##   [77,] -11.96779 2.185845e-05 0.005893581
##   [78,] -11.65497 2.413474e-05 0.005677703
##   [79,] -11.23593 2.421467e-05 0.005362869
##   [80,] -10.67241 1.883609e-05 0.005207420
##   [81,] -11.67362 2.373955e-05 0.005698529
##   [82,] -11.93565 2.617524e-05 0.005785355
##   [83,] -11.09706 2.277854e-05 0.005319700
##   [84,] -11.67600 2.307990e-05 0.005648667
##   [85,] -11.11512 2.215077e-05 0.005358699
##   [86,] -11.33119 1.646774e-05 0.005627026
##   [87,] -11.17116 1.943362e-05 0.005441693
##   [88,] -11.71784 3.043738e-05 0.005567661
##   [89,] -11.07270 1.979608e-05 0.005390530
##   [90,] -11.68174 1.594320e-05 0.005847786
##   [91,] -11.32849 1.421414e-05 0.005649620
##   [92,] -11.04295 1.865244e-05 0.005414685
##   [93,] -11.67692 1.815353e-05 0.005725345
##   [94,] -11.58083 2.329956e-05 0.005655921
##   [95,] -11.31497 2.448481e-05 0.005464538
##   [96,] -11.61305 2.505320e-05 0.005606312
##   [97,] -11.60718 2.543549e-05 0.005626937
##   [98,] -11.45785 2.662722e-05 0.005520832
##   [99,] -11.81464 2.200236e-05 0.005808086
##  [100,] -12.19388 2.348260e-05 0.006015426
##  [101,] -11.11276 1.749914e-05 0.005475882
##  [102,] -11.26551 2.531094e-05 0.005373891
##  [103,] -11.88905 2.223829e-05 0.005871028
##  [104,] -11.86921 2.748054e-05 0.005706860
##  [105,] -11.10547 1.937160e-05 0.005433453
##  [106,] -11.20841 8.455788e-06 0.005640066
##  [107,] -11.22644 2.012310e-05 0.005427521
##  [108,] -11.49945 2.452909e-05 0.005574960
##  [109,] -11.10182 1.372364e-05 0.005583970
##  [110,] -12.16578 2.840216e-05 0.005857364
##  [111,] -11.29631 2.142757e-05 0.005487843
##  [112,] -11.21018 1.701344e-05 0.005509184
##  [113,] -12.03010 2.464261e-05 0.005894977
##  [114,] -12.21922 3.807880e-05 0.005720091
##  [115,] -11.45345 1.880787e-05 0.005626062
##  [116,] -12.07418 1.992865e-05 0.005895322
##  [117,] -11.43942 2.485178e-05 0.005496934
##  [118,] -11.42070 1.767318e-05 0.005562266
##  [119,] -11.78188 2.410179e-05 0.005715957
##  [120,] -12.01887 2.562918e-05 0.005847547
##  [121,] -12.05955 2.205463e-05 0.006012420
##  [122,] -11.14554 2.135507e-05 0.005415430
##  [123,] -10.94728 1.902812e-05 0.005257459
##  [124,] -11.53213 1.559003e-05 0.005735836
##  [125,] -11.25454 1.838719e-05 0.005567393
##  [126,] -12.12404 2.752257e-05 0.005853625
##  [127,] -11.86120 1.670852e-05 0.005938297
##  [128,] -11.05617 1.721888e-05 0.005509439
##  [129,] -11.39119 2.103669e-05 0.005522301
##  [130,] -12.66392 3.054164e-05 0.006167916
##  [131,] -11.21313 2.270360e-05 0.005405058
##  [132,] -11.21254 2.175192e-05 0.005437242
##  [133,] -11.61681 2.060733e-05 0.005722988
##  [134,] -12.26750 2.338237e-05 0.006062680
##  [135,] -10.79139 1.396452e-05 0.005319019
##  [136,] -11.98216 2.509724e-05 0.005915198
##  [137,] -11.05024 1.827558e-05 0.005416240
##  [138,] -11.33436 1.940978e-05 0.005477049
##  [139,] -11.40924 1.720561e-05 0.005636276
##  [140,] -11.57365 2.313383e-05 0.005687605
##  [141,] -12.12181 2.716597e-05 0.005924396
##  [142,] -11.05340 1.869583e-05 0.005432763
##  [143,] -11.43832 1.899519e-05 0.005535950
##  [144,] -11.31931 2.337907e-05 0.005519791
##  [145,] -11.90794 3.362343e-05 0.005689171
##  [146,] -12.03474 2.350040e-05 0.005841559
##  [147,] -11.38649 2.302178e-05 0.005492500
##  [148,] -11.39293 1.763576e-05 0.005595069
##  [149,] -12.21864 2.592291e-05 0.005963501
##  [150,] -11.42762 2.286590e-05 0.005590264
##  [151,] -11.45753 1.838477e-05 0.005632719
##  [152,] -11.33921 2.776630e-05 0.005374290
##  [153,] -11.27994 1.616681e-05 0.005661230
##  [154,] -11.94214 2.134645e-05 0.005913241
##  [155,] -12.02400 2.696778e-05 0.005828488
##  [156,] -11.19941 1.992429e-05 0.005506473
##  [157,] -11.26442 1.488461e-05 0.005591080
##  [158,] -11.56732 2.098681e-05 0.005710610
##  [159,] -11.42761 2.762649e-05 0.005391974
##  [160,] -12.44084 2.442032e-05 0.006181123
##  [161,] -11.03531 1.448841e-05 0.005436808
##  [162,] -11.89640 1.770961e-05 0.005988947
##  [163,] -11.56274 1.706523e-05 0.005706842
##  [164,] -11.02295 2.074526e-05 0.005378860
##  [165,] -12.17526 2.285921e-05 0.005987473
##  [166,] -11.93528 1.916273e-05 0.005904891
##  [167,] -11.27926 2.645295e-05 0.005328765
##  [168,] -11.49537 1.840915e-05 0.005679159
##  [169,] -12.39240 2.683819e-05 0.006062912
##  [170,] -11.23284 1.732751e-05 0.005491235
##  [171,] -12.05010 2.243358e-05 0.005910472
##  [172,] -11.89093 1.736367e-05 0.005943334
##  [173,] -11.51558 2.131771e-05 0.005581557
##  [174,] -11.44487 2.252616e-05 0.005510815
##  [175,] -11.77129 2.229322e-05 0.005777042
##  [176,] -11.62417 2.353504e-05 0.005629258
##  [177,] -10.72741 1.460005e-05 0.005294142
##  [178,] -11.36506 2.208615e-05 0.005521068
##  [179,] -11.27906 1.427879e-05 0.005591953
##  [180,] -11.37274 2.377057e-05 0.005498058
##  [181,] -11.05627 2.586097e-05 0.005292539
##  [182,] -10.72892 1.048967e-05 0.005359848
##  [183,] -11.62529 1.980157e-05 0.005703135
##  [184,] -11.83570 2.589764e-05 0.005724748
##  [185,] -12.02007 2.425194e-05 0.005899232
##  [186,] -11.51400 1.973871e-05 0.005703676
##  [187,] -10.89025 2.202251e-05 0.005278284
##  [188,] -10.94730 2.309914e-05 0.005246545
##  [189,] -11.77974 2.287285e-05 0.005730030
##  [190,] -12.14518 2.491162e-05 0.005885223
##  [191,] -11.83703 2.106820e-05 0.005811982
##  [192,] -10.98807 1.425415e-05 0.005425859
##  [193,] -11.74618 2.096087e-05 0.005796600
##  [194,] -11.64782 1.973536e-05 0.005676372
##  [195,] -11.35626 2.543085e-05 0.005464802
##  [196,] -11.46187 1.575631e-05 0.005781054
##  [197,] -10.84091 1.564743e-05 0.005309492
##  [198,] -11.88130 2.341933e-05 0.005849015
##  [199,] -12.15259 2.731658e-05 0.005889662
##  [200,] -11.76054 1.739582e-05 0.005808991
##  [201,] -11.67471 1.929311e-05 0.005735144
##  [202,] -11.25797 2.294614e-05 0.005470640
##  [203,] -11.39114 1.434974e-05 0.005708442
##  [204,] -11.73807 1.406731e-05 0.005883994
##  [205,] -12.07320 3.343062e-05 0.005658760
##  [206,] -11.63599 2.526376e-05 0.005686010
##  [207,] -11.65531 2.524413e-05 0.005607369
##  [208,] -11.59915 2.899529e-05 0.005443666
##  [209,] -11.71483 1.827754e-05 0.005741937
##  [210,] -11.47623 1.769697e-05 0.005731482
##  [211,] -11.43312 1.677929e-05 0.005735194
##  [212,] -11.82661 2.143585e-05 0.005832258
##  [213,] -11.78936 2.414983e-05 0.005744746
##  [214,] -10.94248 2.442014e-05 0.005213468
##  [215,] -11.62899 2.569100e-05 0.005619354
##  [216,] -12.11002 2.404032e-05 0.005882621
##  [217,] -11.26444 1.881157e-05 0.005561461
##  [218,] -12.05176 2.946102e-05 0.005725854
##  [219,] -11.48368 2.238137e-05 0.005516845
##  [220,] -11.55705 1.542131e-05 0.005717471
##  [221,] -11.55631 2.008266e-05 0.005684840
##  [222,] -11.77332 2.456595e-05 0.005746811
##  [223,] -10.98751 1.547726e-05 0.005334529
##  [224,] -11.56033 1.870310e-05 0.005777480
##  [225,] -12.40289 2.639428e-05 0.006117414
##  [226,] -11.80286 2.245902e-05 0.005790793
##  [227,] -12.08919 2.167460e-05 0.005933766
##  [228,] -11.62397 1.946471e-05 0.005706126
##  [229,] -11.76170 1.991506e-05 0.005733897
##  [230,] -12.26836 2.653092e-05 0.006009385
##  [231,] -11.66273 2.324318e-05 0.005681127
##  [232,] -11.68849 2.388128e-05 0.005687288
##  [233,] -11.17220 1.268308e-05 0.005507908
##  [234,] -12.36328 3.313450e-05 0.005801859
##  [235,] -11.63557 2.431109e-05 0.005658105
##  [236,] -12.00272 2.034613e-05 0.005932974
##  [237,] -11.80453 1.452365e-05 0.005957102
##  [238,] -11.60033 1.645190e-05 0.005812025
##  [239,] -10.95720 2.811095e-05 0.005193135
##  [240,] -11.51150 2.055767e-05 0.005633809
##  [241,] -11.35792 1.620779e-05 0.005695608
##  [242,] -11.46000 2.042316e-05 0.005582181
##  [243,] -12.30802 3.091476e-05 0.005987424
##  [244,] -11.74874 2.471845e-05 0.005686200
##  [245,] -11.33554 1.482304e-05 0.005640475
##  [246,] -11.42825 2.202726e-05 0.005541431
##  [247,] -11.21308 1.941658e-05 0.005483368
##  [248,] -11.55855 2.028464e-05 0.005698069
##  [249,] -11.22091 2.257398e-05 0.005442246
##  [250,] -11.55244 1.305055e-05 0.005761247
##  [251,] -11.77740 2.446025e-05 0.005786013
##  [252,] -11.76168 2.123808e-05 0.005786790
##  [253,] -10.92548 2.165212e-05 0.005196792
##  [254,] -11.42085 1.415364e-05 0.005718582
##  [255,] -11.51203 1.827671e-05 0.005700295
##  [256,] -11.01204 1.891602e-05 0.005369698
##  [257,] -11.44091 1.450531e-05 0.005755021
##  [258,] -12.15392 2.411317e-05 0.005985184
##  [259,] -11.80899 2.389815e-05 0.005750010
##  [260,] -10.77053 1.308342e-05 0.005417383
##  [261,] -12.32241 1.941270e-05 0.006110452
##  [262,] -11.16486 2.232660e-05 0.005407950
##  [263,] -10.80624 1.499364e-05 0.005287331
##  [264,] -11.09259 1.864035e-05 0.005409309
##  [265,] -12.09214 1.706833e-05 0.006058244
##  [266,] -11.87249 1.772212e-05 0.005876441
##  [267,] -11.18398 1.404553e-05 0.005607610
##  [268,] -11.71187 2.168176e-05 0.005779913
##  [269,] -11.41546 1.870530e-05 0.005618008
##  [270,] -11.68024 2.125484e-05 0.005770612
##  [271,] -12.42089 2.445421e-05 0.006170815
##  [272,] -11.27895 2.798303e-05 0.005351367
##  [273,] -11.67916 1.862688e-05 0.005726363
##  [274,] -11.46936 1.267664e-05 0.005767841
##  [275,] -11.45578 2.515436e-05 0.005533678
##  [276,] -11.45790 1.348384e-05 0.005733113
##  [277,] -12.20734 1.332830e-05 0.006199665
##  [278,] -12.07413 2.020435e-05 0.005953024
##  [279,] -11.97351 2.031556e-05 0.005986307
##  [280,] -11.56158 2.171434e-05 0.005653814
##  [281,] -11.39456 2.163643e-05 0.005515765
##  [282,] -11.48352 2.268074e-05 0.005613078
##  [283,] -12.58318 2.739713e-05 0.006088740
##  [284,] -12.42029 2.649289e-05 0.006004097
##  [285,] -11.83635 2.106221e-05 0.005842853
##  [286,] -10.99034 1.449528e-05 0.005408241
##  [287,] -11.29896 1.981362e-05 0.005489929
##  [288,] -10.81894 2.159337e-05 0.005193871
##  [289,] -11.77495 2.421070e-05 0.005687223
##  [290,] -12.05307 2.419703e-05 0.005918854
##  [291,] -11.82283 1.905703e-05 0.005836996
##  [292,] -11.19236 1.545573e-05 0.005513442
##  [293,] -11.43939 2.838377e-05 0.005422124
##  [294,] -11.95797 2.653217e-05 0.005787190
##  [295,] -13.13819 3.192695e-05 0.006391965
##  [296,] -10.95981 1.730531e-05 0.005335715
##  [297,] -11.96467 1.630538e-05 0.006035876
##  [298,] -11.88579 2.735334e-05 0.005710634
##  [299,] -12.27850 3.288733e-05 0.005783129
##  [300,] -11.88539 2.212174e-05 0.005808772
##  [301,] -11.72460 1.592248e-05 0.005762960
##  [302,] -11.03727 1.201862e-05 0.005494907
##  [303,] -11.18371 1.428705e-05 0.005592546
##  [304,] -11.79223 2.610195e-05 0.005706608
##  [305,] -11.29979 2.215934e-05 0.005417024
##  [306,] -12.68136 2.251207e-05 0.006239023
##  [307,] -11.40171 1.761947e-05 0.005560837
##  [308,] -11.51169 2.765245e-05 0.005545974
##  [309,] -11.57551 2.118651e-05 0.005635009
##  [310,] -11.19010 2.602008e-05 0.005325426
##  [311,] -11.73484 1.336459e-05 0.005871274
##  [312,] -12.12897 2.350660e-05 0.005912906
##  [313,] -11.69316 1.874765e-05 0.005793445
##  [314,] -11.88674 2.271250e-05 0.005804224
##  [315,] -11.33568 1.746228e-05 0.005622869
##  [316,] -11.97336 2.086679e-05 0.005916654
##  [317,] -11.15771 2.284094e-05 0.005390831
##  [318,] -11.52692 2.210022e-05 0.005629502
##  [319,] -11.47796 1.576606e-05 0.005739540
##  [320,] -11.85438 2.526245e-05 0.005716473
##  [321,] -11.41329 1.480955e-05 0.005710033
##  [322,] -11.68198 1.458043e-05 0.005833364
##  [323,] -11.13287 1.888090e-05 0.005454420
##  [324,] -11.17101 2.022600e-05 0.005501647
##  [325,] -12.11534 2.934261e-05 0.005764704
##  [326,] -11.65883 2.193912e-05 0.005661999
##  [327,] -11.28425 2.259470e-05 0.005426501
##  [328,] -11.45665 2.147879e-05 0.005572821
##  [329,] -11.20172 2.160081e-05 0.005329642
##  [330,] -11.32282 2.315475e-05 0.005491789
##  [331,] -11.21084 2.139555e-05 0.005507343
##  [332,] -11.47852 2.062574e-05 0.005572026
##  [333,] -11.54417 2.175212e-05 0.005614915
##  [334,] -11.50187 2.078929e-05 0.005657470
##  [335,] -11.71857 2.881706e-05 0.005589722
##  [336,] -12.23606 2.884221e-05 0.005906950
##  [337,] -11.49697 2.461658e-05 0.005503998
##  [338,] -11.79491 2.223983e-05 0.005727314
##  [339,] -11.87817 2.800590e-05 0.005747042
##  [340,] -11.88281 2.324302e-05 0.005810453
##  [341,] -11.93350 2.095354e-05 0.005847675
##  [342,] -11.22746 1.681244e-05 0.005562304
##  [343,] -10.84197 1.586968e-05 0.005329199
##  [344,] -11.68617 2.183071e-05 0.005750349
##  [345,] -11.22780 1.201345e-05 0.005551146
##  [346,] -11.56868 1.790249e-05 0.005797043
##  [347,] -11.93587 2.270976e-05 0.005845938
##  [348,] -11.28530 2.104278e-05 0.005569864
##  [349,] -11.26164 1.562193e-05 0.005533808
##  [350,] -11.94972 1.987952e-05 0.005915080
##  [351,] -11.25818 1.603276e-05 0.005558211
##  [352,] -11.08041 1.479762e-05 0.005510240
##  [353,] -11.31777 2.238643e-05 0.005507349
##  [354,] -11.69793 1.869409e-05 0.005810043
##  [355,] -11.37934 1.563097e-05 0.005667666
##  [356,] -11.08502 1.197976e-05 0.005562132
##  [357,] -11.20862 1.645361e-05 0.005460150
##  [358,] -11.53587 1.538924e-05 0.005795015
##  [359,] -11.16104 1.907940e-05 0.005507918
##  [360,] -11.64821 2.329570e-05 0.005684817
##  [361,] -11.91249 2.127423e-05 0.005802697
##  [362,] -11.93681 2.555177e-05 0.005826434
##  [363,] -11.97934 2.345799e-05 0.005893600
##  [364,] -11.75614 1.520194e-05 0.005789950
##  [365,] -11.54889 2.823366e-05 0.005476708
##  [366,] -12.14356 2.813159e-05 0.005829967
##  [367,] -11.11895 1.512618e-05 0.005551394
##  [368,] -11.89045 1.454080e-05 0.005939745
##  [369,] -11.64382 2.367064e-05 0.005662643
##  [370,] -11.42040 2.153044e-05 0.005593397
##  [371,] -12.36953 2.570719e-05 0.006024506
##  [372,] -11.25784 2.010543e-05 0.005517121
##  [373,] -12.26999 2.907129e-05 0.005885676
##  [374,] -11.62038 2.593532e-05 0.005613282
##  [375,] -11.52040 2.306660e-05 0.005614932
##  [376,] -12.00323 2.766976e-05 0.005793005
##  [377,] -12.39337 2.279211e-05 0.006092549
##  [378,] -12.00972 2.671539e-05 0.005865252
##  [379,] -11.06804 1.407796e-05 0.005477464
##  [380,] -10.80077 1.788107e-05 0.005301821
##  [381,] -11.68773 2.264400e-05 0.005700476
##  [382,] -11.07268 2.425162e-05 0.005289436
##  [383,] -11.96096 2.554039e-05 0.005890431
##  [384,] -11.34808 2.346118e-05 0.005509647
##  [385,] -11.32633 2.264413e-05 0.005522661
##  [386,] -11.36945 1.340424e-05 0.005756056
##  [387,] -11.13095 9.019573e-06 0.005643397
##  [388,] -11.61861 2.533685e-05 0.005527697
##  [389,] -11.86795 1.331877e-05 0.006002811
##  [390,] -10.42139 1.221804e-05 0.005106045
##  [391,] -11.91803 2.523557e-05 0.005798862
##  [392,] -11.87837 2.174555e-05 0.005806298
##  [393,] -10.83365 1.213966e-05 0.005384935
##  [394,] -10.86810 1.721665e-05 0.005280670
##  [395,] -11.33607 1.816498e-05 0.005613232
##  [396,] -12.18871 2.236704e-05 0.006022972
##  [397,] -12.05177 2.187483e-05 0.005900150
##  [398,] -11.78389 2.185767e-05 0.005860581
##  [399,] -12.41082 2.812935e-05 0.006000712
##  [400,] -11.23412 1.955471e-05 0.005449502
##  [401,] -11.43641 1.868886e-05 0.005669159
##  [402,] -11.37597 1.292837e-05 0.005746718
##  [403,] -11.38308 2.273220e-05 0.005622170
##  [404,] -11.26534 1.502460e-05 0.005610849
##  [405,] -10.98137 1.926923e-05 0.005311879
##  [406,] -10.94560 1.826944e-05 0.005298594
##  [407,] -11.46222 1.830747e-05 0.005670880
##  [408,] -12.08087 1.625815e-05 0.006082003
##  [409,] -12.15388 2.402049e-05 0.005947517
##  [410,] -11.31757 2.147640e-05 0.005547481
##  [411,] -12.23898 2.677809e-05 0.006044004
##  [412,] -12.46600 2.092099e-05 0.006121408
##  [413,] -11.71859 2.043475e-05 0.005723999
##  [414,] -11.50909 2.655215e-05 0.005534627
##  [415,] -11.20461 1.285375e-05 0.005564203
##  [416,] -11.76407 2.172514e-05 0.005761731
##  [417,] -11.72570 2.457252e-05 0.005617606
##  [418,] -11.30628 2.046170e-05 0.005524760
##  [419,] -11.66838 1.706611e-05 0.005752581
##  [420,] -11.27340 2.191913e-05 0.005364505
##  [421,] -11.27352 1.926017e-05 0.005524240
##  [422,] -11.02402 2.441270e-05 0.005201767
##  [423,] -11.21652 2.099773e-05 0.005343448
##  [424,] -10.66787 1.566318e-05 0.005189725
##  [425,] -11.84905 2.299808e-05 0.005801982
##  [426,] -10.92416 1.768481e-05 0.005354397
##  [427,] -11.40136 2.476450e-05 0.005475902
##  [428,] -11.10495 1.533962e-05 0.005443201
##  [429,] -11.86708 2.502275e-05 0.005742568
##  [430,] -11.35076 1.675223e-05 0.005640730
##  [431,] -11.07733 2.604963e-05 0.005272245
##  [432,] -11.92687 2.469585e-05 0.005829604
##  [433,] -10.80429 1.369513e-05 0.005360997
##  [434,] -11.70928 2.056334e-05 0.005723953
##  [435,] -11.24788 1.844025e-05 0.005589907
##  [436,] -11.73382 1.764258e-05 0.005820375
##  [437,] -11.54286 2.805274e-05 0.005442072
##  [438,] -11.30834 1.658096e-05 0.005663340
##  [439,] -12.39416 2.840992e-05 0.006016698
##  [440,] -11.58994 2.525175e-05 0.005613947
##  [441,] -10.89737 1.551589e-05 0.005319470
##  [442,] -12.19474 2.625722e-05 0.005925715
##  [443,] -11.33481 1.727062e-05 0.005540002
##  [444,] -11.57811 1.890687e-05 0.005689048
##  [445,] -12.29749 2.202673e-05 0.005955457
##  [446,] -11.46599 2.269650e-05 0.005634594
##  [447,] -12.34944 3.119392e-05 0.005920708
##  [448,] -11.12131 2.160145e-05 0.005400879
##  [449,] -11.78889 2.292079e-05 0.005773008
##  [450,] -11.98959 2.429769e-05 0.005834865
##  [451,] -12.04944 1.961282e-05 0.006038480
##  [452,] -12.63976 2.497750e-05 0.006289572
##  [453,] -11.69545 2.138865e-05 0.005756335
##  [454,] -11.14739 1.758002e-05 0.005492474
##  [455,] -11.76888 1.754845e-05 0.005828331
##  [456,] -11.30822 2.237626e-05 0.005441833
##  [457,] -11.73349 2.313177e-05 0.005761226
##  [458,] -11.62913 1.506277e-05 0.005758015
##  [459,] -11.73608 2.384743e-05 0.005663689
##  [460,] -10.93800 1.861767e-05 0.005283156
##  [461,] -11.26881 2.083036e-05 0.005482454
##  [462,] -11.19473 2.135303e-05 0.005453881
##  [463,] -12.12699 2.364509e-05 0.005935306
##  [464,] -11.66593 1.827737e-05 0.005786310
##  [465,] -11.17280 1.576488e-05 0.005527888
##  [466,] -11.31758 2.330584e-05 0.005526048
##  [467,] -11.35258 1.626097e-05 0.005584016
##  [468,] -11.35689 2.517158e-05 0.005391651
##  [469,] -12.01675 2.263731e-05 0.005824622
##  [470,] -11.94943 2.510095e-05 0.005831279
##  [471,] -11.89188 2.329522e-05 0.005767838
##  [472,] -10.90467 2.240070e-05 0.005224032
##  [473,] -12.45948 2.679378e-05 0.006091765
##  [474,] -11.24113 2.097286e-05 0.005459584
##  [475,] -11.78521 1.666260e-05 0.005805057
##  [476,] -11.21922 2.012433e-05 0.005355290
##  [477,] -11.64223 1.370045e-05 0.005877536
##  [478,] -11.42389 1.973585e-05 0.005657580
##  [479,] -11.02340 1.972236e-05 0.005336725
##  [480,] -10.92933 2.057080e-05 0.005381887
##  [481,] -11.71657 2.522159e-05 0.005686983
##  [482,] -11.87720 2.422257e-05 0.005782698
##  [483,] -12.57270 2.156513e-05 0.006255420
##  [484,] -11.57413 2.388327e-05 0.005539385
##  [485,] -11.54815 1.840267e-05 0.005609683
##  [486,] -10.87299 2.092229e-05 0.005248359
##  [487,] -11.32509 1.752643e-05 0.005600164
##  [488,] -11.55387 2.665943e-05 0.005529111
##  [489,] -11.47292 1.844326e-05 0.005661653
##  [490,] -11.80464 1.672958e-05 0.005881459
##  [491,] -11.71611 2.100832e-05 0.005816454
##  [492,] -12.34529 2.751404e-05 0.005980600
##  [493,] -11.63163 2.267081e-05 0.005642221
##  [494,] -12.07039 1.627721e-05 0.005975107
##  [495,] -12.39797 2.883773e-05 0.006012686
##  [496,] -11.47205 2.072799e-05 0.005568263
##  [497,] -11.51290 2.579217e-05 0.005411470
##  [498,] -11.66867 2.271103e-05 0.005645516
##  [499,] -12.28861 2.115161e-05 0.006086316
##  [500,] -10.91831 2.430168e-05 0.005223849
##  [501,] -11.52949 1.486517e-05 0.005777836
##  [502,] -11.36219 1.735013e-05 0.005569704
##  [503,] -12.08410 2.128933e-05 0.005938719
##  [504,] -11.26937 2.205370e-05 0.005487162
##  [505,] -11.96818 1.942343e-05 0.005936251
##  [506,] -11.79889 2.675443e-05 0.005680350
##  [507,] -11.67944 2.372930e-05 0.005674875
##  [508,] -11.74529 1.756727e-05 0.005786424
##  [509,] -12.00225 2.070511e-05 0.005886793
##  [510,] -11.45304 1.846271e-05 0.005666397
##  [511,] -11.46327 2.417162e-05 0.005470756
##  [512,] -10.77435 1.339069e-05 0.005292344
##  [513,] -11.66443 1.663162e-05 0.005775121
##  [514,] -10.98579 1.472980e-05 0.005526919
##  [515,] -11.55705 2.954349e-05 0.005476925
##  [516,] -11.30579 1.921359e-05 0.005478360
##  [517,] -11.98988 2.448040e-05 0.005864828
##  [518,] -10.82562 2.303446e-05 0.005210400
##  [519,] -12.25503 2.465786e-05 0.006074146
##  [520,] -12.16638 2.432288e-05 0.005912771
##  [521,] -11.55312 2.345882e-05 0.005604092
##  [522,] -11.83476 2.053078e-05 0.005791498
##  [523,] -11.09521 2.267513e-05 0.005340316
##  [524,] -11.91657 2.536789e-05 0.005745978
##  [525,] -11.74677 1.925708e-05 0.005863840
##  [526,] -11.46694 1.611268e-05 0.005619376
##  [527,] -11.05223 1.639186e-05 0.005462553
##  [528,] -11.28623 2.296813e-05 0.005509531
##  [529,] -12.06362 2.435204e-05 0.005802715
##  [530,] -11.75045 2.410975e-05 0.005601907
##  [531,] -11.24234 2.429953e-05 0.005450926
##  [532,] -10.83340 1.876369e-05 0.005336120
##  [533,] -11.79448 1.753313e-05 0.005835015
##  [534,] -11.30369 1.825643e-05 0.005533078
##  [535,] -10.93348 1.789166e-05 0.005325507
##  [536,] -12.57048 2.518835e-05 0.006115941
##  [537,] -11.55662 1.290308e-05 0.005816216
##  [538,] -11.28222 1.785733e-05 0.005508921
##  [539,] -11.43302 1.975739e-05 0.005622237
##  [540,] -11.38527 1.723046e-05 0.005628327
##  [541,] -11.58118 1.988123e-05 0.005693859
##  [542,] -11.15558 1.980398e-05 0.005396378
##  [543,] -11.79791 1.856827e-05 0.005847437
##  [544,] -10.65167 1.486515e-05 0.005302971
##  [545,] -11.71940 1.871867e-05 0.005843132
##  [546,] -11.50943 1.626835e-05 0.005776384
##  [547,] -11.77953 1.651720e-05 0.005821506
##  [548,] -11.87301 2.634271e-05 0.005742479
##  [549,] -11.59052 1.915957e-05 0.005743625
##  [550,] -11.43082 2.143280e-05 0.005609328
##  [551,] -12.05178 2.117415e-05 0.005959326
##  [552,] -12.05756 1.874078e-05 0.005973209
##  [553,] -11.40028 2.764209e-05 0.005419881
##  [554,] -11.60839 2.420524e-05 0.005672096
##  [555,] -11.97006 1.828545e-05 0.006031347
##  [556,] -12.03676 3.097527e-05 0.005668366
##  [557,] -11.77360 2.565162e-05 0.005624168
##  [558,] -11.92285 2.364377e-05 0.005840326
##  [559,] -11.67850 2.772396e-05 0.005554128
##  [560,] -11.26246 1.897930e-05 0.005540796
##  [561,] -11.48815 2.914332e-05 0.005531544
##  [562,] -11.62193 2.651970e-05 0.005614158
##  [563,] -10.96161 5.074907e-06 0.005512803
##  [564,] -12.22465 2.076538e-05 0.006058816
##  [565,] -11.51237 2.297289e-05 0.005595010
##  [566,] -11.00904 1.955990e-05 0.005365240
##  [567,] -11.65071 2.264323e-05 0.005693489
##  [568,] -11.74024 2.481003e-05 0.005674937
##  [569,] -11.73375 2.185828e-05 0.005644428
##  [570,] -10.99994 1.726464e-05 0.005387105
##  [571,] -11.63733 2.238201e-05 0.005703240
##  [572,] -11.54171 2.931333e-05 0.005491206
##  [573,] -11.88061 2.149403e-05 0.005864090
##  [574,] -11.68935 1.336752e-05 0.005792780
##  [575,] -11.36013 2.222406e-05 0.005500326
##  [576,] -12.36162 3.345592e-05 0.005940863
##  [577,] -10.93967 1.410897e-05 0.005354492
##  [578,] -11.68194 2.402836e-05 0.005714065
##  [579,] -11.00591 1.772229e-05 0.005406633
##  [580,] -11.59706 2.354631e-05 0.005614694
##  [581,] -10.78780 2.098751e-05 0.005245872
##  [582,] -11.80121 2.052251e-05 0.005861617
##  [583,] -12.10532 2.481617e-05 0.005874831
##  [584,] -11.93452 2.256277e-05 0.005797141
##  [585,] -11.02872 2.105255e-05 0.005358867
##  [586,] -10.97609 1.395848e-05 0.005487902
##  [587,] -12.15713 2.841873e-05 0.005801796
##  [588,] -11.86170 2.580883e-05 0.005709892
##  [589,] -11.32725 2.303030e-05 0.005506829
##  [590,] -11.96439 2.631488e-05 0.005765530
##  [591,] -11.93460 1.521739e-05 0.005956598
##  [592,] -11.93671 2.740483e-05 0.005734239
##  [593,] -11.64304 1.844390e-05 0.005712115
##  [594,] -11.77215 1.971814e-05 0.005782162
##  [595,] -11.48334 1.692150e-05 0.005684331
##  [596,] -12.02810 2.855254e-05 0.005791777
##  [597,] -11.42861 2.366978e-05 0.005514580
##  [598,] -11.72392 2.177079e-05 0.005756736
##  [599,] -11.32386 2.287305e-05 0.005513240
##  [600,] -11.05519 1.496820e-05 0.005516397
##  [601,] -11.10562 2.481301e-05 0.005315786
##  [602,] -11.53318 2.071186e-05 0.005626413
##  [603,] -11.48147 1.169826e-05 0.005770259
##  [604,] -11.73953 2.068014e-05 0.005767129
##  [605,] -11.11130 2.129740e-05 0.005345497
##  [606,] -11.90224 2.392521e-05 0.005756958
##  [607,] -12.12825 1.819755e-05 0.006065492
##  [608,] -12.08056 2.598535e-05 0.005829253
##  [609,] -12.24005 2.537965e-05 0.005945460
##  [610,] -10.99221 1.364401e-05 0.005478992
##  [611,] -12.44189 2.517887e-05 0.006101762
##  [612,] -12.06032 2.581137e-05 0.005847905
##  [613,] -12.09922 1.720755e-05 0.006041381
##  [614,] -11.86248 1.564818e-05 0.005884829
##  [615,] -11.28025 1.789092e-05 0.005543215
##  [616,] -12.32326 2.922004e-05 0.005942452
##  [617,] -11.23447 2.171191e-05 0.005440903
##  [618,] -11.28226 1.821648e-05 0.005489138
##  [619,] -12.36818 2.961761e-05 0.005994053
##  [620,] -11.82647 2.410191e-05 0.005746852
##  [621,] -11.90226 1.867188e-05 0.005862745
##  [622,] -11.30100 2.413861e-05 0.005496312
##  [623,] -11.40378 1.905144e-05 0.005575021
##  [624,] -10.81423 1.576379e-05 0.005352432
##  [625,] -11.46464 1.203964e-05 0.005766948
##  [626,] -11.11471 2.085686e-05 0.005361532
##  [627,] -11.81772 1.583663e-05 0.005940681
##  [628,] -11.69717 2.165777e-05 0.005685862
##  [629,] -10.77176 2.105988e-05 0.005183405
##  [630,] -12.29957 2.667686e-05 0.006016989
##  [631,] -11.68442 2.335205e-05 0.005661072
##  [632,] -12.36109 2.327503e-05 0.006125037
##  [633,] -11.12395 2.049802e-05 0.005378345
##  [634,] -11.14712 1.179168e-05 0.005596042
##  [635,] -11.77429 2.251534e-05 0.005711253
##  [636,] -12.28138 2.574775e-05 0.006011550
##  [637,] -12.45692 3.003682e-05 0.006046788
##  [638,] -11.46272 2.518923e-05 0.005470478
##  [639,] -11.39871 1.625815e-05 0.005709298
##  [640,] -11.42152 1.873386e-05 0.005560533
##  [641,] -12.20225 2.282584e-05 0.006008578
##  [642,] -11.19290 1.571759e-05 0.005653549
##  [643,] -11.36510 2.350035e-05 0.005480681
##  [644,] -11.75506 2.528328e-05 0.005679273
##  [645,] -11.30429 2.196716e-05 0.005452399
##  [646,] -11.17036 1.621507e-05 0.005516429
##  [647,] -11.29631 1.687056e-05 0.005579785
##  [648,] -11.79538 2.600264e-05 0.005724411
##  [649,] -11.08699 1.073397e-05 0.005534161
##  [650,] -12.25914 2.003969e-05 0.006103083
##  [651,] -11.45299 1.478737e-05 0.005689960
##  [652,] -11.74628 2.043064e-05 0.005793504
##  [653,] -11.89821 2.369231e-05 0.005848822
##  [654,] -12.22937 2.050390e-05 0.006098967
##  [655,] -11.99259 2.653801e-05 0.005765474
##  [656,] -11.16075 2.657888e-05 0.005400349
##  [657,] -11.93193 1.757331e-05 0.005918610
##  [658,] -11.92111 2.211405e-05 0.005826527
##  [659,] -12.12785 2.117540e-05 0.005993039
##  [660,] -11.22670 1.488292e-05 0.005630929
##  [661,] -12.05376 2.292001e-05 0.005952582
##  [662,] -10.91025 1.640790e-05 0.005358973
##  [663,] -11.12426 1.527326e-05 0.005538309
##  [664,] -11.79482 3.021246e-05 0.005588212
##  [665,] -12.06857 3.159073e-05 0.005744026
##  [666,] -11.74203 1.944992e-05 0.005773991
##  [667,] -12.35968 2.864189e-05 0.005990596
##  [668,] -11.50483 2.375772e-05 0.005463107
##  [669,] -12.04423 2.373223e-05 0.005861508
##  [670,] -11.74006 1.980966e-05 0.005791287
##  [671,] -11.02957 2.077820e-05 0.005301418
##  [672,] -11.68335 2.337407e-05 0.005700590
##  [673,] -11.53034 2.598528e-05 0.005630180
##  [674,] -10.93493 1.257829e-05 0.005415462
##  [675,] -11.89752 2.106687e-05 0.005844084
##  [676,] -10.84012 2.222330e-05 0.005148692
##  [677,] -11.54338 1.616478e-05 0.005729361
##  [678,] -11.10714 1.449275e-05 0.005522579
##  [679,] -12.66843 3.146178e-05 0.006106868
##  [680,] -11.38583 1.453146e-05 0.005743499
##  [681,] -11.49727 2.148164e-05 0.005588788
##  [682,] -11.62091 1.320329e-05 0.005901961
##  [683,] -11.72748 1.965773e-05 0.005780094
##  [684,] -11.43933 2.438985e-05 0.005478209
##  [685,] -11.08622 2.291228e-05 0.005316738
##  [686,] -11.13282 1.684525e-05 0.005540921
##  [687,] -11.59369 2.813137e-05 0.005527032
##  [688,] -10.33242 1.305933e-05 0.005073875
##  [689,] -11.35093 2.162243e-05 0.005554885
##  [690,] -11.62252 2.555319e-05 0.005616225
##  [691,] -11.08634 2.133621e-05 0.005370081
##  [692,] -11.82935 2.211776e-05 0.005805824
##  [693,] -11.22188 2.470147e-05 0.005404187
##  [694,] -10.88452 1.162470e-05 0.005490865
##  [695,] -10.93307 1.473261e-05 0.005420703
##  [696,] -11.86213 2.951010e-05 0.005721634
##  [697,] -11.94203 2.300971e-05 0.005847481
##  [698,] -12.06736 2.157876e-05 0.005942344
##  [699,] -11.05995 2.557394e-05 0.005257268
##  [700,] -11.59128 1.148413e-05 0.005886770
##  [701,] -11.29712 2.268380e-05 0.005430551
##  [702,] -11.26784 1.401005e-05 0.005523328
##  [703,] -11.07284 2.230566e-05 0.005343332
##  [704,] -11.42312 1.945644e-05 0.005656623
##  [705,] -11.10871 1.754043e-05 0.005484444
##  [706,] -12.16340 2.475562e-05 0.005933259
##  [707,] -10.73239 1.671897e-05 0.005274372
##  [708,] -10.96399 1.947548e-05 0.005275526
##  [709,] -12.40161 3.209592e-05 0.005890209
##  [710,] -11.92767 2.784729e-05 0.005818245
##  [711,] -11.30165 2.374668e-05 0.005389450
##  [712,] -11.99870 2.644347e-05 0.005841917
##  [713,] -11.91922 2.202923e-05 0.005819220
##  [714,] -11.77238 1.473490e-05 0.005888534
##  [715,] -11.88360 1.722063e-05 0.005914264
##  [716,] -11.26252 1.970537e-05 0.005470753
##  [717,] -11.28974 5.858758e-06 0.005759810
##  [718,] -11.59548 1.723240e-05 0.005779880
##  [719,] -11.86978 1.928584e-05 0.005875640
##  [720,] -11.52981 1.712859e-05 0.005737589
##  [721,] -10.84402 2.322426e-05 0.005165687
##  [722,] -11.49892 1.731363e-05 0.005648876
##  [723,] -11.76649 1.815097e-05 0.005912779
##  [724,] -11.72248 1.907106e-05 0.005749557
##  [725,] -11.44377 2.040635e-05 0.005630722
##  [726,] -10.71134 1.402840e-05 0.005282315
##  [727,] -12.62694 2.999749e-05 0.006170991
##  [728,] -11.86276 2.062144e-05 0.005728983
##  [729,] -11.65727 1.854223e-05 0.005705609
##  [730,] -11.18683 2.145902e-05 0.005435971
##  [731,] -11.46453 2.350033e-05 0.005588388
##  [732,] -11.16802 2.410777e-05 0.005334841
##  [733,] -11.70648 1.872504e-05 0.005820747
##  [734,] -11.22408 1.719322e-05 0.005501897
##  [735,] -11.63972 2.001829e-05 0.005722595
##  [736,] -11.07022 2.504421e-05 0.005333110
##  [737,] -12.31313 2.999182e-05 0.005975680
##  [738,] -11.49116 2.185368e-05 0.005587533
##  [739,] -11.42231 2.121984e-05 0.005567794
##  [740,] -10.79397 1.827712e-05 0.005231335
##  [741,] -11.69222 2.571540e-05 0.005625265
##  [742,] -11.80415 2.161369e-05 0.005782184
##  [743,] -11.58504 2.173628e-05 0.005628686
##  [744,] -11.51799 1.650185e-05 0.005773357
##  [745,] -12.24841 2.624271e-05 0.006041229
##  [746,] -11.99916 2.369609e-05 0.005921140
##  [747,] -12.18098 2.046217e-05 0.006088453
##  [748,] -11.52551 2.288735e-05 0.005634702
##  [749,] -10.93853 2.400017e-05 0.005205139
##  [750,] -10.99479 2.591945e-05 0.005226425
##  [751,] -11.33986 1.579991e-05 0.005671579
##  [752,] -11.77070 2.239857e-05 0.005741701
##  [753,] -11.90795 2.117264e-05 0.005897542
##  [754,] -11.66740 1.720322e-05 0.005845011
##  [755,] -11.73245 1.158746e-05 0.005994106
##  [756,] -11.11413 2.269757e-05 0.005369979
##  [757,] -11.19158 1.551865e-05 0.005552873
##  [758,] -11.98691 2.029398e-05 0.005915105
##  [759,] -11.76702 2.737880e-05 0.005680655
##  [760,] -12.09245 2.225952e-05 0.005952225
##  [761,] -11.87283 2.498924e-05 0.005769365
##  [762,] -11.12565 1.506928e-05 0.005573926
##  [763,] -11.36112 1.080006e-05 0.005642068
##  [764,] -11.49506 2.776153e-05 0.005473807
##  [765,] -12.17179 2.389073e-05 0.006042282
##  [766,] -11.40679 2.060238e-05 0.005596876
##  [767,] -12.64823 2.372373e-05 0.006191608
##  [768,] -12.13041 2.562885e-05 0.005925332
##  [769,] -11.39825 1.472569e-05 0.005661135
##  [770,] -11.30559 2.322394e-05 0.005459113
##  [771,] -11.12635 1.444178e-05 0.005596879
##  [772,] -12.07731 2.967488e-05 0.005716581
##  [773,] -10.93813 1.841911e-05 0.005336982
##  [774,] -12.12224 2.734723e-05 0.005840113
##  [775,] -11.38515 1.489162e-05 0.005633690
##  [776,] -11.79270 1.769379e-05 0.005779437
##  [777,] -11.88491 1.888535e-05 0.005860222
##  [778,] -11.46884 2.068927e-05 0.005657613
##  [779,] -11.49818 2.615899e-05 0.005542829
##  [780,] -11.44296 2.549131e-05 0.005535342
##  [781,] -12.17433 3.136094e-05 0.005807490
##  [782,] -11.13345 1.589633e-05 0.005476611
##  [783,] -11.59862 1.983035e-05 0.005737987
##  [784,] -11.47813 2.273182e-05 0.005595878
##  [785,] -11.00007 1.720186e-05 0.005395290
##  [786,] -11.56283 2.523328e-05 0.005472952
##  [787,] -11.02221 1.840539e-05 0.005375012
##  [788,] -11.14229 1.151930e-05 0.005513537
##  [789,] -12.00714 2.311618e-05 0.005800385
##  [790,] -11.62666 1.941734e-05 0.005763646
##  [791,] -11.77981 1.988115e-05 0.005742854
##  [792,] -11.61564 2.081905e-05 0.005635549
##  [793,] -11.25056 2.391344e-05 0.005454793
##  [794,] -11.33142 1.195685e-05 0.005685366
##  [795,] -11.81436 1.873733e-05 0.005849542
##  [796,] -10.70986 1.760495e-05 0.005130101
##  [797,] -11.40002 1.981956e-05 0.005540219
##  [798,] -11.75784 1.746159e-05 0.005877879
##  [799,] -11.50194 2.710523e-05 0.005466057
##  [800,] -11.85245 2.223833e-05 0.005797634
##  [801,] -11.55049 2.173287e-05 0.005632906
##  [802,] -11.24262 2.455429e-05 0.005515202
##  [803,] -11.83628 2.192565e-05 0.005834191
##  [804,] -11.50064 2.044283e-05 0.005586242
##  [805,] -11.43452 2.377182e-05 0.005538780
##  [806,] -11.06269 1.749330e-05 0.005356152
##  [807,] -11.12055 2.210204e-05 0.005295185
##  [808,] -11.50854 1.127329e-05 0.005847695
##  [809,] -12.27621 2.391082e-05 0.006058004
##  [810,] -12.03600 3.074995e-05 0.005747886
##  [811,] -11.21783 1.650416e-05 0.005504043
##  [812,] -11.43749 2.432636e-05 0.005504231
##  [813,] -11.06813 1.859519e-05 0.005463909
##  [814,] -11.61123 2.093897e-05 0.005689363
##  [815,] -11.56670 2.560144e-05 0.005600076
##  [816,] -11.45646 1.456627e-05 0.005641859
##  [817,] -12.23544 2.909007e-05 0.006023367
##  [818,] -11.38762 2.268772e-05 0.005537335
##  [819,] -11.80519 2.301881e-05 0.005691019
##  [820,] -11.98568 1.844173e-05 0.005976560
##  [821,] -11.61375 2.257247e-05 0.005738292
##  [822,] -12.11114 2.344967e-05 0.006006875
##  [823,] -11.51557 2.040475e-05 0.005680214
##  [824,] -12.05315 2.575989e-05 0.005897025
##  [825,] -12.13513 1.712936e-05 0.006107924
##  [826,] -10.76210 1.833773e-05 0.005232554
##  [827,] -11.51687 2.031619e-05 0.005648315
##  [828,] -11.77290 2.417748e-05 0.005725697
##  [829,] -11.85279 2.778510e-05 0.005668951
##  [830,] -11.35825 2.354704e-05 0.005465144
##  [831,] -12.05384 2.799681e-05 0.005708859
##  [832,] -11.69389 2.351032e-05 0.005678865
##  [833,] -11.33326 2.353439e-05 0.005448989
##  [834,] -11.53723 1.767150e-05 0.005754882
##  [835,] -11.26243 1.652062e-05 0.005535317
##  [836,] -11.72526 2.244555e-05 0.005735041
##  [837,] -11.34374 2.505195e-05 0.005424224
##  [838,] -11.02794 1.561160e-05 0.005499275
##  [839,] -11.11682 2.298435e-05 0.005292952
##  [840,] -10.91214 1.051856e-05 0.005442633
##  [841,] -11.47761 2.068327e-05 0.005636257
##  [842,] -11.27918 1.648936e-05 0.005470695
##  [843,] -11.38772 1.792909e-05 0.005628128
##  [844,] -11.60971 2.398537e-05 0.005652787
##  [845,] -11.58694 2.713833e-05 0.005541126
##  [846,] -11.18659 1.754565e-05 0.005547332
##  [847,] -11.74358 2.414072e-05 0.005703218
##  [848,] -11.04563 1.300129e-05 0.005470634
##  [849,] -10.99309 1.851610e-05 0.005438709
##  [850,] -11.54688 1.990188e-05 0.005636349
##  [851,] -11.53598 1.798519e-05 0.005661827
##  [852,] -11.40015 2.454806e-05 0.005501371
##  [853,] -10.62296 4.924156e-06 0.005366747
##  [854,] -11.46873 2.142633e-05 0.005632774
##  [855,] -10.93230 2.008675e-05 0.005235302
##  [856,] -11.89876 8.220834e-06 0.006046425
##  [857,] -11.40137 2.107995e-05 0.005532457
##  [858,] -11.67590 2.513374e-05 0.005635219
##  [859,] -12.68091 2.258632e-05 0.006341275
##  [860,] -11.20185 1.978624e-05 0.005492001
##  [861,] -11.43500 2.651359e-05 0.005448100
##  [862,] -11.39493 1.322801e-05 0.005773055
##  [863,] -12.42217 3.084589e-05 0.005863858
##  [864,] -11.25702 2.172958e-05 0.005488224
##  [865,] -11.97907 1.943286e-05 0.005915503
##  [866,] -11.33969 1.611100e-05 0.005672548
##  [867,] -11.32301 1.928859e-05 0.005493143
##  [868,] -11.24078 1.359373e-05 0.005630317
##  [869,] -11.12272 1.712176e-05 0.005448383
##  [870,] -12.05285 1.493938e-05 0.006137442
##  [871,] -11.59627 1.918451e-05 0.005692910
##  [872,] -11.68762 3.601895e-05 0.005441959
##  [873,] -11.72221 2.605418e-05 0.005603856
##  [874,] -11.56885 2.219500e-05 0.005539292
##  [875,] -11.64677 2.064074e-05 0.005635163
##  [876,] -11.54759 2.138268e-05 0.005674275
##  [877,] -11.52077 2.025628e-05 0.005627107
##  [878,] -11.38049 1.941284e-05 0.005502799
##  [879,] -11.46004 1.975114e-05 0.005673394
##  [880,] -11.74228 1.802693e-05 0.005830845
##  [881,] -11.11030 1.763538e-05 0.005433566
##  [882,] -11.26165 2.011347e-05 0.005455932
##  [883,] -11.24577 1.837802e-05 0.005546472
##  [884,] -11.78030 2.270998e-05 0.005703271
##  [885,] -11.48507 2.231689e-05 0.005601138
##  [886,] -11.73308 1.780123e-05 0.005821349
##  [887,] -11.69820 1.925872e-05 0.005701966
##  [888,] -11.64408 1.414439e-05 0.005817319
##  [889,] -11.11553 2.545751e-05 0.005402064
##  [890,] -11.27303 2.463939e-05 0.005458408
##  [891,] -11.38457 2.061166e-05 0.005589597
##  [892,] -11.69144 1.866457e-05 0.005752235
##  [893,] -11.55820 1.692606e-05 0.005712310
##  [894,] -11.71589 1.771489e-05 0.005764527
##  [895,] -11.83511 2.068747e-05 0.005933185
##  [896,] -11.51432 2.303584e-05 0.005640845
##  [897,] -10.84729 1.776066e-05 0.005336426
##  [898,] -11.84847 1.891449e-05 0.005949537
##  [899,] -11.32749 1.713761e-05 0.005547675
##  [900,] -10.62119 1.365297e-05 0.005245773
##  [901,] -11.24933 1.793208e-05 0.005535796
##  [902,] -12.24969 2.724502e-05 0.005903968
##  [903,] -11.74043 1.933316e-05 0.005811597
##  [904,] -11.02315 1.961226e-05 0.005333983
##  [905,] -11.36423 2.530594e-05 0.005455034
##  [906,] -11.11433 1.671215e-05 0.005497848
##  [907,] -11.57937 1.434714e-05 0.005812010
##  [908,] -11.95868 2.456191e-05 0.005781521
##  [909,] -11.87512 2.817198e-05 0.005630823
##  [910,] -11.53939 1.964464e-05 0.005679449
##  [911,] -11.67812 1.718125e-05 0.005742664
##  [912,] -11.34794 2.514476e-05 0.005477341
##  [913,] -11.21313 1.252725e-05 0.005658137
##  [914,] -12.28109 2.238608e-05 0.006042001
##  [915,] -11.44728 1.166421e-05 0.005783608
##  [916,] -12.18540 2.345494e-05 0.006047851
##  [917,] -12.67061 2.472844e-05 0.006197083
##  [918,] -11.67239 1.862822e-05 0.005744654
##  [919,] -12.13125 2.205995e-05 0.006020976
##  [920,] -12.29596 2.616979e-05 0.006015067
##  [921,] -11.14209 1.600317e-05 0.005450352
##  [922,] -11.52133 1.730616e-05 0.005656325
##  [923,] -11.36541 1.893291e-05 0.005588119
##  [924,] -11.49901 1.525302e-05 0.005723118
##  [925,] -12.01302 2.281437e-05 0.005827843
##  [926,] -11.10442 1.902488e-05 0.005490235
##  [927,] -10.84044 1.524046e-05 0.005319385
##  [928,] -11.61519 1.701195e-05 0.005774563
##  [929,] -11.10706 1.700477e-05 0.005561118
##  [930,] -11.83975 2.438457e-05 0.005741854
##  [931,] -10.62831 2.378918e-05 0.005067587
##  [932,] -12.05179 1.665674e-05 0.005996739
##  [933,] -11.04036 1.235462e-05 0.005508635
##  [934,] -11.66048 1.923012e-05 0.005774870
##  [935,] -11.91232 1.949465e-05 0.005873372
##  [936,] -11.36624 1.688132e-05 0.005650961
##  [937,] -11.60356 1.751300e-05 0.005829804
##  [938,] -11.67392 2.080568e-05 0.005785545
##  [939,] -11.66535 1.908009e-05 0.005760310
##  [940,] -11.62024 2.567214e-05 0.005553360
##  [941,] -11.68235 2.662618e-05 0.005631466
##  [942,] -11.55173 2.744982e-05 0.005516770
##  [943,] -11.80158 2.492801e-05 0.005814181
##  [944,] -11.13887 2.360597e-05 0.005287631
##  [945,] -11.89536 2.565329e-05 0.005753994
##  [946,] -11.13444 1.286406e-05 0.005549450
##  [947,] -11.21194 2.246583e-05 0.005400300
##  [948,] -11.30308 2.254392e-05 0.005460604
##  [949,] -11.41655 1.890115e-05 0.005609463
##  [950,] -11.50484 2.477361e-05 0.005519206
##  [951,] -11.56297 2.228570e-05 0.005622226
##  [952,] -11.05989 1.464251e-05 0.005466058
##  [953,] -11.94466 1.902988e-05 0.005902711
##  [954,] -12.26260 2.063055e-05 0.006048596
##  [955,] -12.49597 2.588979e-05 0.006166477
##  [956,] -10.87137 9.468391e-06 0.005442962
##  [957,] -10.76399 1.336062e-05 0.005328393
##  [958,] -12.69119 2.745981e-05 0.006169873
##  [959,] -11.50234 2.235135e-05 0.005612275
##  [960,] -11.08955 1.353609e-05 0.005485778
##  [961,] -12.05901 2.150757e-05 0.005899577
##  [962,] -11.39315 1.900396e-05 0.005604669
##  [963,] -11.14940 1.549511e-05 0.005547755
##  [964,] -12.01771 1.843703e-05 0.006048712
##  [965,] -11.08406 1.590653e-05 0.005489368
##  [966,] -11.45527 1.740185e-05 0.005634788
##  [967,] -10.93948 2.331726e-05 0.005170089
##  [968,] -11.30276 2.347935e-05 0.005508191
##  [969,] -10.04836 3.817465e-06 0.004991788
##  [970,] -11.60983 2.386374e-05 0.005619864
##  [971,] -11.35250 2.240752e-05 0.005474919
##  [972,] -11.44675 1.851128e-05 0.005652873
##  [973,] -12.00653 1.667869e-05 0.006085265
##  [974,] -11.88065 2.091892e-05 0.005924862
##  [975,] -11.59878 2.459644e-05 0.005614826
##  [976,] -11.07240 1.967854e-05 0.005378635
##  [977,] -11.38624 1.571426e-05 0.005645551
##  [978,] -11.85149 3.003676e-05 0.005678659
##  [979,] -11.29560 1.205860e-05 0.005720547
##  [980,] -11.16124 2.098949e-05 0.005502613
##  [981,] -12.74151 3.414701e-05 0.006151904
##  [982,] -11.55594 1.764493e-05 0.005717488
##  [983,] -11.73059 2.898090e-05 0.005601097
##  [984,] -12.38095 2.609895e-05 0.006060249
##  [985,] -11.20616 1.233738e-05 0.005551240
##  [986,] -10.83433 1.747392e-05 0.005278965
##  [987,] -11.85544 1.958433e-05 0.005975962
##  [988,] -11.64793 3.184873e-05 0.005433662
##  [989,] -11.58168 2.677302e-05 0.005581749
##  [990,] -10.98649 1.807392e-05 0.005302143
##  [991,] -11.38912 1.692578e-05 0.005622885
##  [992,] -11.07571 1.479917e-05 0.005469774
##  [993,] -11.13241 8.283038e-06 0.005691142
##  [994,] -11.98391 2.687956e-05 0.005810610
##  [995,] -11.55535 1.619631e-05 0.005769484
##  [996,] -11.98631 2.579259e-05 0.005801199
##  [997,] -11.89603 2.521722e-05 0.005689304
##  [998,] -11.14578 1.807392e-05 0.005474952
##  [999,] -11.91552 2.177120e-05 0.005855273
## [1000,] -11.54617 1.977961e-05 0.005719658

(d) Comment on the results

In this case, it seems that the bootstrap standard errors are generally slightly larger, indicating more variability. This may suggest that the assumptions of the standard formula used in the glm() function are reasonable but might slightly underestimate the true variability.

The use of the bootstrap method provides additional insights into the stability of the coefficient estimates. If the bootstrap standard errors are substantially different from those obtained with the glm() function, it may signal potential issues with the assumptions of the standard errors calculated by the glm() function.

If the standard errors from the bootstrap method are in the same order of magnitude and provide similar estimates compared to those from the glm() function, it suggests consistency in the estimation.

7. In Sections 5.3.2 and 5.3.3, we saw that the cv.glm() function can be used in order to compute the LOOCV test error estimate. Alternatively, one could compute those quantities using just the glm() and predict.glm() functions, and a for loop. You will now take this approach in order to compute the LOOCV error for a simple logistic regression model on the Weekly data set. Recall that in the context of classification problems, the LOOCV error is given in (5.4).

(a) Fit a logistic regression model that predicts Direction using Lag1 and Lag2.

(b) Fit a logistic regression model that predicts Direction using Lag1 and Lag2 using all but the first observation.

(c) Use the model from (b) to predict the direction of the first observation. You can do this by predicting that the first observation will go up if P(Direction = “Up”|Lag1, Lag2) > 0.5. Was this observation correctly classified?

(d) Write a for loop from i =1to i = n, where n is the number of observations in the data set, that performs each of the following steps:

i. Fit a logistic regression model using all but the ith observation to predict Direction using Lag1 and Lag2.

ii. Compute the posterior probability of the market moving up for the ith observation.

iii. Use the posterior probability for the ith observation in order to predict whether or not the market moves up.

iv. Determine whether or not an error was made in predicting the direction for the ith observation. If an error was made, then indicate this as a 1, and otherwise indicate it as a 0.

(e) Take the average of the n numbers obtained in (d)iv in order to obtain the LOOCV estimate for the test error. Comment on the results.

Answer:

(a) Fit a logistic regression model predicting Direction using Lag1 and Lag2

data(Weekly)
model <- glm(Direction ~ Lag1 + Lag2, data = Weekly, family = "binomial")

(b) Fit a logistic regression model using all but the first observation

model_exclude_first <- glm(Direction ~ Lag1 + Lag2, data = Weekly[-1, ], family = "binomial")

(c) Predict the direction of the first observation and check if it was correctly classified

prediction_first <- predict(model_exclude_first, newdata = Weekly[1, ], type = "response")

predicted_direction <- ifelse(prediction_first > 0.5, "Up", "Down")

correctly_classified <- predicted_direction == Weekly$Direction[1]

cat("First observation correctly classified:", correctly_classified, "\n")
## First observation correctly classified: FALSE

(d) Write a for loop to perform LOOCV

errors <- numeric(nrow(Weekly))

for (i in 1:nrow(Weekly)) {

  model_loocv <- glm(Direction ~ Lag1 + Lag2, data = Weekly[-i, ], family = "binomial")

  prediction_loocv <- predict(model_loocv, newdata = Weekly[i, ], type = "response")

  predicted_direction_loocv <- ifelse(prediction_loocv > 0.5, "Up", "Down")

  errors[i] <- as.numeric(predicted_direction_loocv != Weekly$Direction[i])
}

errors
##    [1] 1 1 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0 1 0 0 0 1 1 1 1 1 1 0 1 1 1 1 0 1 0 0
##   [38] 0 1 0 1 0 0 1 0 1 1 1 0 1 0 0 0 1 0 0 1 1 0 0 0 0 1 0 1 1 0 0 1 0 1 1 0 0
##   [75] 0 1 0 1 1 0 0 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 0 0 0 1 0 1 1 0 0 1 0 1 0 0 1
##  [112] 1 0 0 1 0 0 1 0 0 1 1 1 1 0 0 0 1 0 1 0 1 1 0 0 0 1 1 1 0 0 0 1 0 0 0 0 0
##  [149] 0 1 1 1 0 1 0 0 1 1 0 1 0 0 1 1 0 0 1 0 0 1 0 0 1 1 1 0 1 0 1 0 0 0 0 0 0
##  [186] 0 0 1 1 0 1 0 1 0 1 0 1 0 0 1 0 0 1 0 0 1 0 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1
##  [223] 0 0 0 1 1 1 0 1 0 1 0 1 0 0 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 0 1 0 0 1 0
##  [260] 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 1 0 0 1 0 0 1 0 0 1 0 1 1 0 0 0 0 0 1 0 1 0
##  [297] 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 0 1 0 1 1 0 0 1 0 1 0 1 1 0 0 0 1 0 1 0 0
##  [334] 1 1 1 1 0 1 0 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 1 0 0 1 0 0 1 0 0 0 1 1 0 1
##  [371] 1 1 1 1 0 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 1 1 0 0 0 0 0 1 0 0 1 1 1 0 1 0 1
##  [408] 0 1 1 1 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 0 0 1 0 1 0 0 0 0 0 1 1 1 1
##  [445] 0 1 1 0 1 0 1 1 0 1 0 0 1 0 0 1 1 0 0 0 0 1 1 0 0 1 0 1 0 0 0 1 0 0 1 0 0
##  [482] 0 1 1 1 0 1 0 0 0 1 0 1 1 1 0 0 0 0 1 1 1 0 1 1 0 1 0 0 0 1 0 1 0 0 0 1 0
##  [519] 1 1 0 0 1 1 0 0 0 1 1 0 1 0 1 1 1 1 1 0 0 0 1 0 0 0 1 1 0 1 0 0 0 1 1 1 1
##  [556] 1 1 0 1 0 1 0 0 1 0 0 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 0 0 1 0 1
##  [593] 0 0 1 0 0 1 0 1 1 0 1 1 1 0 1 0 1 0 1 0 0 0 1 0 1 0 1 0 1 1 0 1 1 0 1 0 1
##  [630] 0 1 1 1 1 0 1 1 0 0 0 1 1 1 1 0 1 1 1 0 1 0 0 0 1 1 1 1 1 1 0 1 0 0 1 0 0
##  [667] 0 1 1 0 1 0 1 1 1 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 1 1
##  [704] 0 0 0 0 1 0 1 0 1 0 1 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 1 1 0 1 1 0
##  [741] 1 1 1 1 0 0 0 1 1 1 1 1 1 0 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 1 0 0 1 0 0 0 1
##  [778] 1 1 0 0 0 1 0 0 1 1 1 0 0 1 0 1 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 1 1 0 0 1 1
##  [815] 0 1 1 1 0 0 0 0 0 1 1 0 0 1 0 0 1 0 1 0 0 0 1 1 0 1 1 0 1 0 1 0 1 1 0 0 1
##  [852] 1 1 0 1 1 0 0 0 1 0 1 0 1 0 1 0 0 0 0 0 1 0 0 1 1 0 0 1 0 1 0 1 1 0 1 0 1
##  [889] 1 0 0 0 1 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 1 1 1 1 1 0 1 1 0 0 0 0 0 1 0 0 1
##  [926] 0 0 0 0 1 0 1 1 1 0 0 1 1 0 1 1 1 1 0 1 0 1 0 1 0 1 0 1 0 0 1 1 1 1 1 0 1
##  [963] 0 0 0 1 1 1 0 1 1 1 1 0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 1 0 0 1 1 1 0 1 0 0 0
## [1000] 0 1 0 0 1 0 1 0 0 1 1 1 1 0 1 0 0 1 0 0 1 0 0 1 1 0 1 1 1 0 1 1 0 0 0 1 0
## [1037] 1 0 1 1 1 1 0 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 1 0 1 1 1 0 0
## [1074] 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0

(e) Calculate LOOCV estimate for test error

loocv_error <- mean(errors)
cat("LOOCV Estimate for Test Error:", loocv_error, "\n")
## LOOCV Estimate for Test Error: 0.4499541

The LOOCV estimate for the test error is approximately 0.45, indicating that the logistic regression model, which predicts the direction of the market using Lag1 and Lag2, has a relatively high test error. This means that, on average, the model misclassifies about 45% of the observations when predicting the direction of the market movement.

9. We will now consider the Boston housing data set, from the ISLR2 library.

(a) Based on this data set, provide an estimate for the population mean of medv. Call this estimate ˆ µ.

(b) Provide an estimate of the standard error of ˆ µ. Interpret this result.

Hint: We can compute the standard error of the sample mean by dividing the sample standard deviation by the square root of the number of observations.

(c) Now estimate the standard error of ˆ µ using the bootstrap. How does this compare to your answer from (b)?

(d) Based on your bootstrap estimate from (c), provide a 95% confidence interval for the mean of medv. Compare it to the results obtained using t.test(Boston$medv). Hint: You can approximate a 95% confidence interval using the formula [ˆ µ 2SE(ˆ µ), ˆ µ + 2SE(ˆ µ)].

(e) Based on this data set, provide an estimate, ˆ µmed, for the median value of medv in the population.

(f) Wenowwouldlike to estimate the standard error of ˆ µmed. Unfortunately, there is no simple formula for computing the standard error of the median. Instead, estimate the standard error of the median using the bootstrap. Comment on your findings.

(g) Based on this data set, provide an estimate for the tenth percentile of medv in Boston census tracts. Call this quantity ˆ µ0.1. (You can use the quantile() function.)

(h) Use the bootstrap to estimate the standard error of ˆ µ0.1. Comment on your findings.

Answer:

(a) Estimate for the population mean of medv

data(Boston)
mean_estimate <- mean(Boston$medv)
mean_estimate
## [1] 22.53281

(b) Estimate of the standard error of the sample mean

se_estimate <- sd(Boston$medv) / sqrt(length(Boston$medv))
se_estimate
## [1] 0.4088611

The standard error estimate of approximately 0.409 suggests that, based on the sample of the Boston housing dataset, the sample mean of the medv variable is expected to vary by around 0.409 units from the true population mean. A smaller standard error indicates less variability in the sample mean and increases our confidence that the sample mean is a reliable estimate of the population mean.

(c) Estimate the standard error of the sample mean using the bootstrap

bootstrap_mean <- function(data, n) {
  mean_values <- replicate(n, mean(sample(data, replace = TRUE)))
  se_bootstrap <- sd(mean_values)
  return(se_bootstrap)
}
bootstrap_se_estimate <- bootstrap_mean(Boston$medv, 1000)
bootstrap_se_estimate
## [1] 0.4113177
  • The standard error obtained in (b) is based on the assumption that the sample is a representative sample of the population.

  • The standard error obtained in (c) is based on resampling from the observed data, allowing for a more robust estimate when the sample size is limited.

In this case, the small difference between the two standard errors indicates that the assumption made in (b) about the sample’s representativeness is likely valid. However, the bootstrap estimate in (c) provides additional insight into the variability of the sample mean, considering the observed data’s structure.

(d) 95% confidence interval for the mean of medv using the bootstrap

lower_ci <- mean_estimate - 2 * bootstrap_se_estimate
upper_ci <- mean_estimate + 2 * bootstrap_se_estimate

cat("95% Confidence Interval:", lower_ci, "-", upper_ci, "\n")
## 95% Confidence Interval: 21.71017 - 23.35544

Comparison to t.test:

t_test_result <- t.test(Boston$medv)
conf_interval_t_test <- t_test_result$conf.int

cat("95% Confidence Interval (t.test):", conf_interval_t_test, "\n")
## 95% Confidence Interval (t.test): 21.72953 23.33608

The two confidence intervals are very close to each other, indicating good agreement between the bootstrap method and the t.test. Both methods provide similar ranges for the population mean, suggesting consistency in their estimates.

(e) Estimate for the median value of medv in the population

median_estimate <- median(Boston$medv)
median_estimate
## [1] 21.2

(f) Estimate the standard error of the median using the bootstrap

bootstrap_median <- function(data, n) {
  median_values <- replicate(n, median(sample(data, replace = TRUE)))
  se_bootstrap <- sd(median_values)
  return(se_bootstrap)
}

bootstrap_se_median_estimate <- bootstrap_median(Boston$medv, 1000)
bootstrap_se_median_estimate
## [1] 0.3723594

The standard error of the median provides a measure of the variability in the estimate of the population median. In this case, the value of 0.3723594 indicates the average amount by which the sample median might vary from the true population median if we were to take multiple bootstrap samples.

(g) Estimate for the tenth percentile of medv

tenth_percentile_estimate <- quantile(Boston$medv, 0.1)
tenth_percentile_estimate
##   10% 
## 12.75

(h) Estimate the standard error of the tenth percentile using the bootstrap

bootstrap_tenth_percentile <- function(data, n) {
  tenth_percentile_values <- replicate(n, quantile(sample(data, replace = TRUE), 0.1))
  se_bootstrap <- sd(tenth_percentile_values)
  return(se_bootstrap)
}

bootstrap_se_tenth_percentile <- bootstrap_tenth_percentile(Boston$medv, 1000)
bootstrap_se_tenth_percentile
## [1] 0.4893239

The standard error of the tenth percentile using the bootstrap (bootstrap_se_tenth_percentile) is approximately 0.4893239. This value represents the variability in estimating the tenth percentile through resampling.