1

I'm using the following code to try to transform my response variable for regression. Seems to need a log transformation.

bc = boxCox(auto.tf.lm)

lambda.mpg = bc$x[which.max(bc$y)]

auto.tf.bc <- with(auto_mpg, data.frame(log(mpg), as.character(cylinders), displacement**.2, log(as.numeric(horsepower)), log(weight), log(acceleration), model_year))

auto.tf.bc.lm <- lm(log(mpg) ~ ., data = auto.tf.bc)
view(auto.tf.bc)

I am receiving this error though.

Error in Math.data.frame(mpg) : 
  non-numeric variable(s) in data frame: manufacturer, model, trans, drv, fl, class

Not sure how to resolve this. The data is in a data frame, not csv.

Here's the output from str(auto.tf.bc). Sorry for such bad question formatting.

'data.frame':   392 obs. of  7 variables:
$ log.mpg.               : num  2.89 2.71 2.89 2.77 2.83 ...
$ as.character.cylinders.: chr  "8" "8" "8" "8" ...
$ displacement.0.2       : num  3.14 3.23 3.17 3.14 3.13 ...
$ log.horsepower.        : num  4.87 5.11 5.01 5.01 4.94 ...
$ log.weight.            : num  8.16 8.21 8.14 8.14 8.15 ...
$ log.acceleration.      : num  2.48 2.44 2.4 2.48 2.35 ...
$ model_year             : num  70 70 70 70 70 70 70 70 70 70 ...

removing the cylinders doesn't change anything.

  • Welcome to Stack Overflow! Could we please have a [mcve]? Otherwise it will very hard/you are unlikely to get an answer (see also https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example ) At the very least, can you edit your question to include `str(auto_mpg)` or `summary(auto_mpg)` ? – Ben Bolker Jul 28 '21 at 21:57
  • Likely to be a duplicate of https://stackoverflow.com/questions/6964006/error-in-math-data-frame-non-numeric-variable-in-data-frame (but can't be sure without a [mcve]) – Ben Bolker Jul 28 '21 at 21:59
  • to clarify, that's `str(auto.tf.bc)` ? – Ben Bolker Jul 28 '21 at 22:28
  • Yes, my apologies. – jamerson11228 Jul 28 '21 at 22:33
  • My goal here is to successfully run the lm() function with my auto.tf.bc data frame. – jamerson11228 Jul 28 '21 at 22:34
  • OK. We still need a [mcve] ... – Ben Bolker Jul 28 '21 at 22:44
  • 1
    Why do you convert Cylinders to character? I think this is the major issue which I am sure giving this error Error in Math.data.frame(mpg) – Jovan Jul 29 '21 at 02:36

0 Answers0