Learning Objectives:

In this lesson students will learn to:

  • Use survey packages in R
  • Define survey objects
  • Inference for the mean

The Data

These data were put together from NBA and WNBA records for the 2022-2023 and 2022 seasons, respecitively.

For this activity we are going to pretend that we are doing a survey on professional basketball players. These data represent only the raw responses to the survey. These data reflect a subset of the NBA and WNBA 2022 population who respondend to the hypothetical survey. Note that survey weights have been created to adjust for nonresponse.

Download the data here:

## DOWNLOAD COMPLETE SURVEY DATA
bballComp<-read.csv("https://raw.githubusercontent.com/kitadasmalley/Teaching/refs/heads/main/DATA429_599/CODE/bballSampComplete.csv")

str(bballComp)
## 'data.frame':    120 obs. of  31 variables:
##  $ Player  : chr  "LeBron James" "Bradley Beal" "Damian Lillard" "Kyrie Irving" ...
##  $ League  : chr  "NBA" "NBA" "NBA" "NBA" ...
##  $ Age     : int  38 29 32 30 24 25 25 25 32 37 ...
##  $ wi_star : num  5.7 5.7 5.7 5.7 5.7 ...
##  $ Salary  : num  44474988 43279250 42492492 38917057 37096500 ...
##  $ SimpPos : chr  "F" "G" "G" "G" ...
##  $ PTS     : num  28.9 23.2 32.2 27.1 26.2 20 20.4 25 14.7 13.9 ...
##  $ GP      : int  55 50 58 60 73 65 75 73 50 59 ...
##  $ GS      : int  54 50 58 60 73 65 75 73 50 59 ...
##  $ MP      : num  35.5 33.5 36.3 37.4 34.8 32.8 34.6 33.4 31.5 32 ...
##  $ FG      : num  11.1 8.9 9.6 9.9 8.2 7.3 8 9.3 5.5 5 ...
##  $ FGA     : num  22.2 17.6 20.7 20.1 19 16 14.9 18.2 11.6 11.3 ...
##  $ FGP     : num  0.5 0.506 0.463 0.494 0.429 0.454 0.54 0.512 0.475 0.44 ...
##  $ X3P     : num  2.2 1.6 4.2 3.1 2.1 2.6 0 1.6 1 1.7 ...
##  $ X3PA    : num  6.9 4.4 11.3 8.3 6.3 6.6 0.2 5 3.2 4.4 ...
##  $ X3PP    : num  0.321 0.365 0.371 0.379 0.335 0.398 0.083 0.324 0.325 0.375 ...
##  $ X2P     : num  8.9 7.3 5.4 6.8 6.1 4.6 8 7.7 4.5 3.3 ...
##  $ X2PA    : num  15.3 13.2 9.4 11.8 12.7 9.4 14.7 13.2 8.4 6.9 ...
##  $ X2PP    : num  0.58 0.552 0.574 0.574 0.476 0.494 0.545 0.584 0.532 0.482 ...
##  $ FT      : num  4.6 3.8 8.8 4.1 7.8 2.8 4.3 4.7 2.6 2.3 ...
##  $ FTA     : num  5.9 4.6 9.6 4.6 8.8 3.3 5.4 6 3.2 2.7 ...
##  $ FTP     : num  0.768 0.842 0.914 0.905 0.886 0.833 0.806 0.78 0.811 0.831 ...
##  $ ORB     : num  1.2 0.8 0.8 1 0.8 0.7 2.5 0.5 0.7 0.5 ...
##  $ DRB     : num  7.1 3.1 4 4.1 2.2 3.2 6.7 3.6 3.6 3.8 ...
##  $ TRB     : num  8.3 3.9 4.8 5.1 3 4 9.2 4.2 4.3 4.3 ...
##  $ AST     : num  6.8 5.4 7.3 5.5 10.2 6.2 3.2 6.1 4.1 8.9 ...
##  $ STL     : num  0.9 0.9 0.9 1.1 1.1 1 1.2 1.1 0.8 1.5 ...
##  $ BLK     : num  0.6 0.7 0.3 0.8 0.1 0.2 0.8 0.3 0.2 0.4 ...
##  $ TOV     : num  3.2 2.9 3.3 2.1 4.1 2.2 2.5 2.5 2 1.9 ...
##  $ PF      : num  1.6 2.1 1.9 2.8 1.4 1.6 2.8 2.4 1.4 2.1 ...
##  $ fpcBball: int  635 635 635 635 635 635 635 635 635 635 ...

1. Survey Packages

Load in survey packages.

#### SURVEY PACKAGES
#install.packages("survey")
#install.packages("srvyr")
library(tidyverse)
library(survey)
library(srvyr)

The new ‘srvyr’ package utilizes the ‘tidyverse’ syntax.

2. Define a Survey Design Object

First, we must define a survey design object by mapping to the column with the final weights. The fpc argument tells R that the sample is finite and without replacement. This should be one number that denotes how large the sampling frame is.

