I am using linear regression to get the slope of some data. If i know the slope how can i flatten the line so that it has no slope?
$\begingroup$
$\endgroup$
4
-
$\begingroup$ Do you mean that you have a $y$ variable predicted by $x$ and want to fit a model $y = a + bx$ but know $b$? $\endgroup$– DaveCommented Jul 28, 2019 at 18:00
-
$\begingroup$ Set $\beta_i=0$? $\endgroup$– DigioCommented Jul 28, 2019 at 18:15
-
1$\begingroup$ What would be your reason for 'detrending'? $\endgroup$– BruceETCommented Jul 28, 2019 at 18:21
-
$\begingroup$ 1. Do you know the slope (as per title) or are you estimating it (as per body text)? 2. What are you trying to achieve? $\endgroup$– Glen_bCommented Jul 28, 2019 at 23:19
Add a comment
|
1 Answer
$\begingroup$
$\endgroup$
3
If you have the dataset $y_i = \beta x_i + \epsilon_i$ and you use linear regression to obtain the least square estimate $\hat{\beta}$, your fitted values are $\hat{y}_i = \hat{\beta}x_i$. Therefore, you detrend your dataset by calculating the difference $y_i - \hat{y}_i$.
Here an R example:
## generate a dataset and plot it
x = 1:20 # predictor variable
y = 0.5*x + rnorm(length(x), 0,1) # response variable = slope + noise
plot(x,y) # plot data
## now fit the data
df = data.frame(x,y) # generate a dataframe
fit.lm = lm(y~x, df) # linear regression
summary(fit.lm) # display result of lin. reg. => get estimator of the slope
## Obtain the residual of the fit = retreaded data
yHat = fitted(fit.lm) # get the fitted values
epsilon = y - yHat # one way to obtain residuals
## There exists more direct ways to obtain the residual:
resid(fit.lm) # second way to obtain residuals
fit.lm$residuals # third way
-
2$\begingroup$ OP said "If i know the slope". So I think you do not need to estimate the slope. Also your model does not have intercept, but your fitted model has intercept. $\endgroup$ Commented Jul 28, 2019 at 19:07
-
1$\begingroup$ This depends on the meaning of "knowing". The OP starts the question with "I am using linear regression to get the slope of some data." Therefore, I reckon that the fit is the source of knowledge. If this is not the case my answer is probably false anyway and I am going to delete it. $\endgroup$– NotMeCommented Jul 28, 2019 at 19:14
-
1$\begingroup$ That is the reason I voted to close, because I really cannot figure out what is asked for. $\endgroup$ Commented Jul 28, 2019 at 19:17