The purpose of this R lab is to have you practice testing assumptions, interpretting output, practicing ways to deal with violations of assumptions and to dip into regression and correlation

NOTE: please refer to the hypothesis testing guide document for code examples!

Part 1: Testing assumptions

Create a code chunk(s) below to bring in “barnacle_data.csv” and complete the following actions. There are questions that will follow your coding. This data comes from some of my research looking at how things like microhabitat and shore height influence the growth and abundance of barnacles.

Actions/Questions: You are interested in seeing if there is an effect of shore height (‘Tide.Height’) on the growth of individual barnacles over a 6 month period.

  1. Question = What type of test would be appropriate to run to compare the means of ‘WidthChange’ by ‘Tide.Height’? (explain why)
  1. assuming that the sample data follows the 3 assumptions, I would run a two-sample t-test. The two sample t-test is the best option because there is one numerical variable and one categorical value which only has two levels.
  1. Code = Create code to test the data for normality (for this, create a histogram AND run a Shapiro-Wilks test)
barn <- read.table('barnacle_data_2.csv',sep=',',header=T)
barn
##     Tide.Height OperculumAug BasalAug  WidthChange
## 1             H        5.258    7.098  68.01916033
## 2             H        5.615    9.256  18.65816768
## 3             H        5.483    8.289   0.97719870
## 4             H        5.065   11.578   6.36552081
## 5             H        5.229    9.563  12.21374046
## 6             H        5.645   11.291   7.41298379
## 7             H        6.367    8.750   1.06285714
## 8             H        5.740   11.798   6.32310561
## 9             H        5.714    9.890   8.24064712
## 10            H        3.821    6.072  45.25691700
## 11            H        3.642    6.293   1.43016050
## 12            H        3.971    8.611   3.79746835
## 13            H        5.323    8.826  26.50124632
## 14            H        4.708    7.784  16.00719424
## 15            H        3.472    6.029  22.64057058
## 16            H        3.235    5.521  42.25683753
## 17            H        3.881    5.820  30.20618557
## 18            H        1.848    3.368  89.60807601
## 19            H        4.865    6.001  40.19330112
## 20            H        5.073    5.813  12.98813005
## 21            H        2.857    3.483   7.69451622
## 22            H        4.771    8.944  12.94722719
## 23            H        4.422    6.262  16.33663366
## 24            H        4.384    7.715   3.30524951
## 25            H        4.898    6.371   3.32757809
## 26            H        4.722    6.874   7.53564155
## 27            H        4.261    7.039  14.98792442
## 28            H        4.604    7.817   9.49213253
## 29            H        4.558    8.892  16.64417454
## 30            H        5.303    8.467   1.29916145
## 31            H        4.723    9.030   6.36766334
## 32            H        3.758    9.147   8.54925112
## 33            H        4.908    8.703  32.64391589
## 34            H        4.361    9.779  19.20441763
## 35            H        4.348   10.218  15.02250930
## 36            H        5.249   10.126  15.40588584
## 37            H        3.956    6.552  11.75213675
## 38            H        4.457    8.149  18.33353786
## 39            H        4.164    8.804  15.54975011
## 40            H        4.088    9.363   0.58741856
## 41            H        3.656    8.790   5.77929465
## 42            H        3.755    8.400   5.83333333
## 43            H        4.026    8.218   8.42054028
## 44            H        3.663    5.287  94.15547570
## 45            H        4.284    6.053  30.81116802
## 46            H        4.672    9.284   7.71219302
## 47            H        3.911    4.594  30.40922943
## 48            L        3.470   10.315   3.46097916
## 49            L        3.215    6.568  21.26979294
## 50            L        3.957    6.979  34.46052443
## 51            L        3.936    7.290  21.38545953
## 52            L        3.585    7.513  32.31731665
## 53            L        3.778    7.208  37.19478357
## 54            L        4.316   10.376  13.97455667
## 55            L        2.288    5.088  55.09040881
## 56            L        2.874    6.361  20.40559660
## 57            L        4.518    9.353  23.69293275
## 58            L        4.056    6.387  97.96461563
## 59            L        3.256    4.375  32.96000000
## 60            L        4.436    7.321  36.64799891
## 61            L        3.643    7.522  68.67854294
## 62            L        4.566    6.641  68.31802439
## 63            L        4.213    9.063  14.17852808
## 64            L        4.324    7.733  19.28100349
## 65            L        3.185    5.127  58.31870490
## 66            L        3.725    6.541  44.10640575
## 67            L        4.116    9.121  25.60026313
## 68            L        3.921    6.626  70.55538787
## 69            L        4.979    6.601  10.37721557
## 70            L        2.364    6.376   6.80677541
## 71            L        4.557    9.280  19.11637931
## 72            L        4.576    7.057  42.39761939
## 73            L        3.583    7.816  26.30501535
## 74            L        3.693    6.758  42.14264575
## 75            L        3.823    7.797  19.72553546
## 76            L        4.175    6.908  46.56919514
## 77            L        3.328    6.613  34.59851807
## 78            L        3.654    5.344  55.03368263
## 79            L        3.686    7.859   8.57615473
## 80            L        3.533    5.623  37.86235106
## 81            L        3.686    9.099   9.42960765
## 82            L        4.994    6.844  66.17475161
## 83            L        3.100    7.648  26.15062762
## 84            L        3.083    5.081  26.51052942
## 85            L        2.828    5.793  47.00500604
## 86            L        3.687    6.926  38.75252671
## 87            L        4.464    8.429  12.43326611
## 88            L        4.864    8.745  24.64265294
## 89            L        2.037    3.750 108.72000000
## 90            L        5.148    6.868  24.24286546
## 91            L        4.257    5.239  87.57396450
## 92            L        3.977    5.342  53.80007488
## 93            L        4.934    7.455  73.72233400
## 94            L        4.966    5.677  19.88726440
## 95            L        4.376    5.645  22.90522586
## 96            L        4.022    6.343   7.11020022
## 97            H        2.384    6.582   5.49984807
## 98            H        3.128    5.800  18.65517241
## 99            H        3.553    6.453   9.29800093
## 100           H        2.941    7.115   3.17638791
## 101           H        3.146    6.465   5.35189482
## 102           H        2.529    6.094   9.92779783
## 103           H        2.915    4.429   4.01896591
## 104           H        3.541    6.797   2.16271885
## 105           H        3.425    3.791  18.93959377
## 106           H        4.016    7.637  12.89773471
## 107           H        3.388    7.166   2.73513815
## 108           H        3.244    6.179  11.02120084
## 109           H        4.471    8.479   4.03349452
## 110           H        2.721    5.915   4.64919696
## 111           H        3.434    6.937  12.90183076
## 112           H        3.390    5.634   1.77493788
## 113           H        3.811    8.301   0.69871100
## 114           H        3.058    6.829   6.97027383
## 115           H        2.996    7.893   5.38451793
## 116           H        4.033   10.004   0.58976409
## 117           H        3.271    7.576   0.19799366
## 118           H        2.459    5.039  10.00198452
## 119           H        2.848    5.411   7.54019590
## 120           H        2.355    4.924   9.44354184
## 121           H        3.226    4.617   4.80831709
## 122           H        2.908    5.836   0.97669637
## 123           H        3.438    6.758   8.34566440
## 124           H        3.102    7.255   3.37698139
## 125           H        2.905    5.495   6.31483166
## 126           H        2.899    4.488  32.10784314
## 127           H        3.664    4.989   6.41411104
## 128           H        3.953    5.095  10.08832188
## 129           H        3.598    4.718   9.11403137
## 130           H        3.712    5.000   7.10000000
## 131           H        3.676    5.525   8.30769231
## 132           H        3.814    4.937   6.94753899
## 133           H        4.335    6.064   8.04749340
## 134           H        2.721    5.204   2.01767871
## 135           H        3.482    6.168  10.68417639
## 136           H        3.303    7.058   1.50184188
## 137           H        3.404    7.095   2.42424242
## 138           H        2.901    5.130   4.69785575
## 139           H        3.687    5.945  13.45668629
## 140           H        3.338    6.304  14.87944162
## 141           H        3.770    5.416   4.33899557
## 142           H        2.937    6.022   1.77681833
## 143           H        2.711    6.399   1.40646976
## 144           L        2.469    5.757  22.14695154
## 145           L        2.647    5.063   3.00217263
## 146           L        2.806    4.918  12.99308662
## 147           L        2.790    4.488  28.14171123
## 148           L        2.245    5.398  11.98592071
## 149           L        2.118    4.392  32.74134791
## 150           L        2.735    5.837  12.78053795
## 151           L        1.824    4.437  41.04124408
## 152           L        3.062    5.167   1.43216567
## 153           L        2.146    4.936  14.12074554
## 154           L        1.572    3.365  53.87815750
## 155           L        1.839    3.950  23.69620253
## 156           L        0.533    0.815 447.36196320
## 157           L        2.175    4.860  26.37860082
## 158           L        1.954    4.473  22.11044042
## 159           L        1.974    5.062  20.20940340
## 160           L        1.667    4.271  23.46054788
## 161           L        1.476    4.098  26.32991703
## 162           L        1.771    5.867  22.75438896
## 163           L        1.698    4.231  17.08815883
## 164           L        1.763    5.341   3.83823254
## 165           L        1.527    2.157  56.00370885
## 166           L        1.982    2.923  36.09305508
## 167           L        1.875    4.359   0.06882312
## 168           L        1.890    6.340   0.25236593
## 169           L        2.277    6.037   1.07669372
## 170           L        1.648    5.127   1.28730252
## 171           L        2.453    5.178  18.38547702
## 172           L        4.374    7.607   4.78506639
## 173           L        3.775    6.709   6.48382769
## 174           L        2.963    6.629   7.82923518
## 175           L        2.642    4.595  24.06964091
## 176           L        2.385    5.413   6.96471458
## 177           L        3.030    4.794   5.67375886
## 178           L        2.977    6.953  11.79347044
## 179           L        3.971    6.916  25.80971660
## 180           L        3.162    6.733   6.00029704
## 181           L        2.911    6.126   8.60267711
## 182           L        3.107    5.147  10.80240917
## 183           L        3.691    5.390  33.06122449
## 184           L        2.939    5.633  14.27303391
## 185           L        3.300    7.230  18.25726141
## 186           L        2.886    5.893  34.29492618
## 187           L        3.375    6.912  13.94675926
## 188           L        2.870    4.840  10.59917355
## 189           L        3.403    6.239  15.98012502
## 190           L        3.013    7.053  13.05827308
## 191           L        2.502    6.042  22.04568024
shapiro.test(barn$WidthChange)
## 
##  Shapiro-Wilk normality test
## 
## data:  barn$WidthChange
## W = 0.4299, p-value < 2.2e-16
hist(barn$WidthChange)

  1. Question = what do the tests for normality tell you about the data? Is this assumption met?
  1. The shapiro test gave a p-value of 2.2e-16 which tells us to reject the null hypothesis. Meaning the data is not normally distributed. As further confirmation, the histogram is significantly skewed telling us the sample data is not normally distributed.
  1. Code = Create code to test the data for equal variance (use a Levene Test)
