广义加性模型中调整后的 R^2 的公式是什么?在 mgcv 包中。

huangapple go评论84阅读模式
英文:

What is the formula behind adjusted R^2 for generalized additive model in mgcv package?

问题

在使用R包mgcv拟合广义加性模型后,从摘要中可以找到R^2(adjusted)。我想知道它是如何计算的?我的意思是它的公式是什么?

library(mgcv)
x=runif(100)
x2=runif(100)
y=sin(x)+exp(x2) + rnorm(0,1)
m=gam(y~s(x,bs="cr",k=4)+s(x2,bs="cr",k=4),method="REML")
summary(m)

谢谢。

英文:

After fitting generalized additive models using the R package mgcv, from the summary, I can find R^2(adjusted). I want to know how it is actually calculated? I mean what is its formula?
Code:

library(mgcv)
x=runif(100)
x2=runif(100)
y=sin(x)+exp(x2) + rnorm(0,1)
m=gam(y~s(x,bs="cr",k=4)+s(x2,bs="cr",k=4),method="REML")
summary(m)

Thanks.

答案1

得分: 3

我试图查看mgcv的源代码,但无法找到r2。

如果您查看print.summary.gam的源代码,您会看到以下内容:

if (!is.null(x$r.sq)) 
    cat("R-sq.(adj) = ", formatC(x$r.sq, digits = 3, width = 5), 
        "  ")

如果您查看summary.gam的源代码,您会看到以下内容:

residual.df <- length(object$y) - sum(object$edf)

w <- as.numeric(object$prior.weights)

mean.y <- sum(w * object$y)/sum(w)

nobs <- nrow(object$model)

r.sq <- if (inherits(object$family, "general.family") || 
        !is.null(object$family$no.r.sq)) 
        NULL
    else 1 - var(w * (as.numeric(object$y) - object$fitted.values)) * 
        (nobs - 1)/(var(w * (as.numeric(object$y) - mean.y)) * 
        residual.df)

希望这些代码片段对您有帮助。

英文:

> i try to look at the source code of mgcv but i am unable to find out
> r2

If you look at the source code of print.summary.gam, you see this:

if (!is.null(x$r.sq)) 
        cat(&quot;R-sq.(adj) = &quot;, formatC(x$r.sq, digits = 3, width = 5), 
            &quot;  &quot;)

If you look at the source code of summary.gam, you see this:

residual.df &lt;- length(object$y) - sum(object$edf)

w &lt;- as.numeric(object$prior.weights)

mean.y &lt;- sum(w * object$y)/sum(w)

nobs &lt;- nrow(object$model)

r.sq &lt;- if (inherits(object$family, &quot;general.family&quot;) || 
        !is.null(object$family$no.r.sq)) 
        NULL
    else 1 - var(w * (as.numeric(object$y) - object$fitted.values)) * 
        (nobs - 1)/(var(w * (as.numeric(object$y) - mean.y)) * 
        residual.df)

huangapple
  • 本文由 发表于 2023年6月9日 11:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定