---
title: "Simple Random Sampling"
author: "YOUR NAME HERE"
date: today
format: html
execute: 
  error: true
  warning: false
  message: false
---

```{r}
library(sampling);library(survey)
```

# Introduction

This section will aim to answer the following questions: 

* How can we draw a simple random sample of observations from a data set both with, and without replacement: 
* What is the finite population correction (fpc) and why do we need it? 
* What are sampling weights, why do we need them, and how are they created? 
* How do we calculate parameter estimates from an SRS that account for both the fpc and sampling weights? 

# Drawing a Simple Random Sample (Lohr Ch 2.3)

Recall there are two ways to draw simple random samples, with and without replacement.

::: callout-important
### Definition: Simple random sample (SRSWR) with replacement:


:::

::: callout-important
### Definition: Simple random sample (SRS) without replacement:


:::

## Intentionality in sampling

Random does not mean haphazard, contrary it's actually quite intentional. Avoid selecting a sample that you "feel" is random or representative of the population. These practices can lead to....

To avoid the haphazard nature of "blindly choosing", or worse looking at what was sampled and changing it because "it doesn't look random enough", we use ....

## Drawing a SRS using a computer

::: callout-important
### Process 

1.  
2.  
3.  
4.  
:::

:::{.callout-tip icon=true}
### Example: Sampling Statistics PhD programs. 
To _demonstrate_ this example, lets only grab the first 10 programs in the StatisticsPhD data set. From that we'll draw a sample of $n=4$ programs out of these 10. 

```{r}
#| eval: false
stats <- readr::read_csv(here::here("data", "StatisticsPhD.csv"))
(stats10 <- )
```
:::

### Using the `sample()` function

**without replacement**
```{r}
#| eval: false
pop.idx <-   # Steps 1, 2
(idx <- ) # Step 3 
stats10[idx,]               # Step 4
```

* Rows ...... were chosen. 

**with replacement**
```{r}
#| eval: false
set.seed(4134) # set seed for demonstration of duplicate records
(idx.with.dups <- #3 
stats10[idx.with.dups,] #4
```

* Rows ..... were chosen. 
* Carnegie (row 4) was sampled twice. 

## Using the `sampling` package

* The functions `srswr(n,N)` and `srswor(n,N)` draw SRS with, and without replacement respectively. 
* Each take two arguments: $n$ the sample size, and $N$ the population size. 
    - The vector returned is a vector of length $N$ that indicates how many times that position in the vector is selected.  
    Then the `getdata()` function is used to extract the values from the population the indicated number of times.

**without replacement**

```{r}
#| eval: false
(choose.these.wor <- ) # Steps 1,2,3
getdata(stats10, )     # Steps 4
```


**with replacement**
```{r}
#| eval: false
set.seed(4134)

```

* Again, notice Columbia was chosen twice when sampling with replacement. 
* Interestingly, the result from sampling without replacement added a column called `ID_unit` on it that denotes the row it was sampled from. 

:::{.callout-warning icon=false}
### You try it 

The U.S. government conducts a Census of Agriculture every five years, collecting data on all farms (defined as any place from which \$1000 or more of agricultural products were produced and sold). The file `agpop.csv` (textbook data) contains historical information from 1982, 1987, and 1992 on the number of farms, total acreage devoted to farms, number of farms with fewer than 9 acres, and number of farms with more than 1000 acres for the population consisting of the $N=3078$ counties and county-equivalents in the United States.

Draw a sample of 300 farms without replacement using both `sample` and `srswor`. Save one of these data frames with the name `ag.srs` for later use. 
:::




# Formulas for Estimation

