cvAll: Cross Validate all nested models

Description Usage Arguments Value Examples

Description

It fits all subset regression, allowing interaction and categorical covariates, and cross validates them. The code borrows heavily from package meifly.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
cvAll(x, ...)

## Default S3 method:
cvAll(x, data, k, lossfn = .calcMSE, ind, finalfit = TRUE)

## S3 method for class 'lm'
cvAll(x, data, k = 5, ind = NULL, ...)

## S3 method for class 'lme'
cvAll(x, data, k = 5, ind = NULL, ...)

## S3 method for class 'merMod'
cvAll(x, data, k = 5, ind = NULL, ...)

validateAll(x, data, method, valid_prob, ...)

Arguments

x

Either an lm object or a two side formula.

data

Data.

k

Postive integer. k-fold cross validation is carried out. Defaults to 5.

lossfn

A function with 2 arguments. The first takes an array of predicted value, the second takes a vector of observed value. Then it calculates the loss. It defaults to a function which calculates the mean sqaured error (MSE).

finalfit

if TRUE models are refitted to the entire dataset at the end.

valid_prob

Positive number between 0 and 1. It is the proportion of data used in the validation set.

Value

An maxtrix of predictions on the validation set, with the following attributes:

loss The loss calculated by lossfn.

data The data used.

models The fitted models or just formulae of models, depending on finalfit.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
## Using cvAll

allreg=cvAll(conc~uptake*Treatment,CO2)
allreg
attr(allreg,'models') 

calcmae=function(pred,obs)mean(abs(pred-obs),na.rm=TRUE)
allreg=cvAll(conc~uptake*Treatment,CO2,lossfn=calcmae)

## Using cvAll with class lm
lm0=lm(conc~uptake*Treatment,CO2)
allreg2=cvAll(lm0,CO2)

kohleth/kmisc documentation built on May 20, 2019, 12:53 p.m.