24小时热门版块排行榜    

查看: 2255  |  回复: 4
本帖产生 1 个 计算强帖 ,点击这里进行查看
当前只显示满足指定条件的回帖,点击这里查看本话题的所有回帖

snoopyzhao

至尊木虫 (职业作家)

★ ★ ★ ★ ★
luoqiquan(金币+5, 计算强帖+1):thank you very much 2010-11-05 20:30:43
贴一个我用 R 语言写的 LOO-LGO CV 的程序,应该能够看得懂,至于其它语言如何实现,我就无能为力了……
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))
}

3楼2010-11-05 20:09:14
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

智能机器人

Robot (super robot)

我们都爱小木虫

找到一些相关的精华帖子,希望有用哦~

科研从小木虫开始,人人为我,我为人人
相关版块跳转 我要订阅楼主 songbin7957 的主题更新
普通表情 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[基金申请] 能否退出参与的面上项目解除限项 +21 koalala 2026-08-24 24/1200 2026-08-24 19:25 by 家与远方
[基金申请] 2026国自然函评费到账 +17 羊腰板 2026-08-21 19/950 2026-08-24 16:52 by iaeyuan
[基金申请] 建议基金发布提前给出明确的时间点 +13 kulium 2026-08-21 16/800 2026-08-24 16:27 by superceng
[基金申请] 今日不放榜?网传国自然预计 8 月 27 日可查结果 +17 医学老男孩 2026-08-20 21/1050 2026-08-24 14:21 by refreshing11
[基金申请] 朋友圈看到的 +7 wangzilk 2026-08-18 9/450 2026-08-24 10:50 by cmrandy
[基金申请] 让我中一个面上吧! +13 大萍1987 2026-08-20 16/800 2026-08-24 10:23 by 太傻了
[基金申请] 放榜前的不淡定 20+4 snowwithsea 2026-08-19 17/850 2026-08-24 10:20 by echo8914667
[基金申请] 什么时候开奖? +10 CrisMessi 2026-08-18 11/550 2026-08-24 06:50 by 开心的小狮子
[基金申请] 2026年的国家社科基金项目通讯评审的新规则与新动向、新挑战 +4 process2012 2026-08-23 5/250 2026-08-23 19:58 by jurkat.1640
[教师之家] 跳槽后在研项目怎么办? +5 简单化xn 2026-08-22 10/500 2026-08-23 12:38 by 简单化xn
[基金申请] 今天放榜吗? +15 布布和一二 2026-08-19 16/800 2026-08-23 09:55 by 张春生
[基金申请] 只有每年这种时候来逛逛小木虫 +24 yaoyewhu2008 2026-08-20 26/1300 2026-08-22 17:43 by kammury
[基金申请] 今天基金会出结果吗?20260819 +16 kkkl_v 2026-08-19 17/850 2026-08-22 16:12 by 阿布Abu
[基金申请] 时间戳今天,20号变了 +5 archvillain 2026-08-20 5/250 2026-08-22 06:12 by hui_daxiao
[基金申请] 时间戳又变了 +13 wuchongjun 2026-08-20 19/950 2026-08-21 17:21 by 紫杉醇
[基金申请] 应该是下周三26日公布了吧? +4 哈哈蛤? 2026-08-21 4/200 2026-08-21 10:58 by Vivilian
[基金申请] 今天放榜没戏了吧 +9 yuleib84 2026-08-19 11/550 2026-08-21 10:06 by gltch
[基金申请] 重要消息,中午系统在维护 +11 yuleib84 2026-08-18 12/600 2026-08-20 11:09 by xskun
[基金申请] 明天放榜? +5 Shxjjxjkx 2026-08-18 5/250 2026-08-18 18:14 by -大大大大大-
[基金申请] 今天维护系统维护 祝所有人 高中 +8 gjjjzhong 2026-08-18 9/450 2026-08-18 13:01 by 家与远方
信息提示
请填处理意见