Whosane27
Whosane27

Reputation: 1

Finding residuals in r

Having created a least-squares regression model using a data set with certain set of x and y values, how do I then use an x value that is not from the original data set to find residual of the y-value corresponding to that x-value?

When I use resid(lm(y~x)), it gives me the residuals of all the original points/observations, but I am interested in finding out residual for a point on the regression line that was not part of the observations in the original dataset.

Upvotes: 0

Views: 2379

Answers (1)

Kozolovska
Kozolovska

Reputation: 1119

This snippet of code should give you an idea. I generate data, fit a model and use it to predict a new X vector, finding the residuals for it.

# creating data 

x <- rnorm(1000)
y <- x * 2 + rnorm(1000)
new_x <- rnorm(1000)
new_y <- new_x * 2 + rnorm(10000)

# creating model 
lm_model <- lm(y ~ x)

# predicting using model new data 
y_hat_new <- predict(lm_model, data.frame(new_x)) # data must be in data.frame
new_resid <- new_y - y_hat_new

Upvotes: 3

Related Questions