titi
titi

Reputation: 619

In R, how to add the fitted value column to the original dataframe?

I have a multiple regression model. I want to add the fitted values and residuals to the original data.frame as two new columns. How can I achieve that? My model in R is like this:

BD_lm <- lm(y ~ x1+x2+x3+x4+x5+x6, data=BD)
summary(BD)

I also got the fitted value

BD_fit<-fitted(BD_lm)

But I want to add this BD_fit values as a column to my original data BD. I don't know how. When I tried to call BD_fit, it just gave me a lot of numbers. I am running a large dataset, so it is hard to list all of them here.

Upvotes: 14

Views: 35139

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 269644

Suppose:

fm <- lm(demand ~ Time, BOD)

Then try this:

cbind(BOD, resid = resid(fm), fitted = fitted(fm))

or this:

BOD$resid <- resid(fm)
BOD$fitted <- fitted(fm)

ADDED:

If you have NA values in demand then your fitted values and residuals will be of a different length than the number of rows of your data, meaning the above will not work. In such a case use: na.exclude like this:

BOD$demand[3] <- NA # set up test data
fm <- lm(demand ~ Time, BOD, na.action = na.exclude)

na.exclude will automatically pad the predictions and residuals with NA values so that they are of the same length as the original data. Now the previous lines should work.

Upvotes: 26

Metrics
Metrics

Reputation: 15458

BD_fit<-data.frame(BD_fit)
BD$fit<-BD_fit[1]

Upvotes: 0

Florian R. Klein
Florian R. Klein

Reputation: 1435

Despite not knowing your case in detail, adding to a data frame is quite easy. You could jsut add a new column like so:

df <- data.frame(var1=1:10)
df$var2 <- 11:20

You only have to make sure that your additional data columns have the same length as the original ones. Otherwise, you won't be able to add them to your data frame.

Upvotes: -1

Related Questions