Unable to get R-squared for test dataset

旧巷老猫 提交于 2019-12-24 01:27:49

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/51249359/unable-to-get-r-squared-for-test-dataset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!