library(car)
## Loading required package: carData
leveneTest(WidthChange ~ Tide.Height,
          data = barn)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)  
## group   1  5.1557 0.0243 *
##       189                 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
  1. Question = what does the test for equal variance tell you about the data? Is this assumption met?
  1. In order to support the null hypothesis that the variance among the groups are equal the levene test P-value would have to be greater than 0.05. In this case, the p-value is 0.0243 therefore the null hypothesis/assumption is not met. The varience between the groups is not equal.
  1. Code = log transform the WidthChange column
barn$log_WidthChange <- log(barn$WidthChange)
barn
##     Tide.Height OperculumAug BasalAug  WidthChange log_WidthChange
## 1             H        5.258    7.098  68.01916033      4.21978944
## 2             H        5.615    9.256  18.65816768      2.92628400
## 3             H        5.483    8.289   0.97719870     -0.02306527
## 4             H        5.065   11.578   6.36552081      1.85089605
## 5             H        5.229    9.563  12.21374046      2.50256159
## 6             H        5.645   11.291   7.41298379      2.00323303
## 7             H        6.367    8.750   1.06285714      0.06096070
## 8             H        5.740   11.798   6.32310561      1.84421048
## 9             H        5.714    9.890   8.24064712      2.10907887
## 10            H        3.821    6.072  45.25691700      3.81235552
## 11            H        3.642    6.293   1.43016050      0.35778667
## 12            H        3.971    8.611   3.79746835      1.33433462
## 13            H        5.323    8.826  26.50124632      3.27719176
## 14            H        4.708    7.784  16.00719424      2.77303826
## 15            H        3.472    6.029  22.64057058      3.11974346
## 16            H        3.235    5.521  42.25683753      3.74376618
## 17            H        3.881    5.820  30.20618557      3.40804672
## 18            H        1.848    3.368  89.60807601      4.49544545
## 19            H        4.865    6.001  40.19330112      3.69370034
## 20            H        5.073    5.813  12.98813005      2.56403587
## 21            H        2.857    3.483   7.69451622      2.04050790
## 22            H        4.771    8.944  12.94722719      2.56088165
## 23            H        4.422    6.262  16.33663366      2.79341005
## 24            H        4.384    7.715   3.30524951      1.19551197
## 25            H        4.898    6.371   3.32757809      1.20224474
## 26            H        4.722    6.874   7.53564155      2.01964397
## 27            H        4.261    7.039  14.98792442      2.70724484
## 28            H        4.604    7.817   9.49213253      2.25046330
## 29            H        4.558    8.892  16.64417454      2.81206028
## 30            H        5.303    8.467   1.29916145      0.26171902
## 31            H        4.723    9.030   6.36766334      1.85123258
## 32            H        3.758    9.147   8.54925112      2.14584369
## 33            H        4.908    8.703  32.64391589      3.48565850
## 34            H        4.361    9.779  19.20441763      2.95514034
## 35            H        4.348   10.218  15.02250930      2.70954970
## 36            H        5.249   10.126  15.40588584      2.73474963
## 37            H        3.956    6.552  11.75213675      2.46403508
## 38            H        4.457    8.149  18.33353786      2.90873205
## 39            H        4.164    8.804  15.54975011      2.74404457
## 40            H        4.088    9.363   0.58741856     -0.53201766
## 41            H        3.656    8.790   5.77929465      1.75428164
## 42            H        3.755    8.400   5.83333333      1.76358859
## 43            H        4.026    8.218   8.42054028      2.13067399
## 44            H        3.663    5.287  94.15547570      4.54494741
## 45            H        4.284    6.053  30.81116802      3.42787722
## 46            H        4.672    9.284   7.71219302      2.04280259
## 47            H        3.911    4.594  30.40922943      3.41474616
## 48            L        3.470   10.315   3.46097916      1.24155154
## 49            L        3.215    6.568  21.26979294      3.05728789
## 50            L        3.957    6.979  34.46052443      3.53981445
## 51            L        3.936    7.290  21.38545953      3.06271123
## 52            L        3.585    7.513  32.31731665      3.47560321
## 53            L        3.778    7.208  37.19478357      3.61616852
## 54            L        4.316   10.376  13.97455667      2.63723830
## 55            L        2.288    5.088  55.09040881      4.00897563
## 56            L        2.874    6.361  20.40559660      3.01580921
## 57            L        4.518    9.353  23.69293275      3.16517681
## 58            L        4.056    6.387  97.96461563      4.58460635
## 59            L        3.256    4.375  32.96000000      3.49529471
## 60            L        4.436    7.321  36.64799891      3.60135883
## 61            L        3.643    7.522  68.67854294      4.22943682
## 62            L        4.566    6.641  68.31802439      4.22417363
## 63            L        4.213    9.063  14.17852808      2.65172871
## 64            L        4.324    7.733  19.28100349      2.95912034
## 65            L        3.185    5.127  58.31870490      4.06592288
## 66            L        3.725    6.541  44.10640575      3.78660503
## 67            L        4.116    9.121  25.60026313      3.24260263
## 68            L        3.921    6.626  70.55538787      4.25639804
## 69            L        4.979    6.601  10.37721557      2.33961259
## 70            L        2.364    6.376   6.80677541      1.91791850
## 71            L        4.557    9.280  19.11637931      2.95054552
## 72            L        4.576    7.057  42.39761939      3.74709221
## 73            L        3.583    7.816  26.30501535      3.26975962
## 74            L        3.693    6.758  42.14264575      3.74106019
## 75            L        3.823    7.797  19.72553546      2.98191401
## 76            L        4.175    6.908  46.56919514      3.84093927
## 77            L        3.328    6.613  34.59851807      3.54381085
## 78            L        3.654    5.344  55.03368263      4.00794541
## 79            L        3.686    7.859   8.57615473      2.14898565
## 80            L        3.533    5.623  37.86235106      3.63395724
## 81            L        3.686    9.099   9.42960765      2.24385449
## 82            L        4.994    6.844  66.17475161      4.19229899
## 83            L        3.100    7.648  26.15062762      3.26387319
## 84            L        3.083    5.081  26.51052942      3.27754199
## 85            L        2.828    5.793  47.00500604      3.85025411
## 86            L        3.687    6.926  38.75252671      3.65719596
## 87            L        4.464    8.429  12.43326611      2.52037563
## 88            L        4.864    8.745  24.64265294      3.20447880
## 89            L        2.037    3.750 108.72000000      4.68877577
## 90            L        5.148    6.868  24.24286546      3.18812237
## 91            L        4.257    5.239  87.57396450      4.47248374
## 92            L        3.977    5.342  53.80007488      3.98527486
## 93            L        4.934    7.455  73.72233400      4.30030579
## 94            L        4.966    5.677  19.88726440      2.99007955
## 95            L        4.376    5.645  22.90522586      3.13136509
## 96            L        4.022    6.343   7.11020022      1.96153040
## 97            H        2.384    6.582   5.49984807      1.70472047
## 98            H        3.128    5.800  18.65517241      2.92612345
## 99            H        3.553    6.453   9.29800093      2.22979942
## 100           H        2.941    7.115   3.17638791      1.15574467
## 101           H        3.146    6.465   5.35189482      1.67745067
## 102           H        2.529    6.094   9.92779783      2.29533868
## 103           H        2.915    4.429   4.01896591      1.39102463
## 104           H        3.541    6.797   2.16271885      0.77136616
## 105           H        3.425    3.791  18.93959377      2.94125464
## 106           H        4.016    7.637  12.89773471      2.55705169
## 107           H        3.388    7.166   2.73513815      1.00618195
## 108           H        3.244    6.179  11.02120084      2.39982077
## 109           H        4.471    8.479   4.03349452      1.39463313
## 110           H        2.721    5.915   4.64919696      1.53669451
## 111           H        3.434    6.937  12.90183076      2.55736922
## 112           H        3.390    5.634   1.77493788      0.57376542
## 113           H        3.811    8.301   0.69871100     -0.35851807
## 114           H        3.058    6.829   6.97027383      1.94165451
## 115           H        2.996    7.893   5.38451793      1.68352779
## 116           H        4.033   10.004   0.58976409     -0.52803266
## 117           H        3.271    7.576   0.19799366     -1.61952025
## 118           H        2.459    5.039  10.00198452      2.30278353
## 119           H        2.848    5.411   7.54019590      2.02024816
## 120           H        2.355    4.924   9.44354184      2.24533110
## 121           H        3.226    4.617   4.80831709      1.57034715
## 122           H        2.908    5.836   0.97669637     -0.02357946
## 123           H        3.438    6.758   8.34566440      2.12174217
## 124           H        3.102    7.255   3.37698139      1.21698223
## 125           H        2.905    5.495   6.31483166      1.84290110
## 126           H        2.899    4.488  32.10784314      3.46910033
## 127           H        3.664    4.989   6.41411104      1.85850041
## 128           H        3.953    5.095  10.08832188      2.31137851
## 129           H        3.598    4.718   9.11403137      2.20981513
## 130           H        3.712    5.000   7.10000000      1.96009478
## 131           H        3.676    5.525   8.30769231      2.11718187
## 132           H        3.814    4.937   6.94753899      1.93838749
## 133           H        4.335    6.064   8.04749340      2.08536066
## 134           H        2.721    5.204   2.01767871      0.70194770
## 135           H        3.482    6.168  10.68417639      2.36876380
## 136           H        3.303    7.058   1.50184188      0.40669228
## 137           H        3.404    7.095   2.42424242      0.88551907
## 138           H        2.901    5.130   4.69785575      1.54710618
## 139           H        3.687    5.945  13.45668629      2.59947610
## 140           H        3.338    6.304  14.87944162      2.69998050
## 141           H        3.770    5.416   4.33899557      1.46764289
## 142           H        2.937    6.022   1.77681833      0.57482431
## 143           H        2.711    6.399   1.40646976      0.34108285
## 144           L        2.469    5.757  22.14695154      3.09769986
## 145           L        2.647    5.063   3.00217263      1.09933623
## 146           L        2.806    4.918  12.99308662      2.56441742
## 147           L        2.790    4.488  28.14171123      3.33725286
## 148           L        2.245    5.398  11.98592071      2.48373269
## 149           L        2.118    4.392  32.74134791      3.48863874
## 150           L        2.735    5.837  12.78053795      2.54792354
## 151           L        1.824    4.437  41.04124408      3.71457751
## 152           L        3.062    5.167   1.43216567      0.35918775
## 153           L        2.146    4.936  14.12074554      2.64764503
## 154           L        1.572    3.365  53.87815750      3.98672515
## 155           L        1.839    3.950  23.69620253      3.16531480
## 156           L        0.533    0.815 447.36196320      6.10336803
## 157           L        2.175    4.860  26.37860082      3.27255311
## 158           L        1.954    4.473  22.11044042      3.09604991
## 159           L        1.974    5.062  20.20940340      3.00614801
## 160           L        1.667    4.271  23.46054788      3.15532020
## 161           L        1.476    4.098  26.32991703      3.27070582
## 162           L        1.771    5.867  22.75438896      3.12475805
## 163           L        1.698    4.231  17.08815883      2.83838576
## 164           L        1.763    5.341   3.83823254      1.34501198
## 165           L        1.527    2.157  56.00370885      4.02541792
## 166           L        1.982    2.923  36.09305508      3.58610047
## 167           L        1.875    4.359   0.06882312     -2.67621547
## 168           L        1.890    6.340   0.25236593     -1.37687514
## 169           L        2.277    6.037   1.07669372      0.07389498
## 170           L        1.648    5.127   1.28730252      0.25254896
## 171           L        2.453    5.178  18.38547702      2.91156106
## 172           L        4.374    7.607   4.78506639      1.56549990
## 173           L        3.775    6.709   6.48382769      1.86931103
## 174           L        2.963    6.629   7.82923518      2.05786483
## 175           L        2.642    4.595  24.06964091      3.18095133
## 176           L        2.385    5.413   6.96471458      1.94085663
## 177           L        3.030    4.794   5.67375886      1.73585184
## 178           L        2.977    6.953  11.79347044      2.46754603
## 179           L        3.971    6.916  25.80971660      3.25075103
## 180           L        3.162    6.733   6.00029704      1.79180898
## 181           L        2.911    6.126   8.60267711      2.15207345
## 182           L        3.107    5.147  10.80240917      2.37976918
## 183           L        3.691    5.390  33.06122449      3.49836113
## 184           L        2.939    5.633  14.27303391      2.65837202
## 185           L        3.300    7.230  18.25726141      2.90456289
## 186           L        2.886    5.893  34.29492618      3.53499742
## 187           L        3.375    6.912  13.94675926      2.63524717
## 188           L        2.870    4.840  10.59917355      2.36077603
## 189           L        3.403    6.239  15.98012502      2.77134576
## 190           L        3.013    7.053  13.05827308      2.56942189
## 191           L        2.502    6.042  22.04568024      3.09311668
  1. Code = run a shaprio-wilks and levene test on the transformed data
