Hypothesis Test for Slope
Read It: Hypothesis Test for Slope
So far in this lesson, you have learned about a hypothesis test for correlation and have been introduced to simple linear regression. Here, we will go one step further in our analysis to evaluate the significance of the slope of a line. For this analysis, we will return to our one-line linear regression command: stats.linregress
. However, before we can start the analysis, we need to define the hypotheses. For a hypothesis test of slope, the parameter of interest is . Additionally, the null hypothesis will always state , while the alternative will be some inequality (e.g., , , or ). Below is an example of a set of hypotheses for slope, which we will test in the video demonstration.
Watch It: Video - Hypothesis Test Slope (4:40 minutes)
Try It: DataCamp - Apply Your Coding Skills
Edit the following code to conduct a hypothesis test for the slope between x and y. Print the slope and p-value.
# This will get executed each time the exercise gets initialized.
# libraries
import pandas as pd
import numpy as np
import scipy.stats as stats
# create some data
df = pd.DataFrame({'x': np.random.randint(0,100,100),
'y': np.random.randint(-50,150,100)})
# one-line test
output = ...
# print results
print('slope: ', ...)
print('p-value: ', ...)
# libraries
import pandas as pd
import numpy as np
import scipy.stats as stats
# create some data
df = pd.DataFrame({'x': np.random.randint(0,100,100),
'y': np.random.randint(-50,150,100)})
# one-line test
output = stats.linregress(df['x'], df['y'])
# print p-value
print('slope: ', output.slope)
print('p-value: ', output.pvalue)