---
title: "Lab 06: Coding Style"
author: "Your Name Here"
date: "Date Here"
format: html
---

### Exercise 1

In the code below, check for the following, to make sure the code adheres to good coding style. 

- Ensure comment is on two lines instead of one. Each line should start with a `#`.

- Put spaces after each comma within each set of `()`

- Put spaces before and after each `=`,  `-`, `*`, and `<-`

- Rename the function to something meaningful, adhering to naming guidelines. (For context on the function, see the comment as well as the description below the code. You don't need to understand fully what the function is doing, but see if you can get some clues. The name can be general, but it should be more informative than it is currently.)

- Update the comment with the new name of the function.

- Split the function into three lines, following guidelines for `{}`

```{r}
#| label: ex-1
#| eval: FALSE

# f2 is a function that finds a distance between points on two normal distributions that the user specifies.

f2<-function(x,m1,m2,sd1,sd2){0.5*abs(dnorm(x,mean=m1,sd=sd1)-dnorm(x,mean=m2,sd=sd2))}

```

Taken from Wikipedia: Normal distributions are important in statistics and are often used in the natural and social sciences to represent real-valued random variables whose distributions are not known. Their importance is partly due to the central limit theorem. It states that, under some conditions, the average of many samples (observations) of a random variable with finite mean and variance is itself a random variable—whose distribution converges to a normal distribution as the number of samples increases. Therefore, physical quantities that are expected to be the sum of many independent processes, such as measurement errors, often have distributions that are nearly normal.

### Exercise 2

In the code below, check for the following, to make sure the code adheres to good coding style:

- Each `+` should be followed by a line break.

- There should be spaces after commas and around `=` and `+`

- Break up `geom_vline()` and `labs()` into multiple lines

- Remove any unnecessary spaces within parentheses

- Indent each label within `labs()` to a new line. Make sure each line below `labs()` is indented by two spaces relative to the first line. (Note: you do not need to change any of the actual titles here.)

```{r}
#| label: ex-2
#| eval: FALSE

ggplot( data=boot_dist,aes(x=boot_props))+geom_histogram(binwidth=0.01,color="darkblue",fill="skyblue" ) + 

geom_vline( xintercept=c(ex_5_interval$lower,ex_5_interval$upper),color = "darkred",lwd=2 ) +

labs( x="Bootstrap proportions",y="Count",title="Most Durham residents satisfied with parks and recreation",subtitle = "1,000 bootstrap replicates with 99% CI shown" )

```


 

### Exercise 3

In the code below, check for the following, to make sure the code adheres to good coding style:

- Break the comment into multiple lines. Each line should start with a `#`.

- Rename the dataset to `nc_bike_crash`

- Ensure line breaks after each `|>`

- Rename `prubability_needing_ambulance_after_crash`

- Ensure proper spacing throughout code.

```{r ex-3, eval = F}

# You may have noticed that ncbikecrash isn't a great dataset name. Give it a better name in the code chunk below. Make sure that everything in the chunk conforms to tidyverse style guidelines, including this comment!


ncbikecrash |> group_by(rural_urban) |> count(ambulance_req) |>

mutate(prubability_needing_ambulance_after_crash = n/sum(n))|>filter(ambulance_req=="Yes")|>select(rural_urban,prob)

```


## Exercises 4-5: On your Own

Now, use what you've learned to edit the code in Exercises 4-5 so that they adhere to the style guidelines.

### Exercise 4

```{r}
#| label: ex-4
#| eval: false
d<-data.frame(grp=c("A","A","B","B","C","C"),val=c(3,5,4,6,2,7))
ggplot(d,aes(x=grp,y=val,fill=grp))+geom_bar(stat="identity")+theme_bw()+labs(title="average value by group")+theme(axis.text.x=element_text(angle=45,hjust=1))
```


### Exercise 5

The code below is performing a bootstrap simulation to estimate the sampling distribution and confidence interval for the proportion of people in Durham who are satisfied with parks and recreation quality. 

(Hint: One of the changes you should make is changing `ex_5_dat` to a more meaningful name.)

```{r}
#| label: ex-5
#| eval: FALSE

set.seed(5)

boot_boot_boot_dist<-numeric(1000) 

ex_5_dat<-durham |> filter(quality_parks_rec!=9)

for(i in 1:1000){

indices <- sample(1:nrow(ex_5_dat), replace = T)



boot_prop<-ex_5_dat |>

slice(indices )|>

mutate(parks_satisfied = ifelse(quality_parks_rec > 3,1,0)) |> 

summarize(prop_parks = mean(parks_satisfied))|>pull()
boot_dist[i] <- boot_prop}

boot_dist <- tibble(boot_props=boot_dist)

ex_5_interval <- boot_dist |> summarize(lower=quantile(boot_props,0.005),upper=quantile(boot_props,0.995))|>round(3)

```


## AI Attestation

I have not used AI on this assignment

[Type Your Name Here if you agree.]