shapiro.test(barn$log_WidthChange)
## 
##  Shapiro-Wilk normality test
## 
## data:  barn$log_WidthChange
## W = 0.95978, p-value = 2.894e-05
library(car)
leveneTest(log_WidthChange ~ Tide.Height,
          data = barn)
## Warning in leveneTest.default(y = y, group = group, ...): group coerced to
## factor.
## Levene's Test for Homogeneity of Variance (center = median)
##        Df F value Pr(>F)
## group   1   0.225 0.6358
##       189
  1. Question = what did the transformation do to your assumptions? Have they all been met?
  1. After doing the log transformation, the shapiro test gave a p-value of 2.894e-05 which is greatly improved, but still not enough to fulfill the assumption of normality. On the other hand, the levene test gave a p-value of 0.6358 which passes the assumption of equal variance. So although improved, not all of the assumptions have been met.
  1. Question = given the outcome, what type of non-parametric test should be run?
  1. Given the outcome I would run the Mann-Whitney U-test (AKA Wilcoxon test) used for two-sample t-tests.
  1. Code = code for the test you decided in Q9
wilcox.test(log_WidthChange ~ Tide.Height, data = barn)
## 
##  Wilcoxon rank sum test with continuity correction
## 
## data:  log_WidthChange by Tide.Height
## W = 2200, p-value = 6.623e-10
## alternative hypothesis: true location shift is not equal to 0
  1. Question = interpret the outcome of the code from Q10
  1. The wilcox test gave a p-value of 6.623e-10 therefore we reject the null hypothesis. Meaning that there is a significant difference between samples.
  1. Code = create an appropriate visual to show WidthChange by Tide.Height
