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

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

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

问题

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

  1. library(mgcv)
  2. x=runif(100)
  3. x2=runif(100)
  4. y=sin(x)+exp(x2) + rnorm(0,1)
  5. m=gam(y~s(x,bs="cr",k=4)+s(x2,bs="cr",k=4),method="REML")
  6. 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:

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

Thanks.

答案1

得分: 3

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

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

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

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

  1. residual.df <- length(object$y) - sum(object$edf)
  2. w <- as.numeric(object$prior.weights)
  3. mean.y <- sum(w * object$y)/sum(w)
  4. nobs <- nrow(object$model)
  5. r.sq <- if (inherits(object$family, "general.family") ||
  6. !is.null(object$family$no.r.sq))
  7. NULL
  8. else 1 - var(w * (as.numeric(object$y) - object$fitted.values)) *
  9. (nobs - 1)/(var(w * (as.numeric(object$y) - mean.y)) *
  10. 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:

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

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

  1. residual.df &lt;- length(object$y) - sum(object$edf)
  2. w &lt;- as.numeric(object$prior.weights)
  3. mean.y &lt;- sum(w * object$y)/sum(w)
  4. nobs &lt;- nrow(object$model)
  5. r.sq &lt;- if (inherits(object$family, &quot;general.family&quot;) ||
  6. !is.null(object$family$no.r.sq))
  7. NULL
  8. else 1 - var(w * (as.numeric(object$y) - object$fitted.values)) *
  9. (nobs - 1)/(var(w * (as.numeric(object$y) - mean.y)) *
  10. 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:

确定