### SURVEY DESIGN OBJECT
bball_des<- bballComp %>%
  as_survey_design(
    weights = wi_star, #WEIGHTS
    fpc = fpcBball # WITHOUT REPLACEMENT
  )

bball_des
## Independent Sampling design
## Called via srvyr
## Sampling variables:
##   - ids: `1` 
##   - fpc: fpcBball 
##   - weights: wi_star 
## Data variables: 
##   - Player (chr), League (chr), Age (int), wi_star (dbl), Salary (dbl), SimpPos
##     (chr), PTS (dbl), GP (int), GS (int), MP (dbl), FG (dbl), FGA (dbl), FGP
##     (dbl), X3P (dbl), X3PA (dbl), X3PP (dbl), X2P (dbl), X2PA (dbl), X2PP
##     (dbl), FT (dbl), FTA (dbl), FTP (dbl), ORB (dbl), DRB (dbl), TRB (dbl), AST
##     (dbl), STL (dbl), BLK (dbl), TOV (dbl), PF (dbl), fpcBball (int)

3. Estimating Means

Weighted graphics

### WEIGHTED GRAPHIC
ggplot(bballComp, aes(x = FGP, 
                          y = ..density.., 
                          weight = wi_star)) + 
  geom_histogram()
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

### MEANS AND SE AND CI

bball_des %>%
  summarize(fgp = survey_mean(FGP,
                              vartype = c("se", "ci")
  ))
## # A tibble: 1 × 4
##     fgp fgp_se fgp_low fgp_upp
##   <dbl>  <dbl>   <dbl>   <dbl>
## 1 0.448 0.0106   0.427   0.469

Let’s confirm our weighted estimate:

### CONFIRM
## POINT EST
num<-sum(bballComp$wi_star*bballComp$FGP)
den<-sum(bballComp$wi_star)
num/den ### CHECKS OUT 
## [1] 0.4480058
### COMPARE TO UNWEIGHTED
mean(bballComp$FGP)
## [1] 0.4463667

4. One Sample T-Test

Is the average field goal percent for professional basketball players in the 2022 season \(50\%\)?

## ONE T-TEST
### FGP 
ttest1 <- bball_des %>%
  svyttest(
    formula = FGP -.50 ~ 0,
    design = .,
    na.rm = TRUE
  )

ttest1
## 
##  Design-based one-sample t-test
## 
## data:  FGP - 0.5 ~ 0
## t = -4.9075, df = 118, p-value = 2.987e-06
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  -0.07297488 -0.03101358
## sample estimates:
##        mean 
## -0.05199423
## BACK TRANSFROM
ttest1$estimate +.5
##      mean 
## 0.4480058

5. Two Sample T-test to Compare Means

Is the average field goal percent for professional basketball players in the 2022 season different for NBA and WNBA?

## TWO T-TEST
ttest2 <- bball_des %>%
  svyttest(
    formula = FGP ~ League,
    design = .,
    na.rm = TRUE
  )

ttest2
## 
##  Design-based t-test
## 
## data:  FGP ~ League
## t = -1.6185, df = 118, p-value = 0.1082
## alternative hypothesis: true difference in mean is not equal to 0
## 95 percent confidence interval:
##  -0.069954405  0.007032711
## sample estimates:
## difference in mean 
##        -0.03146085

6. One Sample T-Test for Proportion

Although the average field goal percentage (FGP) is not \(50\%\), there are still some professional basketball players who have an FGP that is greater than \(50\%\). How many are there?

### PROPORTION FGP>50%
bball_des %>%
  mutate(FGP50=(FGP>.50))%>%
  group_by(FGP50)%>%
  summarize(p = survey_prop())
## When `proportion` is unspecified, `survey_prop()` now defaults to `proportion = TRUE`.
## ℹ This should improve confidence interval coverage.
## This message is displayed once per session.
## # A tibble: 2 × 3
##   FGP50     p   p_se
##   <lgl> <dbl>  <dbl>
## 1 FALSE 0.756 0.0358
## 2 TRUE  0.244 0.0358

Suppose that you had a friend who was a big basketball fan and they claimed that \(10\%\) percent of players with a FGP greater than \(50\%\). Do we have evidence to support that?

### ONE SAMPLE PROP
ttest_prop1 <- bball_des %>%
  svyttest(
    formula = (FGP>.50) - 0.10 ~ 0,
    design = .,
    na.rm = TRUE
  )

ttest_prop1
## 
##  Design-based one-sample t-test
## 
## data:  (FGP > 0.5) - 0.1 ~ 0
## t = 4.0252, df = 118, p-value = 0.000101
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
##  0.07317746 0.21490437
## sample estimates:
##      mean 
## 0.1440409
## BACK TRANSFROM
ttest_prop1$estimate +.1
##      mean 
## 0.2440409

7. Two Sample T-Test for Proportions

A filled bar graph is useful for visualizing differences, while conditioning in the subgroups.

