Error in eval(predvars, data, env) : object 'charges' not found

Requirement:
4) Scroll down to lines 37, and change the code based on the model you specified (only one independent variable-- AgeOfHouse).

fit <- lm(charges ~ bmi, data = mydata)

Notes:

• You will need to add a new column (AgeOfHouse=2019- yr_built) in the dataset and save your changes before running the script

My entire code:

rm(list = ls());
mydata <- read.csv("kc_house_data (1).csv",header=TRUE)
sink("RegressionOutput.txt", append=FALSE, split=TRUE)
pdf("rplot.pdf")
plot(charges ~ bmi, data = mydata)
mydata$AgeOfHouse <- 2019-yr_built
fit <- lm(charges ~ AgeOfHouse, data = mydata)
summary(fit)
mydata$charges.fit <- predict(fit)
plot(charges ~ charges.fit, data = mydata)
pairs(charges~ bmi + age + male + children + smoker, data = mydata)
res <- cor(mydata)

round(res, 2)
mfit <- lm(charges~ bmi + age + male + children + smoker, data = mydata)

summary(mfit)

mydata$charges.mfit <- predict(mfit)

plot(charges ~ charges.mfit, data = mydata)
dev.off()
sink();

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. Also, please review the homework policy.

What did you have in your namespace, as shown by ls() before

rm(list = ls());

Was mydata there? Is it still there?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.