R语言crossval包 crossval函数使用说明

您所在的位置:网站首页 r语言performance在哪个包 R语言crossval包 crossval函数使用说明

R语言crossval包 crossval函数使用说明

2024-07-15 20:06| 来源: 网络整理| 查看: 265

返回R语言crossval包函数列表

功能\作用概述:

crossval使用B重复执行K-折叠交叉验证。如果Y是一个因子,则使用平衡抽样(即在每个折叠中,每个类别以适当的比例表示)。

语法\用法:

crossval(predfun, X, Y, K=10, B=20, verbose=TRUE, ...)

参数说明:

predfun : 预测功能(详见)。

X : 预测矩阵(列对应变量)。

Y : 单变量响应变量。

K : 折叠次数。

B : 重复次数。

verbose : 如果verbose=TRUE,则在交叉验证期间会显示状态消息。

... : predfun的可选参数

示例\实例:

# load "crossval" packagelibrary("crossval")

# classification examples

# set up lda prediction functionpredfun.lda = function(train.x, train.y, test.x, test.y, negative){ require("MASS") # for lda function

lda.fit = lda(train.x, grouping=train.y) ynew = predict(lda.fit, test.x)$class

# count TP, FP etc. out = confusionMatrix(test.y, ynew, negative=negative)

return( out )}

# Student's Sleep Datadata(sleep)X = as.matrix(sleep[,1, drop=FALSE]) # increase in hours of sleep Y = sleep[,2] # drug given plot(X ~ Y)levels(Y) # "1" "2"dim(X) # 20 1

set.seed(12345)cv.out = crossval(predfun.lda, X, Y, K=5, B=20, negative="1")

cv.out$stat# FP TP TN FN #0.60 1.08 1.40 0.92

diagnosticErrors(cv.out$stat)# acc sens spec ppv npv lor #0.6200000 0.5400000 0.7000000 0.6428571 0.6034483 1.0076405

## Not run:

# Wine Data - see http://archive.ics.uci.edu/ml/datasets/Wine for detailswine.data = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data", sep=",")c = wine.data[,1]X = as.matrix(wine.data[c!=3,-1])Y = as.factor(c[c!=3])dim(X) # 130 13levels(Y) # "1", "2"

set.seed(12345)cv.out = crossval(predfun.lda, X, Y, K=5, B=50, negative="1")

cv.out$stat# FP TP TN FN # 0.028 14.092 11.772 0.108

diagnosticErrors(cv.out$stat)# acc sens spec ppv npv lor # 0.9947692 0.9923944 0.9976271 0.9980170 0.9909091 10.9125059

## End(Not run)

# linear regression example

data("attitude")y = attitude[,1] # rating variablex = attitude[,-1] # date frame with the remaining variablesis.factor(y) # FALSE

summary( lm(y ~ . , data=x) )

# set up lm prediction functionpredfun.lm = function(train.x, train.y, test.x, test.y){ lm.fit = lm(train.y ~ . , data=train.x) ynew = predict(lm.fit, test.x )

# compute squared error risk (MSE) out = mean( (ynew - test.y)^2 )

return( out )}

# prediction MSE using all variablesset.seed(12345)cv.out = crossval(predfun.lm, x, y, K=5, B=20)c(cv.out$stat, cv.out$stat.se) # 68.06480 2.91447

# and only two variablescv.out = crossval(predfun.lm, x[,c(1,3)], y, K=5, B=20)c(cv.out$stat, cv.out$stat.se) # 52.855325 1.878877

# for more examples (e.g. using cross validation in a regression or classification context)# see the R packages "sda", "care", or "binda".



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3