---
title: "Project 4: The Lost Hiker"
author: "Your Name"
format:
  html:
    toc: true
    code-fold: false
execute:
  echo: true
  warning: false
  message: false
---

## Model and assumptions

Describe the model in your own words. State two assumptions it makes about the hiker or the terrain, and explain one way a real lost hiker behaves differently.

## One hike

Before simulating: what is the fewest number of minutes in which the hiker could reach the road? The river?

Complete the loop. Remove `#| eval: false` when your code is ready to run.

```{r}
#| eval: false

set.seed(2026)   # you may choose a different seed
road <- 20
river <- -10
max_minutes <- 240

x <- numeric(max_minutes + 1)
y <- numeric(max_minutes + 1)
outcome <- "lost"

for (i in 2:(max_minutes + 1)) {
  # TODO: move x[i] one step east or west from x[i - 1], as in Module 9.5.
  # TODO: move y[i] one step north or south from y[i - 1].
  # TODO: if y[i] has reached the road, set outcome to "road" and break.
  # TODO: if y[i] has reached the river, set outcome to "river" and break.
}

minutes_walked <- i - 1
outcome
minutes_walked
```

```{r}
#| eval: false

# TODO: plot the path x[1:i], y[1:i] with type = "l" and axis labels.
#       Set ylim so both lines show, for example ylim = c(-12, 22).
# TODO: add the road and river with abline(h = road) and abline(h = river),
#       in different colors, and mark the starting point.
```

Describe the path and how the hike ended.

## Many hikes

```{r}
#| eval: false

set.seed(2026)
hikes <- 1000
p_north <- 0.5
max_minutes <- 240
outcome <- character(hikes)

for (hike in 1:hikes) {
  y <- 0
  outcome[hike] <- "lost"
  for (minute in 1:max_minutes) {
    # TODO: move y one step north with probability p_north, otherwise south.
    # TODO: if y has reached the road, set outcome[hike] to "road" and break.
    # TODO: if y has reached the river, set outcome[hike] to "river" and break.
  }
}

c(road = mean(outcome == "road"),
  river = mean(outcome == "river"),
  lost = mean(outcome == "lost"))
```

Now repeat with a different seed:

```{r}
#| eval: false

set.seed(4)   # a different seed
# TODO: rerun the 1,000 hikes and display the three fractions.
```

How much did the fractions change? How many digits do you trust?

## Check against an exact answer

```{r}
#| eval: false

set.seed(2026)
max_minutes <- 5000
# TODO: rerun the 1,000 hikes with this much longer time limit.
# TODO: compute the fraction that reached the road and its relative error
#       compared with 1/3, as a percentage.
```

Is the error about the size of the seed-to-seed variation? Explain in plain language why the river is twice as likely as the road.

## A pull toward the road

```{r}
#| eval: false

set.seed(2026)
max_minutes <- 240
p_values <- seq(0.50, 0.60, by = 0.01)
road_fraction <- numeric(length(p_values))

for (k in seq_along(p_values)) {
  p_north <- p_values[k]
  outcome <- character(hikes)
  # TODO: copy your 1,000-hike loop from "Many hikes" here.
  road_fraction[k] <- mean(outcome == "road")
}

data.frame(p_north = p_values, road_fraction)
```

```{r}
#| eval: false

# TODO: plot road_fraction against p_values with points and axis labels.
# TODO: find the smallest p_north with road_fraction >= 0.75, using which()[1].
```

Describe the shape of the graph. How big a change in the hiker's behavior is the `p_north` you found? Would anyone watching the hiker notice it?

## Recommendation

Write a short paragraph to a park ranger designing a trailhead sign: based on your model, what should the sign tell hikers about getting lost? Then describe at least one way your model could be wrong or limited.
