ASH
ASH

Reputation: 20322

Unable to get R-squared for test dataset

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

Upvotes: 2

Views: 2316

Answers (1)

Zheyuan Li
Zheyuan Li

Reputation: 73325

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.

Upvotes: 2

Related Questions