Below is a table of common statistics and how they are estimated under the SRS framework. This table can also be found on the [formulas](https://sampling-458.netlify.app/notes/formulas.html) page. 

| Measure    | Unbiased Estimate $(\hat{\theta})$         | Estimated variance of $(\hat{\theta})$                             |
|------------------|-------------------------|-----------------------------|
| Mean       | $\bar{y} = \frac{1}{n}\sum_{i\in S} y_{i}$ | $\hat{V}(\bar{y}) = (1-\frac{n}{N})\frac{s^{2}}{n}$                |
| Total      | $\hat{\tau} = N\bar{y}$                    | $\hat{V}(\hat{\tau}) = N^{2}\hat{V}(\bar{y})$                      |
| Proportion | $\hat{p} = \bar{y}$                        | $\hat{V}(\hat{p}) = (1-\frac{n}{N})\frac{\hat{p}(1-\hat{p})}{n-1}$ |

* $i \in S$ : Unit $i$ is an element in the sample $S$

Did you notice something different about the formula for the variances? 

::: callout-important
### Finite Population Correction

\vspace{2cm} 

The larger % of the population that you include in your sample (sampling fraction = $\frac{n}{N}$), the closer you are to a census, the smaller the variability your estimate will have.
:::

* Most samples that are taken from a very large population, the fpc is close to ____.
* So the variance is more determined by ___________, not the % of the ___________.

:::{.callout-tip icon=true}
### Example
Using the formulas provided, calculate the estimated standard deviation of the sample mean $\sqrt{\hat{V}(\bar{y})}$ of the the number of acres devoted to farms in 1992 (variable `acres92`). Interpret this number in an english sentence. 
:::

```{r}

```

Sample means generated from samples of size ________ will vary from sample to sample by _______________ acres. 


:::{.callout-warning icon=false}
### You try it
Using a random sample of 500 farms, estimate the standard deviation of the sample proportion of the number of farms with less than 200,000 acres. 

Hint(s)

* Use the `srswor` function to draw the sample
* create an binary indicator to identify farms meeting the criteria
* use the `mean` function to calculate the sample proportion
* use the formula to calculate the sd of the proportion
:::

:::{.callout-note icon=false collapse="true" appearance=minimal}
### Clean up
As we go through these notes, we create a lot of the same named objects, like `n` and `N`, or `idx` and `idx2`. Every once in a while its a good idea to clean out your global environment to not get confused. Use the `rm()` function to remove everything except the population `ag`, our sample of 300 `ag.srs`, and their respective sample sizes: `n` and `N`. 
```{r}
#| eval: false
ls()[!ls() %in% c()]
```

:::

# Sampling Weights (Lohr Ch 2.4)

Recall that a goal of sampling is to obtain a representative sample, one that is similar to the true unknown population. Thus, conceptually if we duplicate certain units from our sample a certain amount of times, we could "reconstruct" what the population looks like. That is, we could create ....... copies of unit ....... for each unit in the sample. 

::: callout-important
### Definition: Sampling Weight (Design weight)


:::

In an SRS, each unit has an inclusion probability of $\delta_{i} = \frac{?}{?}$, so the sampling weights are all $w_{i} =\frac{?}{?}$. 

We don't _actually_ make $w_{i}$ copies of record $i$, but use these weights as a multiplier in our estimation calculations. 

|         Population size         |                   Total                    |                   Mean                    |
|:---------------------:|:-----------------:|:-----------------------:|
| $\hat{N} = \sum_{i \in S}w_{i}$ | $\hat{\tau} = \sum_{i \in S}w_{i}y_{i}$ | $\bar{y} = \frac{\hat{\tau}}{\hat{N}}$ |

These weighted estimators are used in all probability sampling designs. 

:::{.callout-tip icon=true}
### Example: Calculating weighted estimates
Estimate the total and average number of acres devoted to farms in 1992 using both weighted and unweighted estimates. Then compare these values to the parameter. 
:::

The sampling weights are $w_{i} = \frac{?}{?}$ for each unit $i$ in the sample, so we'll add that on as a new column before calculating the weighted estimates. 
```{r}

```

Calculate weighted and unweighted estimates, then the pop parameters. 
```{r}
#| eval: false
tau.hat.wt <- 
y.bar.wt   <- 
tau.hat.nowt <- 
y.bar.nowt   <- 
mu  <- 
tau <- 
```

Package it in a data frame for easier viewing. 
```{r}
#| eval: false
data.frame(
  Measure = c("Total", "Mean"), 
  Parameter = c(tau,mu),
  Unweighted = c(tau.hat.nowt, y.bar.nowt), 
  Weighted = c(tau.hat.wt, y.bar.wt)
  ) |> knitr::kable(align = 'lccc', 
             caption = "Comparing weighted and unweighted estimates")
```

Note that when we have an SRS with equal sampling weights $w_{i} = \frac{N}{n}$, the weighted mean is equal to the weighted mean. 

:::{.callout-warning icon=false}
### You try it
Calculate the proportion of farms with less than 200k acres, with, and without weights. Compare to the population proportion. 
:::

# Analysis using the `survey` package (Lohr Ch 2.6)

Survey designs are specified using the `________` function. The main arguments to the the function are id to specify sampling units, weights to specify sampling weights, and fpc to specify finite population size corrections. These arguments should be given as formulas, referring to columns in a data frame given as the data argument.
```{r}

```

The `id` argument specifies the clusters. We are not using any clustering, so each unit has it's own id. 

## Point and interval estimates

Survey design adjusted estimates can be obtained using `svymean` and `svytotal`. 
```{r}

```

Estimates for multiple variables can be obtained at the same time. 
```{r}

```

You can pass the results to construct confidence intervals. 
```{r}

```

> interpretation here 

:::{.callout-warning icon=false}
### You try it
Using your sample of 300 farms, estimate the total number of farms in the United States in 1987 and the average farm size in acres for the same year. Report both point and interval estimates. 
:::



# Topic Summary

:::{.callout-note appearance=minimal}
### Summarize your understanding
Using the notes in this document, and Lohr Ch 2.11, summarize your understanding of this section. See Canvas for details.
:::

