---
title: "Speed of sound"
authod: "Andres Aravena"
date: "December 28, 2020"
---


First we read the data
```{r message=FALSE, warning=FALSE}
library(readr)
echo <- read_table2("echo_delay.txt")
summary(echo)
```

Then we make a plot. We choose `distance` so it is the independent variable. The dependent variable is `delay`.

```{r}
plot(delay ~ distance, data = echo)
```

What is the model?
```{r}
model <- lm(delay ~ distance, data = echo)
model
```

The coefficients are
```{r}
coef(model)
```

graphically
```{r}
plot(delay ~ distance, data = echo)
abline(model)
```


Now we can make a prediction
```{r}
new_distance <- c(55, 182, 212)
prediced_delay <- predict(model, 
        newdata = data.frame(
            distance=new_distance
            )
)

knitr::kable(data.frame(new_distance, prediced_delay))
```

The formula is
$$delay = A + B \cdot distance$$

The units of the coefficients are:

+ `distance` is measured in $10^{-3}m$ (millimeters)
+ `delay` is measured in $10^{-6}s$ (microseconds)
+ `coef(model)[1]` is in $10^{-6}s$
+ `coef(model)[2]` is in $10^{-6}s/10^{-3}m=10^{-3}s/m$


Therefore `1/coef(model)[2]` is in $Km/s$  and should be the speed of sound. In other words, `1000/coef(model)[2]` should be the speed of sound in $m/s$

```{r}
1000/coef(model)[2]
```

But the "distance" is only half of the "sound travel distance". Therefore, the correct speed of sound (in $m/$) is

```{r}
2000/coef(model)[2]
```

