Reputation: 1893
I used the Ames data set to create the following code:
NbrMLR <- lm(SalePrice ~ Neighborhood, data = ames_housing_data)
pred <- as.data.frame(predict(NbrMLR, ames_housing_data))
library(reshape)
pred <- rename(pred,c('predict(NbrMLR, ames_housing_data)' = 'prd'))
ames_housing_data$NbrPred <- pred
ames_housing_data$NbrRes <- SalePrice - ames_housing_data$NbrPred
ames_housing_data$absoluteNbrRes <- abs(ames_housing_data$NbrRes)
NbrMAE <- mean(ames_housing_data$absoluteNbrRes)
This code results in the following error:
argument is not numeric or logical: returning NA
str(ames_housing_data$absoluteNbrRes)
tells that I have a data frame of variable type $ prd: num
. Though I'm familiar with num
, I've never heard of prd: num
before. Does this cause problem? Why does not mean()
act upon what otherwise seems to me to be a valid vector?
Upvotes: 2
Views: 187
Reputation: 73265
ames_housing_data$NbrPred <- pred[[1]]
will fix it.
Regarding your error: prd
is a variable name (as you defined it!). You pass a data frame to mean
, which causes you error.
If I were you, I would write the following code:
NbrMLR <- lm(SalePrice ~ Neighborhood, data = ames_housing_data)
ames_housing_data$NbrPred <- predict(NbrMLR, ames_housing_data)
ames_housing_data$NbrRes <- SalePrice - ames_housing_data$NbrPred
ames_housing_data$absoluteNbrRes <- abs(ames_housing_data$NbrRes)
NbrMAE <- mean(ames_housing_data$absoluteNbrRes)
Upvotes: 1