-2

I'm new to R and I cannot figure out the following:

mydata <- read.table("<PATH>", header=TRUE )

mydata has 3 columns, N, Age and Expenditures.

I've tried:

 predict(mydata$Expenditure, data.frame(Age=c(50, 25)))

 predict(mydata, data.frame(Age=c(50, 25)))

It's a simple linear regression, and I'm trying to predict Expenditure with Age 50 and 25. But it keeps returning

no applicable method for 'predict' applied to an object of class "data.frame"

I figured that this shouldn't be this hard in R, what's going wrong?

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Nathan Ruth
  • 163
  • 1
  • 1
  • 10
  • 2
    Not sure without a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), but usually the first thing you pass to `predict` is the model you've created and then you pass you new data with `newx = mydata` or some such. – Nick Criswell Oct 24 '18 at 20:45

1 Answers1

0

I did so much wrong I had no idea, but I figured it out. The correct way of doing it was by creating a "fit" with the lm() function.

mydata <- read.table("<PATH>", header=TRUE)
myfit <- lm(Expenditures ~ Age, data = mydata)
newdata <- data.frame(Age=c(50, 25))
predict(myfit, newdata)

This did the trick

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
Nathan Ruth
  • 163
  • 1
  • 1
  • 10