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