boxplot(log_WidthChange ~ Tide.Height,
          data = barn)


Part 2: Correlation

The barnacle data set had measured key size metric for this species over time. In Part 1, you explored how tide height would impact the growht of one of these metrics, basal width. However, we collected two types of size data on the barnacles, their basal width and their operculum length. Much like we see body and brain size correlate, do we see basal width and operculum length do the same thing?

Actions/Questions: Complete the following using one or several code chunks with the ‘barnacle_data.csv’ file

  1. Code: create a scatter plot of OperculumAug vs. BasalAug
plot(barn$OperculumAug, barn$BasalAug, pch = 16, col="blue")

  1. Question: what do you suspect the correlation is? Does it look positive or negative? Does it look strong or weak?
  1. This looks to be a semi-strong, positive correlation close to zero.
  1. Code: Run a correlation test on this relationship
cor(x = barn$OperculumAug, y = barn$BasalAug) 
## [1] 0.7152863
cor.test(x = barn$OperculumAug, y = barn$BasalAug) 
## 
##  Pearson's product-moment correlation
## 
## data:  barn$OperculumAug and barn$BasalAug
## t = 14.071, df = 189, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.6381109 0.7782316
## sample estimates:
##       cor 
## 0.7152863
  1. Question: interpret the p-value and tell me what this means for this relationship.
  1. The p-value is 2.2e-16 so the correlation is statistically significant.

