속도와 신뢰구간 추정
국립공원 하이킹 데이터를 계속 살펴보겠습니다. 일부 거리 값이 음수인데, 이는 등산로 시작점과 반대 방향으로 걸었기 때문이에요. 데이터가 지저분하므로 전체적인 추세에만 집중하겠습니다.
이 연습에서는 부트스트랩 리샘플링을 사용해 선형 모델의 속도 값 분포를 구하고, 그 분포로부터 속도의 최적 추정치와 그 추정치에 대한 90% 신뢰구간을 계산하는 것이 목표예요. 여기서 말하는 속도는 시간의 함수로 거리를 적합하는 선형회귀 모델의 기울기 매개변수입니다.
시작을 돕기 위해 distance와 time 데이터, 그리고 각 리샘플에 대해 속도 값을 계산하는 미리 정의된 least_squares() 함수를 로드해 두었어요.

이 연습은 강의의 일부입니다
Python으로 배우는 선형 모델 입문
연습 안내
np.random.choice()를 사용해population_inds에서sample_inds를 뽑고, 각 데이터의 거리-시간 쌍이 유지되도록 하세요.- 시간 순서를 보존하기 위해
sample_inds를.sort()로 정렬한 뒤, 이를 사용해distances와times를 인덱싱하세요. least_squares(times, distances)로 선형 모델 매개변수를 계산하고,a1을resample_speeds에 저장하세요.np.mean()과np.percentiles()를resample_speeds에 적용해 속도의 점추정치와 신뢰구간ci_90을 계산한 뒤, 둘 다 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Resample each preloaded population, and compute speed distribution
population_inds = np.arange(0, 99, dtype=int)
for nr in range(num_resamples):
sample_inds = np.random.choice(____, size=100, replace=True)
sample_inds.____()
sample_distances = distances[____]
sample_times = times[____]
a0, a1 = ____(sample_times, sample_distances)
resample_speeds[nr] = ____
# Compute effect size and confidence interval, and print
speed_estimate = np.mean(____)
ci_90 = np.percentile(____, [5, 95])
print('Speed Estimate = {:0.2f}, 90% Confidence Interval: {:0.2f}, {:0.2f} '.format(____, ____[0], ____[1]))