Chapter 10 The Model-Fitting Paradigm in R

Caution: in a highly developmental stage! See Section 1.1.

Scratch notes from our in-class activities in Lecture 3. This covers the model-fitting paradigm in R.

Use the iris data to demonstrate. Split into training and test data:

head(iris)
set.seed(100)
iris <- mutate(
    iris, 
    training = caTools::sample.split(Sepal.Width, SplitRatio=0.8))
#      training = sample(1:2, replace=TRUE, prob=c(0.8, 0.2), size=nrow(iris)))
iris_train <- filter(iris, training)
iris_test <- filter(iris, !training)
#caret::createDataPartition()

Fit a LOESS model:

fit <- loess(Sepal.Width ~ Petal.Width, data=iris_train)

Make predictions:

yhat <- predict(fit, newdata=iris_test)

Calculate error:

mean((yhat - iris_test$Sepal.Width)^2)

10.1 broom package

#tidy(fit)
#glance(fit)
broom::augment(fit, newdata = iris_test)

With linear regression:

fit2 <- lm(Sepal.Width ~ Petal.Width, data=iris_train)
#unclass(fit2)
#unclass(summary(fit2))

broom::tidy(fit2)
broom::augment(fit2)
broom::glance(fit2)