mpiktas / midasr

R package for mixed frequency time series data analysis.
http://mpiktas.github.io/midasr/
Other
73 stars 34 forks source link

Calculation of R squared #40

Closed siddharthgopi closed 10 years ago

siddharthgopi commented 10 years ago

When I run a summary of the regression, I get the residual standard errors back, for example, 0.2704. Does this mean the R squared is (1-0.2704) = 0.7296?

Thank you

vzemlys commented 10 years ago

No. For non-linear regressions R squared is a tricky concept and you should not judge your model based on it. Here are the functions which calculate it:

  R2 <- function(z) {
r <- z$residuals
f <- z$fitted.values
mss <- if (attr(z$terms, "intercept")) 
    sum((f - mean(f))^2)
else sum(f^2)
rss <- sum(r^2)
ans <- list(r.squared=NULL,adj.r.squared=NULL)

n <- length(r)
p <- length(coef(z))
rdf <- n-p
df.int <- if (attr(z$terms, "intercept")) 1L
else 0L

ans$r.squared <- mss/(mss + rss)
ans$adj.r.squared <- 1 - (1 - ans$r.squared) * ((n - df.int)/rdf)

if(!is.null(z$unrestricted)) { ansu <- R2unres(z)
                           }
else {
    ansu <- NULL
}

out <- list(restricted=ans,unrestricted=ansu)
class(out) <- "R2_midas_r"
out

}

    print.R2_midas_r <- function(x,digits = max(3L, getOption("digits") - 3L)) {
     cat("Multiple R-squared: ", formatC(x$restricted$r.squared, digits = digits))
      cat(",\tAdjusted R-squared: ", formatC(x$restricted$adj.r.squared, digits = digits),"\n")
 if(!is.null(x$unrestricted)) {
    cat("MIDAS-U:\n")
    cat("Multiple R-squared: ", formatC(x$unrestricted$r.squared, digits = digits))
 cat(",\tAdjusted R-squared: ", formatC(x$unrestricted$adj.r.squared, digits = digits),"\n")
 }

}

  R2unres <- function(x) {
z <- x$unrestricted
r <- z$residuals
f <- z$fitted.values
mss <- if ("`(Intercept)`" %in% names(coef(z))) 
    sum((f - mean(f))^2)
else sum(f^2)
rss <- sum(r^2)
ans <- list(r.squared=NULL,adj.r.squared=NULL)

n <- length(r)   
rdf <- z$df.residual
df.int <- if ("`(Intercept)`" %in% names(coef(z))) 1L
else 0L

ans$r.squared <- mss/(mss + rss)
ans$adj.r.squared <- 1 - (1 - ans$r.squared) * ((n - df.int)/rdf)
ans

}

Sorry for bad formatting. After sourcing these functions simply run R2(x) where x is a output from midas_r

siddharthgopi commented 10 years ago

Thank you, I read up a bit on how R squared works in NLS after your comment.