英文:
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("R-sq.(adj) = ", formatC(x$r.sq, digits = 3, width = 5),
" ")
If you look at the source code of summary.gam
, you see this:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论