Unable to get R-squared for test dataset
Asked Answered
L

1

2

I am trying to learn a bit about different types of regression and I am hacking my way through the code sample below.

library(magrittr)
library(dplyr)


# Polynomial degree 1
df=read.csv("C:\\path_here\\auto_mpg.csv",stringsAsFactors = FALSE) # Data from UCI
df1 <- as.data.frame(sapply(df,as.numeric))

# Select key columns
df2 <- df1 %>% select(cylinder,displacement,horsepower,weight,acceleration,year,mpg)
df3 <- df2[complete.cases(df2),]


smp_size <- floor(0.75 * nrow(df3))
# Split as train and test sets
train_ind <- sample(seq_len(nrow(df3)), size = smp_size)

train <- mtcars[train_ind, ]
test <- mtcars[-train_ind, ]


Rsquared <- function (x, y) cor(x, y) ^ 2


# Fit a model of degree 1
fit <- lm(mpg~. ,data=train)
rsquared1 <-Rsquared(fit,test$mpg)
sprintf("R-squared for Polynomial regression of degree 1 (auto_mpg.csv)  is : %f", rsquared1)

I am getting this error:

'Error in cor(x, y) : 'x' must be numeric'

I got the code samples from here (1.2b & 1.3a).

https://gigadom.wordpress.com/2017/10/06/practical-machine-learning-with-r-and-python-part-1/

The raw data is available here.

https://raw.githubusercontent.com/tvganesh/MachineLearning-RandPython/master/auto_mpg.csv

Looselimbed answered 9/7, 2018 at 15:42 Comment(0)
S
2

Just a few minutes ago I got an upvote for Function to calculate R2 (R-squared) in R. Now I guess it is from you, thanks.

Rsquare function expects two vectors, but you've passed in a model object fit (which is a list) and a vector test$mpg. I guess you want predict(fit, newdata = test) for its first argument here.

Sensualist answered 9/7, 2018 at 15:49 Comment(4)
Yeah, I did that up-vote. Well, your suggestion works, but hot to interpret the results? Those can't be R-squared values. Those numbers are way to high!Looselimbed
Exactly! I'm seeing this: Mazda RX4 Wag = 20.20765 & Valiant = 19.38514 & 13.44487.Looselimbed
Well, what, exactly, does 'predict(fit, newdata = test)' do?Looselimbed
Ah! Now it makes sense! Ok, thanks for everything!!Looselimbed

© 2022 - 2024 — McMap. All rights reserved.