Part 3: Regression

Load the data ‘environmental.csv’, this dataset has tracked environmental parameters in NYC throughout the course of a summer. You are interested developing a predictive model that tracks how radiation influences temperature. This would allow you to better hone in on weather forecasting.

Actions/Questions: Complete the following using one or several code chunks

  1. Code: create a scatter plot of ‘radiation’ vs. ‘temperature’ (x axis should be radiation)
envir <- read.table('environmental.csv', ',', header=T)
envir
##     rownames ozone radiation temperature wind
## 1          1    41       190          67  7.4
## 2          2    36       118          72  8.0
## 3          3    12       149          74 12.6
## 4          4    18       313          62 11.5
## 5          5    23       299          65  8.6
## 6          6    19        99          59 13.8
## 7          7     8        19          61 20.1
## 8          8    16       256          69  9.7
## 9          9    11       290          66  9.2
## 10        10    14       274          68 10.9
## 11        11    18        65          58 13.2
## 12        12    14       334          64 11.5
## 13        13    34       307          66 12.0
## 14        14     6        78          57 18.4
## 15        15    30       322          68 11.5
## 16        16    11        44          62  9.7
## 17        17     1         8          59  9.7
## 18        18    11       320          73 16.6
## 19        19     4        25          61  9.7
## 20        20    32        92          61 12.0
## 21        21    23        13          67 12.0
## 22        22    45       252          81 14.9
## 23        23   115       223          79  5.7
## 24        24    37       279          76  7.4
## 25        25    29       127          82  9.7
## 26        26    71       291          90 13.8
## 27        27    39       323          87 11.5
## 28        28    23       148          82  8.0
## 29        29    21       191          77 14.9
## 30        30    37       284          72 20.7
## 31        31    20        37          65  9.2
## 32        32    12       120          73 11.5
## 33        33    13       137          76 10.3
## 34        34   135       269          84  4.0
## 35        35    49       248          85  9.2
## 36        36    32       236          81  9.2
## 37        37    64       175          83  4.6
## 38        38    40       314          83 10.9
## 39        39    77       276          88  5.1
## 40        40    97       267          92  6.3
## 41        41    97       272          92  5.7
## 42        42    85       175          89  7.4
## 43        43    10       264          73 14.3
## 44        44    27       175          81 14.9
## 45        45     7        48          80 14.3
## 46        46    48       260          81  6.9
## 47        47    35       274          82 10.3
## 48        48    61       285          84  6.3
## 49        49    79       187          87  5.1
## 50        50    63       220          85 11.5
## 51        51    16         7          74  6.9
## 52        52    80       294          86  8.6
## 53        53   108       223          85  8.0
## 54        54    20        81          82  8.6
## 55        55    52        82          86 12.0
## 56        56    82       213          88  7.4
## 57        57    50       275          86  7.4
## 58        58    64       253          83  7.4
## 59        59    59       254          81  9.2
## 60        60    39        83          81  6.9
## 61        61     9        24          81 13.8
## 62        62    16        77          82  7.4
## 63        63   122       255          89  4.0
## 64        64    89       229          90 10.3
## 65        65   110       207          90  8.0
## 66        66    44       192          86 11.5
## 67        67    28       273          82 11.5
## 68        68    65       157          80  9.7
## 69        69    22        71          77 10.3
## 70        70    59        51          79  6.3
## 71        71    23       115          76  7.4
## 72        72    31       244          78 10.9
## 73        73    44       190          78 10.3
## 74        74    21       259          77 15.5
## 75        75     9        36          72 14.3
## 76        76    45       212          79  9.7
## 77        77   168       238          81  3.4
## 78        78    73       215          86  8.0
## 79        79    76       203          97  9.7
## 80        80   118       225          94  2.3
## 81        81    84       237          96  6.3
## 82        82    85       188          94  6.3
## 83        83    96       167          91  6.9
## 84        84    78       197          92  5.1
## 85        85    73       183          93  2.8
## 86        86    91       189          93  4.6
## 87        87    47        95          87  7.4
## 88        88    32        92          84 15.5
## 89        89    20       252          80 10.9
## 90        90    23       220          78 10.3
## 91        91    21       230          75 10.9
## 92        92    24       259          73  9.7
## 93        93    44       236          81 14.9
## 94        94    21       259          76 15.5
## 95        95    28       238          77  6.3
## 96        96     9        24          71 10.9
## 97        97    13       112          71 11.5
## 98        98    46       237          78  6.9
## 99        99    18       224          67 13.8
## 100      100    13        27          76 10.3
## 101      101    24       238          68 10.3
## 102      102    16       201          82  8.0
## 103      103    13       238          64 12.6
## 104      104    23        14          71  9.2
## 105      105    36       139          81 10.3
## 106      106     7        49          69 10.3
## 107      107    14        20          63 16.6
## 108      108    30       193          70  6.9
## 109      109    14       191          75 14.3
## 110      110    18       131          76  8.0
## 111      111    20       223          68 11.5
plot(envir$radiation, envir$temperature, pch = 16, col="blue")

  1. Code: create and run a linear regression model for ‘radiation’ vs. ‘temperature’