### BAR GRAPH : COMPARE LEAGUES
## WEIGHTED
bballComp%>%
  mutate(FGP50=(FGP>.50))%>%
  ggplot(aes(x=League, y=wi_star, fill=FGP50))+
  geom_bar(stat="identity", position = "fill")+
  ggtitle("Weighted Sample Proportions")

Now let’s compare the proportions across the two leagues.

### TABLE
### PROPORTION FGP>50%
### COMPARE NBA and WNBA
bball_des %>%
  mutate(FGP50=(FGP>.50))%>%
  group_by(League, FGP50)%>% # ORDER MATTERS HERE BECAUSE IT CONSITIONS ON THE FIRST 
  summarize(p = survey_prop())
## # A tibble: 4 × 4
## # Groups:   League [2]
##   League FGP50     p   p_se
##   <chr>  <lgl> <dbl>  <dbl>
## 1 NBA    FALSE 0.744 0.0436
## 2 NBA    TRUE  0.256 0.0436
## 3 WNBA   FALSE 0.789 0.0598
## 4 WNBA   TRUE  0.211 0.0598

It looks like there is a difference in point estimates, is this significant?

### PROP NBA VS WNBA
### TWO SAMPLE PROP
ttest_prop2 <- bball_des %>%
  mutate(FGP50=(FGP>.50))%>%
  svyttest(
    formula = FGP50 ~ League,
    design = .,
    na.rm = TRUE
  )

ttest_prop2
## 
##  Design-based t-test
## 
## data:  FGP50 ~ League
## t = -0.61576, df = 118, p-value = 0.5392
## alternative hypothesis: true difference in mean is not equal to 0
## 95 percent confidence interval:
##  -0.1921271  0.1009846
## sample estimates:
## difference in mean 
##        -0.04557125

We can also look at position:

### COMPARE POSITIONS
bballComp%>%
  mutate(FGP50=(FGP>.50))%>%
  ggplot(aes(x=SimpPos, y=wi_star, fill=FGP50))+
  geom_bar(stat="identity", position = "fill")+
  ggtitle("Weighted Sample Proportions")

Now make a table:

### TABLE
### PROPORTION FGP>50%
### COMPARE SimpPos
bball_des %>%
  mutate(FGP50=(FGP>.50))%>%
  group_by(SimpPos, FGP50)%>% # ORDER MATTERS HERE BECAUSE IT CONSITIONS ON THE FIRST 
  summarize(p = survey_prop())
## # A tibble: 6 × 4
## # Groups:   SimpPos [3]
##   SimpPos FGP50      p   p_se
##   <chr>   <lgl>  <dbl>  <dbl>
## 1 C       FALSE 0.406  0.0859
## 2 C       TRUE  0.594  0.0859
## 3 F       FALSE 0.906  0.0407
## 4 F       TRUE  0.0942 0.0407
## 5 G       FALSE 0.819  0.0497
## 6 G       TRUE  0.181  0.0497

It looks like there is a difference in point estimates, is this significant?

8. Chi-Squared Test for Independence

We can make a table for this

#install.packages("gt")
library(gt)

### TABLE
chi_CI <- bball_des %>%
  mutate(FGP50=(FGP>.50))%>%
  group_by(SimpPos, FGP50) %>%
  summarize(Observed = round(survey_mean(vartype = "ci"), 3))

chi_CI 
## # A tibble: 6 × 5
## # Groups:   SimpPos [3]
##   SimpPos FGP50 Observed Observed_low Observed_upp
##   <chr>   <lgl>    <dbl>        <dbl>        <dbl>
## 1 C       FALSE    0.406        0.236        0.576
## 2 C       TRUE     0.594        0.424        0.764
## 3 F       FALSE    0.906        0.825        0.986
## 4 F       TRUE     0.094        0.014        0.175
## 5 G       FALSE    0.819        0.72         0.917
## 6 G       TRUE     0.181        0.083        0.28
### FORMAT INTO TABLE

chi_CI_tab <- chi_CI  %>%
  mutate(prop = paste0(
    Observed, " (", Observed_low, ", ",
    Observed_upp, ")"
  )) %>%
  select(FGP50, SimpPos, prop) %>%
  pivot_wider(
    names_from = SimpPos,
    values_from = prop
  ) %>%
  gt(rowname_col = "SimpPos")%>%
  tab_stubhead(label = "SimpPos")

chi_CI_tab
FGP50 C F G
FALSE 0.406 (0.236, 0.576) 0.906 (0.825, 0.986) 0.819 (0.72, 0.917)
TRUE 0.594 (0.424, 0.764) 0.094 (0.014, 0.175) 0.181 (0.083, 0.28)

We could also do a chi-squared test of homogeneity:

### CHI SQR
### LEAGUE vs FGP50
chiSq <- bball_des %>%
  mutate(FGP50=(FGP>.50))%>%
  svychisq(
    formula = ~ FGP50 + SimpPos ,
    design = .,
    statistic = "Chisq",
    na.rm = TRUE
  )

chiSq
## 
##  Pearson's X^2: Rao & Scott adjustment
## 
## data:  NextMethod()
## X-squared = 24.473, df = 2, p-value = 4.25e-07