Reputation: 233
I have a dataframe which is a time series. I am using the function lm
to build a multivariate regression model.
linearmodel <- lm(Y~X1+X2+X3, data = data)
lm()
object as the input.car::residualPlot
) gives residuals on the Y-axis and fitted-values on the Y-axis.lm()
is time agnositc. So, I can live with if the residuals are on Y-axis in the same order as the data input and nothing on the X-axisplot<- plotresidualsinorder(linearmodels)
should give me the residuals on Y-axis in the same order of the data input?My research led me to car package, which is wonderful in its own right, but doesn't have the function to solve my problem.
Many thanks in advance for the help.
Upvotes: 1
Views: 1210
Reputation: 3
Calisto's solution will work, but there is a more simple and straightforward solution. The lm function already give to you the regression residuals. So you may simply pass:
plot(XTime, linearmodel$residuals, main = "Residuals")
XTime is the Date variable of your dataset, maybe you may require to format that with POSIX
functions: https://www.rdocumentation.org/packages/base/versions/3.6.2/topics/as.POSIX*
Add parameters as you need to share it on R-shiny.
Upvotes: 0
Reputation: 3240
You can use the Residual Plot information. For the proposed solution, we need to apply the lm
function to a formula that describes your Y
variables by the variables X1+X2+X3
, and save the linear regression model in a new linearmodel
variable. Finally, we compute the residual with the resid
function. In your case, the following solution can be representative for your problem.
Proposed solution:
linearmodel <- lm(Y~X1+X2+X3, data = data)
lm_resid <- resid(linearmodel)
plot(data$X1+X2+X3, lm_resid,
ylab="Residuals", xlab="Time",
main="Data")
abline(0, 0)
For any help concerning how does the resid
function works, you can try:
help(resid)
Upvotes: 1