plot(x = envir$radiation, y = envir$temperature)

envir.lm <- lm(formula = radiation ~ temperature, data = envir)
summary(envir.lm)
## 
## Call:
## lm(formula = radiation ~ temperature, data = envir)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -169.823  -54.259    7.112   65.960  187.996 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)   
## (Intercept) -34.0209    68.6223  -0.496  0.62105   
## temperature   2.8129     0.8756   3.212  0.00173 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 87.52 on 109 degrees of freedom
## Multiple R-squared:  0.08649,    Adjusted R-squared:  0.07811 
## F-statistic: 10.32 on 1 and 109 DF,  p-value: 0.001731
  1. Question: how do you interpret the p-value? How do you interpret the R^2 value?
  1. The p-value is 0.001731 which is less than 0.05 suggesting a significant relationship between the two variables. The adjusted R squared value is 0.07811 (on a scale of 0-1) telling us that X can predict most of the variation of Y.
  1. Code: add a least squares regression line to your plot
plot(x = envir$radiation, y = envir$temperature)
envir.lm <- lm(radiation ~ temperature, data = envir)
abline(envir.lm, col ='red') 

  1. Code: create code that predicts what the temperature in NYC will be with a radiation value of 180
envir.lm <- lm(radiation ~ temperature, data = envir)
predict(envir.lm, radiation = 180)
##        1        2        3        4        5        6        7        8 
## 154.4429 168.5073 174.1331 140.3784 148.8171 131.9397 137.5655 160.0686 
##        9       10       11       12       13       14       15       16 
## 151.6300 157.2557 129.1268 146.0042 151.6300 126.3139 157.2557 140.3784 
##       17       18       19       20       21       22       23       24 
## 131.9397 171.3202 137.5655 137.5655 154.4429 193.8233 188.1975 179.7589 
##       25       26       27       28       29       30       31       32 
## 196.6362 219.1393 210.7007 196.6362 182.5718 168.5073 148.8171 171.3202 
##       33       34       35       36       37       38       39       40 
## 179.7589 202.2620 205.0749 193.8233 199.4491 199.4491 213.5136 224.7651 
##       41       42       43       44       45       46       47       48 
## 224.7651 216.3265 171.3202 193.8233 191.0104 193.8233 196.6362 202.2620 
##       49       50       51       52       53       54       55       56 
## 210.7007 205.0749 174.1331 207.8878 205.0749 196.6362 207.8878 213.5136 
##       57       58       59       60       61       62       63       64 
## 207.8878 199.4491 193.8233 193.8233 193.8233 196.6362 216.3265 219.1393 
##       65       66       67       68       69       70       71       72 
## 219.1393 207.8878 196.6362 191.0104 182.5718 188.1975 179.7589 185.3847 
##       73       74       75       76       77       78       79       80 
## 185.3847 182.5718 168.5073 188.1975 193.8233 207.8878 238.8296 230.3909 
##       81       82       83       84       85       86       87       88 
## 236.0167 230.3909 221.9522 224.7651 227.5780 227.5780 210.7007 202.2620 
##       89       90       91       92       93       94       95       96 
## 191.0104 185.3847 176.9460 171.3202 193.8233 179.7589 182.5718 165.6944 
##       97       98       99      100      101      102      103      104 
## 165.6944 185.3847 154.4429 179.7589 157.2557 196.6362 146.0042 165.6944 
##      105      106      107      108      109      110      111 
## 193.8233 160.0686 143.1913 162.8815 176.9460 179.7589 157.2557