24小时热门版块排行榜    

查看: 2278  |  回复: 6
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

evenleesm1316

木虫 (小有名气)


[交流] 【求助】留一法交互验证如何做?

请问模型的预测中,留一法交互验证能够用什么软件实现?最好有详细操作步骤,谢谢啦!
也就是计算Rcv

MATLAB之类的好像可以算,但是具体怎么做不太清楚,求指教

[ Last edited by mingdong on 2011-1-15 at 21:40 ]
回复此楼

» 猜你喜欢

» 本主题相关价值贴推荐,对您同样有帮助:

» 抢金币啦!回帖就可以得到:

查看全部散金贴

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
★ ★ ★ ★ ★
evenleesm1316(金币+5): 谢谢!是不是这个专业的,还是不太懂你这个程序应该在哪个软件里面运行,请指教得白话一点好不? 2011-01-17 10:21:32
mingdong(金币+5): 感谢交流! 2011-01-17 22:52:06
引用回帖:
Originally posted by evenleesm1316 at 2011-01-16 22:26:34:


请具体请教是啥子软件啊?可以把帖子的链接给下不?我没有找到

[ Last edited by evenleesm1316 on 2011-1-16 at 22:35 ]

找不到了,再贴一遍吧。该程序不具有通用性(只能用于 R 下的线性回归的交叉验证),但它的确实现了最常见(经典)的交叉验证的算法,呵呵,要改到其它方法或语言中也是很容易的一件事情。
CODE:
lmcv <- function(obj, ng = NULL, random = FALSE) {
#
# The Leave-One-Out (LOO) and/or Leave-Group-Out (LGO) Cross-Validation in R for (Multiple) Linear Regression.
#
# Input:
#   obj: the model of MLR
#   ng: number of group, if missing, do LOO
#   random: logical, if TRUE, do random CV
#
# Output:
#   q.squared: cross-validation relation coefficient.
#   SDEP: Standard Deviation of Error of Predictions
#   newsq: variance in Y explained only for LOO CV
#
# Usage:
#   loo <- cv.lm(obj)
#   lgo <- cv.lm(obj, ng = 5)
#
    data <- data.frame(y = obj$model[,1], x = obj$model[, -1])
    col.names <- colnames(data)
    N <- nrow(data)
    if (random == TRUE) data <- data[sample(1:N),]
    if (missing(ng)) ng <- N # LOO CV
    ytest <- numeric(N)
    ypred <- numeric(N)
    newrsq <- numeric(ng)
    g <- N %/% ng
    for (i in 1:ng) {
        if (g == 1) {
            index <- i
        }
        else {
            index <- c(i, ng * seq(1, (g - 1)) + i)
            if (N %% ng != 0 & i <= N %% ng) index <- c(index, (g * ng + i))
        }
        ytest[index] <- data[index,1]
        newtrain <- data[-index, ]
        xtest <- data.frame(x = data[index, -1])
        colnames(xtest) <- col.names[-1]
        newfm <- lm(y ~., data = newtrain)
        newrsq[i] <- summary(newfm)$r.squared
        ypred[index] <- predict(newfm, xtest)
    }
    q.squared <- 1 - sum((ytest - ypred)^2) / sum((ytest - mean(ytest))^2)
    SDEP <- sqrt(sum((ytest - ypred)^2) / N)
    return(list(q.squared = q.squared, SDEP = SDEP, newrsq = newrsq))
}

4楼2011-01-16 23:27:42
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
查看全部 7 个回答

luoqiquan(金币+1):thanks 2011-01-14 20:20:11
evenleesm1316(金币+5): 2011-01-16 22:26:10
前面我好象贴过一个 R 语言的用 CV 来对线性回归模型进行检验的程序,呵呵……
2楼2011-01-14 11:01:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

evenleesm1316

木虫 (小有名气)


引用回帖:
Originally posted by snoopyzhao at 2011-01-14 11:01:18:
前面我好象贴过一个 R 语言的用 CV 来对线性回归模型进行检验的程序,呵呵……

请具体请教是啥子软件啊?可以把帖子的链接给下不?我没有找到

[ Last edited by evenleesm1316 on 2011-1-16 at 22:35 ]
3楼2011-01-16 22:26:34
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
★ ★ ★ ★ ★
mingdong(金币+5): 感谢交流! 2011-01-17 22:51:54
引用回帖:
谢谢!是不是这个专业的,还是不太懂你这个程序应该在哪个软件里面运行,请指教得白话一点好不?

上面这个程序是在 R (http://www.r-project.org) 中运行的。

我想说的是,不管能不能运行,但主要的思想在这个程序中还是体现出来了。知道了怎么留,怎么去,用自己熟悉的语言编一个不困难的……
6楼2011-01-17 11:00:27
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
普通表情 高级回复 (可上传附件)
信息提示
请填处理意见