#Practice Set 1
#q1
Batting <- read.csv("MLB Batting Data.csv")
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.4.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
sum(Batting$batting_avg>=0.300)
## [1] 106
#answer was 106
#q2
Batting$HitByPitch_NewVar <- NA
Batting$HitByPitch_NewVar[Batting$b_hit_by_pitch>=1] <-1
Batting$HitByPitch_NewVar[Batting$b_hit_by_pitch==0] <- 0
table(Batting$HitByPitch_NewVar)
##
## 0 1
## 29 700
#29 weren't hit at least once, 700 were at least once.
#q3
X <- Batting$exit_velocity_avg
Y <- Batting$b_home_run
reg <- lm(Y~X)
summary(reg)
##
## Call:
## lm(formula = Y ~ X)
##
## Residuals:
## Min 1Q Median 3Q Max
## -22.5319 -5.5319 -0.2615 5.3555 28.1160
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -217.6313 11.1088 -19.59 <2e-16 ***
## X 2.7042 0.1254 21.57 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.941 on 727 degrees of freedom
## Multiple R-squared: 0.3901, Adjusted R-squared: 0.3893
## F-statistic: 465.1 on 1 and 727 DF, p-value: < 2.2e-16
plot(X, Y, xlab="Exit Velocity Average", ylab="Home Run Number", main="Question 3 Regression")
plot(reg$residuals)
#There is a general association of an increase in Exit Velocity Average is associated with Home Run Number increase,
#but adjusted r^2 is 0.3893 which implies r is about 0.6239, which is a moderate association. Also after looking at the returned
#regression coefficient associated (beta_1) which was 2.7042, this is a positive number also suggestion the positive association
#between the two variables. This value is also statistically significant since it has a P-Value less than 0.05. I would say the
#conventional wisdom is somewhat correct.
#q4
x <- Batting$player_age
y <- Batting$batting_avg
reg2 <- lm(y~x)
summary(reg2)
##
## Call:
## lm(formula = y ~ x)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.100959 -0.018464 -0.000835 0.019041 0.077603
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.2829787 0.0077558 36.49 <2e-16 ***
## x -0.0004381 0.0002656 -1.65 0.0995 .
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02707 on 727 degrees of freedom
## Multiple R-squared: 0.003729, Adjusted R-squared: 0.002359
## F-statistic: 2.721 on 1 and 727 DF, p-value: 0.09946
#not stat sig beta 1, low r
Batting$age_new <- NA
Batting$age_new[Batting$player_age<30] <- 0
Batting$age_new[Batting$player_age>=30] <- 1
x2 <- Batting$age_new
reg3 <- lm(y~x2)
summary(reg3)
##
## Call:
## lm(formula = y ~ x2)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.099747 -0.018747 -0.000042 0.018958 0.079253
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.272042 0.001301 209.134 <2e-16 ***
## x2 -0.004294 0.002038 -2.107 0.0355 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.02704 on 727 degrees of freedom
## Multiple R-squared: 0.00607, Adjusted R-squared: 0.004703
## F-statistic: 4.44 on 1 and 727 DF, p-value: 0.03545
#better beta 1
#q5
Slugging <- function(Batting){
slugging_percentage <- data.frame(((Batting$b_single) + (2*Batting$b_double)
+ (3*Batting$b_triple) + (4*Batting$b_home_run))/(Batting$b_ab))
return(slugging_percentage)
}
Slugging(Batting)
## X..Batting.b_single.....2...Batting.b_double.....3...Batting.b_triple....
## 1 0.3832976
## 2 0.3861720
## 3 0.4171429
## 4 0.3987207
## 5 0.4160839
## 6 0.4131737
## 7 0.4227941
## 8 0.4391218
## 9 0.4527027
## 10 0.5036101
## 11 0.3662692
## 12 0.4656616
## 13 0.4521158
## 14 0.4294355
## 15 0.4452424
## 16 0.4119782
## 17 0.5485612
## 18 0.3971377
## 19 0.4438095
## 20 0.3572621
## 21 0.4241436
## 22 0.5479452
## 23 0.4865385
## 24 0.4232456
## 25 0.4062500
## 26 0.3853955
## 27 0.4265594
## 28 0.4342380
## 29 0.6009317
## 30 0.4282700
## 31 0.3844857
## 32 0.4400685
## 33 0.4859649
## 34 0.5403950
## 35 0.5295405
## 36 0.4615385
## 37 0.5081081
## 38 0.5778175
## 39 0.4276094
## 40 0.4733475
## 41 0.3151261
## 42 0.4712286
## 43 0.4225806
## 44 0.4553415
## 45 0.4234043
## 46 0.4063745
## 47 0.5011086
## 48 0.5285714
## 49 0.5725191
## 50 0.4479718
## 51 0.5156250
## 52 0.4858223
## 53 0.4163090
## 54 0.3708920
## 55 0.5282686
## 56 0.5202703
## 57 0.4842767
## 58 0.5430712
## 59 0.5627240
## 60 0.5296703
## 61 0.5474576
## 62 0.4207436
## 63 0.4274809
## 64 0.5863636
## 65 0.4088670
## 66 0.4429134
## 67 0.5207207
## 68 0.5069930
## 69 0.4588910
## 70 0.6314908
## 71 0.5181237
## 72 0.3815789
## 73 0.5057915
## 74 0.4093656
## 75 0.5481240
## 76 0.4540338
## 77 0.4034749
## 78 0.4320988
## 79 0.4975124
## 80 0.5334646
## 81 0.5130112
## 82 0.5218978
## 83 0.3751914
## 84 0.4775281
## 85 0.6293532
## 86 0.4524715
## 87 0.5523349
## 88 0.4606742
## 89 0.5047619
## 90 0.4132231
## 91 0.5032154
## 92 0.5858086
## 93 0.3350515
## 94 0.4323232
## 95 0.4869403
## 96 0.4638448
## 97 0.4498270
## 98 0.4507042
## 99 0.4897959
## 100 0.4975689
## 101 0.5373406
## 102 0.4902280
## 103 0.4125737
## 104 0.4513889
## 105 0.6273063
## 106 0.4714286
## 107 0.4511278
## 108 0.5073801
## 109 0.4210526
## 110 0.4385382
## 111 0.4599659
## 112 0.4028021
## 113 0.4098361
## 114 0.3919156
## 115 0.4804089
## 116 0.4841438
## 117 0.4797441
## 118 0.5053763
## 119 0.3970346
## 120 0.4572565
## 121 0.5307856
## 122 0.4246575
## 123 0.4086957
## 124 0.4024896
## 125 0.4663024
## 126 0.4585987
## 127 0.4207436
## 128 0.4071146
## 129 0.3244353
## 130 0.4037479
## 131 0.5829060
## 132 0.4748201
## 133 0.5367483
## 134 0.4786642
## 135 0.4223827
## 136 0.3237705
## 137 0.4961089
## 138 0.4134199
## 139 0.4086242
## 140 0.4869739
## 141 0.4020443
## 142 0.5812500
## 143 0.4880295
## 144 0.4240838
## 145 0.4297352
## 146 0.3975659
## 147 0.4547069
## 148 0.5311005
## 149 0.4144330
## 150 0.4785553
## 151 0.6387665
## 152 0.4370861
## 153 0.4086242
## 154 0.4407159
## 155 0.5758621
## 156 0.4429530
## 157 0.3718861
## 158 0.5093946
## 159 0.4114286
## 160 0.5030550
## 161 0.5054466
## 162 0.3956835
## 163 0.3933333
## 164 0.4164905
## 165 0.5148342
## 166 0.4030418
## 167 0.4057971
## 168 0.5026087
## 169 0.5407801
## 170 0.3788820
## 171 0.5110063
## 172 0.3866944
## 173 0.4497354
## 174 0.5565217
## 175 0.4091816
## 176 0.4520548
## 177 0.4757119
## 178 0.5500000
## 179 0.4084967
## 180 0.4201389
## 181 0.4355346
## 182 0.5027829
## 183 0.5209472
## 184 0.5494137
## 185 0.4678363
## 186 0.4288499
## 187 0.5182724
## 188 0.5162524
## 189 0.5195312
## 190 0.4380610
## 191 0.4642127
## 192 0.4721649
## 193 0.4532710
## 194 0.5337553
## 195 0.3500000
## 196 0.4694323
## 197 0.3476190
## 198 0.4248788
## 199 0.4104803
## 200 0.4601367
## 201 0.5981651
## 202 0.5220700
## 203 0.5908142
## 204 0.4771784
## 205 0.4225941
## 206 0.3778163
## 207 0.4573379
## 208 0.6446809
## 209 0.5095986
## 210 0.5031546
## 211 0.4602804
## 212 0.5721739
## 213 0.4412417
## 214 0.5339367
## 215 0.3205645
## 216 0.5833333
## 217 0.5154004
## 218 0.4183908
## 219 0.4638695
## 220 0.4763705
## 221 0.3918269
## 222 0.4302885
## 223 0.4067460
## 224 0.5211786
## 225 0.5170732
## 226 0.5252033
## 227 0.4616695
## 228 0.4291845
## 229 0.5377778
## 230 0.4925651
## 231 0.4886364
## 232 0.5000000
## 233 0.6707566
## 234 0.4625551
## 235 0.5553746
## 236 0.4217557
## 237 0.4849445
## 238 0.4673913
## 239 0.4275618
## 240 0.3982103
## 241 0.4095861
## 242 0.5310734
## 243 0.5183946
## 244 0.4386973
## 245 0.5544218
## 246 0.4393939
## 247 0.5190840
## 248 0.4585366
## 249 0.4210526
## 250 0.4602588
## 251 0.4370504
## 252 0.4684096
## 253 0.5692600
## 254 0.5242881
## 255 0.3356164
## 256 0.3352941
## 257 0.3502024
## 258 0.5922671
## 259 0.4971209
## 260 0.4320786
## 261 0.4792531
## 262 0.5920578
## 263 0.4826176
## 264 0.5121457
## 265 0.4223602
## 266 0.4462475
## 267 0.5445135
## 268 0.5829146
## 269 0.4936248
## 270 0.4169675
## 271 0.4576577
## 272 0.5687606
## 273 0.5584906
## 274 0.4429224
## 275 0.5080321
## 276 0.6290323
## 277 0.5219885
## 278 0.5348837
## 279 0.4500000
## 280 0.4318182
## 281 0.4180791
## 282 0.4306839
## 283 0.5313725
## 284 0.5000000
## 285 0.4194139
## 286 0.5548686
## 287 0.5128205
## 288 0.5347985
## 289 0.5060034
## 290 0.4543860
## 291 0.5311909
## 292 0.4442539
## 293 0.5207373
## 294 0.5479452
## 295 0.5175719
## 296 0.3842282
## 297 0.4737991
## 298 0.4331897
## 299 0.5479705
## 300 0.5030550
## 301 0.4088292
## 302 0.5530303
## 303 0.4856597
## 304 0.4231579
## 305 0.4532628
## 306 0.4707113
## 307 0.3578337
## 308 0.4800664
## 309 0.4525692
## 310 0.3800676
## 311 0.5337995
## 312 0.4798599
## 313 0.3945578
## 314 0.3783784
## 315 0.4110535
## 316 0.4630631
## 317 0.3504098
## 318 0.4632953
## 319 0.4455128
## 320 0.5568182
## 321 0.4432548
## 322 0.5359116
## 323 0.4743590
## 324 0.3383585
## 325 0.4580499
## 326 0.4568966
## 327 0.3320312
## 328 0.4850615
## 329 0.4278846
## 330 0.4365591
## 331 0.4272560
## 332 0.3842196
## 333 0.5661017
## 334 0.4488189
## 335 0.5216450
## 336 0.3202614
## 337 0.3940000
## 338 0.4861996
## 339 0.4354305
## 340 0.5619546
## 341 0.4496788
## 342 0.4110738
## 343 0.3689655
## 344 0.4601594
## 345 0.4297030
## 346 0.4403292
## 347 0.4495114
## 348 0.4975767
## 349 0.3758170
## 350 0.3903346
## 351 0.4773140
## 352 0.4876325
## 353 0.3849287
## 354 0.4538745
## 355 0.3702970
## 356 0.4703770
## 357 0.4344828
## 358 0.5412844
## 359 0.4431818
## 360 0.3992995
## 361 0.4980237
## 362 0.4072495
## 363 0.4429530
## 364 0.4345550
## 365 0.3573826
## 366 0.4013304
## 367 0.3942766
## 368 0.3659574
## 369 0.3578947
## 370 0.3945455
## 371 0.4515539
## 372 0.5397112
## 373 0.4776423
## 374 0.4801512
## 375 0.4149533
## 376 0.3829322
## 377 0.5418641
## 378 0.3567753
## 379 0.4277567
## 380 0.5352349
## 381 0.4486692
## 382 0.4488978
## 383 0.5696649
## 384 0.4592476
## 385 0.3357143
## 386 0.3747715
## 387 0.4438687
## 388 0.5677419
## 389 0.4387569
## 390 0.3882979
## 391 0.4819533
## 392 0.4699454
## 393 0.4038835
## 394 0.3766892
## 395 0.5119454
## 396 0.4426966
## 397 0.3434705
## 398 0.4256121
## 399 0.4444444
## 400 0.4078675
## 401 0.3652803
## 402 0.4083969
## 403 0.3923077
## 404 0.4615385
## 405 0.3752759
## 406 0.3670103
## 407 0.4590985
## 408 0.4513274
## 409 0.4074733
## 410 0.4046763
## 411 0.4178862
## 412 0.3859964
## 413 0.3695238
## 414 0.5895652
## 415 0.4181818
## 416 0.6487524
## 417 0.5024470
## 418 0.3811130
## 419 0.5746753
## 420 0.3788546
## 421 0.3223301
## 422 0.4975369
## 423 0.4510433
## 424 0.3580508
## 425 0.5052265
## 426 0.4442675
## 427 0.4883721
## 428 0.4189435
## 429 0.5023697
## 430 0.4166667
## 431 0.3383178
## 432 0.4159664
## 433 0.4208809
## 434 0.4222222
## 435 0.4311475
## 436 0.4628975
## 437 0.4198020
## 438 0.4790620
## 439 0.3352273
## 440 0.3993174
## 441 0.3894737
## 442 0.4275742
## 443 0.6201117
## 444 0.5214408
## 445 0.5126812
## 446 0.4171429
## 447 0.4755877
## 448 0.3964844
## 449 0.4569983
## 450 0.3886640
## 451 0.5630252
## 452 0.4348592
## 453 0.4163636
## 454 0.4024390
## 455 0.4269663
## 456 0.5328244
## 457 0.5291181
## 458 0.4515366
## 459 0.4361874
## 460 0.3978022
## 461 0.4642202
## 462 0.4181818
## 463 0.5045537
## 464 0.4677419
## 465 0.3655031
## 466 0.4649910
## 467 0.4838188
## 468 0.4464000
## 469 0.5551783
## 470 0.5334421
## 471 0.3500785
## 472 0.4166667
## 473 0.5213270
## 474 0.4245690
## 475 0.4593640
## 476 0.4455067
## 477 0.4473684
## 478 0.3854390
## 479 0.3811189
## 480 0.4740883
## 481 0.3738657
## 482 0.4430894
## 483 0.5519031
## 484 0.4644068
## 485 0.3973289
## 486 0.4486572
## 487 0.4321881
## 488 0.4297659
## 489 0.4649123
## 490 0.4928058
## 491 0.4341373
## 492 0.3779070
## 493 0.5064935
## 494 0.5503597
## 495 0.3619744
## 496 0.4775510
## 497 0.5381605
## 498 0.3797753
## 499 0.4991974
## 500 0.4387352
## 501 0.4551607
## 502 0.4958506
## 503 0.4982818
## 504 0.5051370
## 505 0.3737958
## 506 0.4741697
## 507 0.4990893
## 508 0.3672199
## 509 0.3907157
## 510 0.5302714
## 511 0.3326360
## 512 0.5243243
## 513 0.5347826
## 514 0.4458599
## 515 0.5951036
## 516 0.4887737
## 517 0.4008264
## 518 0.5312500
## 519 0.3930530
## 520 0.4992151
## 521 0.4332756
## 522 0.4560327
## 523 0.5493934
## 524 0.5687606
## 525 0.3245283
## 526 0.4945652
## 527 0.5000000
## 528 0.4217391
## 529 0.5437393
## 530 0.4061224
## 531 0.3989726
## 532 0.4377432
## 533 0.4442270
## 534 0.3812261
## 535 0.4524237
## 536 0.4567063
## 537 0.4303797
## 538 0.3880903
## 539 0.4330579
## 540 0.4688525
## 541 0.4817518
## 542 0.4497354
## 543 0.4348592
## 544 0.4208333
## 545 0.4565217
## 546 0.4466192
## 547 0.5500911
## 548 0.4202401
## 549 0.4407115
## 550 0.3783784
## 551 0.4679487
## 552 0.4106195
## 553 0.4629981
## 554 0.4536585
## 555 0.5695793
## 556 0.4116466
## 557 0.5086042
## 558 0.4607679
## 559 0.4570447
## 560 0.4991625
## 561 0.5052854
## 562 0.4686275
## 563 0.5463415
## 564 0.3361884
## 565 0.3110236
## 566 0.5538972
## 567 0.5328125
## 568 0.4282908
## 569 0.4826990
## 570 0.4463190
## 571 0.4377104
## 572 0.4981884
## 573 0.4281099
## 574 0.4354305
## 575 0.5024793
## 576 0.4126316
## 577 0.4268503
## 578 0.4856631
## 579 0.5342262
## 580 0.3793103
## 581 0.3759124
## 582 0.4619469
## 583 0.4171429
## 584 0.5119617
## 585 0.4186047
## 586 0.4060150
## 587 0.4506066
## 588 0.5075472
## 589 0.3533191
## 590 0.3786008
## 591 0.4339286
## 592 0.4357298
## 593 0.4740000
## 594 0.4189655
## 595 0.3798768
## 596 0.4216216
## 597 0.5086705
## 598 0.5160714
## 599 0.3134021
## 600 0.3472486
## 601 0.4125000
## 602 0.2957447
## 603 0.4395604
## 604 0.4578755
## 605 0.5015974
## 606 0.4398074
## 607 0.3875000
## 608 0.4174397
## 609 0.4235501
## 610 0.4634146
## 611 0.4194831
## 612 0.3679245
## 613 0.3695652
## 614 0.4805195
## 615 0.4142857
## 616 0.4665227
## 617 0.4205426
## 618 0.4574257
## 619 0.4479866
## 620 0.4684211
## 621 0.4283054
## 622 0.3782051
## 623 0.3463115
## 624 0.4893993
## 625 0.4569288
## 626 0.5486111
## 627 0.4635628
## 628 0.6291740
## 629 0.5328836
## 630 0.4089980
## 631 0.4513109
## 632 0.3619835
## 633 0.4146758
## 634 0.3996627
## 635 0.4597496
## 636 0.4208417
## 637 0.5048544
## 638 0.4659091
## 639 0.4277674
## 640 0.4589878
## 641 0.4699647
## 642 0.5089141
## 643 0.3803987
## 644 0.4392157
## 645 0.3802345
## 646 0.4329897
## 647 0.3841202
## 648 0.5386179
## 649 0.3935970
## 650 0.4930140
## 651 0.4666667
## 652 0.3980424
## 653 0.3886792
## 654 0.5349716
## 655 0.3876582
## 656 0.4799197
## 657 0.4338235
## 658 0.3489209
## 659 0.4940476
## 660 0.6284501
## 661 0.4200000
## 662 0.4963636
## 663 0.4729459
## 664 0.5256167
## 665 0.4989154
## 666 0.3716667
## 667 0.5610169
## 668 0.3717391
## 669 0.4897260
## 670 0.3273810
## 671 0.4932886
## 672 0.4908425
## 673 0.3996569
## 674 0.5230496
## 675 0.4742063
## 676 0.3905967
## 677 0.3902954
## 678 0.5000000
## 679 0.4117647
## 680 0.5379747
## 681 0.4842767
## 682 0.4794275
## 683 0.4169675
## 684 0.5975610
## 685 0.4382911
## 686 0.5224172
## 687 0.3686354
## 688 0.4580153
## 689 0.5544554
## 690 0.5189107
## 691 0.4240506
## 692 0.5668896
## 693 0.4078947
## 694 0.4029536
## 695 0.3933210
## 696 0.4108527
## 697 0.4000000
## 698 0.4111776
## 699 0.6403846
## 700 0.4062500
## 701 0.4684874
## 702 0.4161392
## 703 0.4365385
## 704 0.4826790
## 705 0.4156627
## 706 0.4257812
## 707 0.5519031
## 708 0.5319865
## 709 0.4980000
## 710 0.4355828
## 711 0.5270506
## 712 0.3953975
## 713 0.4440299
## 714 0.4353183
## 715 0.4534483
## 716 0.3658052
## 717 0.3836478
## 718 0.4568966
## 719 0.4475138
## 720 0.4056437
## 721 0.4703770
## 722 0.4158076
## 723 0.3808664
## 724 0.4645941
## 725 0.4522692
## 726 0.5082267
## 727 0.4964158
## 728 0.4672897
## 729 0.3996540
slug <- Slugging(Batting)
percentage <- slug$X..Batting.b_single.....2...Batting.b_double.....3...Batting.b_triple....
mean(percentage)
## [1] 0.4568072
#0.04568072
sum(percentage > 0.500)
## [1] 183
#183
#q6
#A
Batting_Subset <- subset(Batting, Batting$year=="2019")
sum(Batting_Subset$year==2019)
## [1] 156
#156
#B
mean(Batting_Subset$b_total_pa)
## [1] 588.4359
#588.4359
#C
max(Batting_Subset$b_home_run)
## [1] 53
#53
Batting_Subset$first_name[Batting_Subset$b_home_run==53]
## [1] " Pete"
#pete
Batting_Subset$last_name[Batting_Subset$b_home_run==53]
## [1] "Alonso"
#alonso