Hello, I'm doing some linear regression:>lm<-lm(osas~alp,data) >summary(lm)However, the Rsquared in the output of summary() is not the same as the "standard" Rsquared calculated by spreadsheets, and outlined in statistical guidebooks, being SSR/SSTO. The output says "multiple Rsquared", but it is no multiple regression... What's the difference? Thanks, Wouter Buytaert -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Hi, Wouter Actually, I have a similar problem too with a simple regression. In my case, not only the R-square but also the estimates of intercept and coefficient by lm() seem different from the calculation with the well known formula for a simple regression. What I used is the following code. (I have just started to use R last week so don't blame my inmature code, please!) Simply, > ols1( y, x ) will give you the result of the simple regression. Wouter, could you try the following code on your data and see whether that's what you expect or not? I will appreciate if anyone can give me some advice why this differenct happens. Thankk you. Wataru Shito ----------------------------------------- # Single Explanatory Variable Least Square Regression # library(methods) # create ols class setClass("ols", representation ( coefficients="list", standard.errors="list", r.square="numeric" )) setMethod("show", "ols", function(object) { # create row names for data.frame rownames <- c("(Intercept)", "X") # create data.frame z <- data.frame( row.names=rownames, Estimate=object at coefficients, Std.Error=object at standard.errors, t.value=t.values ) cat("\n") print(z) cat( "\nR-Square:", object at r.square, "\n\n" ) } ) ols1 <- function( y, x ){ size <- length(x) # number of ovservations xbar <- mean(x) ybar <- mean(y) Sxx <- sum( (x-xbar)^2 ) b <- sum( (x-xbar)*(y-ybar) )/Sxx # coefficient a <- ybar - b*xbar # interception e <- y - a - b*x # residuals # SSE (error sum of squares) SSE <- sum( e^2 ) # SST (total sum of squares) SST <- sum( (y-ybar)^2 ) # SSR (regression sum of squares) SSR <- b^2 * Sxx # Coefficient of determination r2 <- SSR / SST # unbiased estimator of sigma^2 s.square <- sum(e^2)/(size - 2) # standard error for b std.error.b <- sqrt( s.square/Sxx ) # standard error for intercept std.error.a <- sqrt( s.square*(1/size + xbar^2/Sxx) ) standard.errors <- list( intercept=std.error.a, coeficient=std.error.b ) coefficients <- list( intercept=a, coefficient=b ) new("ols", coefficients=coefficients, standard.errors=standard.errors, r.square=r2 ) } -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I am not sure where the problem might be and I missed the original message by Wouter, but here is a simple example I just ran (Win2000, 1.5.0) which shows no apparent problems.> x <- c(-3, -1.5, 2, 5, 7, 8) > y <- c(2, 4, 5, 6, 6, 9) > sxx <- sum((x-mean(x))^2) > sxy <- sum((x-mean(x))*(y-mean(y))) > bb <- sxy/sxx > bb[1] 0.4761517> aa <- mean(y) - bb*mean(x) > aa[1] 3.944558> ee <- y - aa - bb*x > sst <- sum((y-mean(y))^2) > sse <- sum(ee^2) > ssr <- sst-sse > ssr/sst[1] 0.8477822> summary(lm(y~x))Call: lm(formula = y ~ x) Residuals: 1 2 3 4 5 6 -0.5161 0.7697 0.1031 -0.3253 -1.2776 1.2462 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 3.9446 0.5098 7.737 0.00150 ** x 0.4762 0.1009 4.720 0.00917 ** --- Signif. codes: 0 `***' 0.001 `**' 0.01 `*' 0.05 `.' 0.1 ` ' 1 Residual standard error: 1.02 on 4 degrees of freedom Multiple R-Squared: 0.8478, Adjusted R-squared: 0.8097 F-statistic: 22.28 on 1 and 4 DF, p-value: 0.009172 Cheers, Andy __________________________________ Andy Jaworski Engineering Systems Technology Center 3M Center, 518-1-01 St. Paul, MN 55144-1000 ----- E-mail: apjaworski at mmm.com Tel: (651) 733-6092 Fax: (651) 736-3122 Wataru Shito <shito at seinan-g To: wouter buytaert <wouter.buytaert at yucom.be> u.ac.jp> cc: r-help at stat.math.ethz.ch (bcc: Andrzej P. Jaworski/US-Corporate/3M/US) 05/09/2002 Subject: Re: [R] Rsquared in summary(lm) 20:24 Hi, Wouter Actually, I have a similar problem too with a simple regression. In my case, not only the R-square but also the estimates of intercept and coefficient by lm() seem different from the calculation with the well known formula for a simple regression. What I used is the following code. (I have just started to use R last week so don't blame my inmature code, please!) Simply, > ols1( y, x ) will give you the result of the simple regression. Wouter, could you try the following code on your data and see whether that's what you expect or not? I will appreciate if anyone can give me some advice why this differenct happens. Thankk you. Wataru Shito ----------------------------------------- # Single Explanatory Variable Least Square Regression # library(methods) # create ols class setClass("ols", representation ( coefficients="list", standard.errors="list", r.square="numeric" )) setMethod("show", "ols", function(object) { # create row names for data.frame rownames <- c("(Intercept)", "X") # create data.frame z <- data.frame( row.names=rownames, Estimate=object at coefficients, Std.Error=object at standard.errors, t.value=t.values ) cat("\n") print(z) cat( "\nR-Square:", object at r.square, "\n\n" ) } ) ols1 <- function( y, x ){ size <- length(x) # number of ovservations xbar <- mean(x) ybar <- mean(y) Sxx <- sum( (x-xbar)^2 ) b <- sum( (x-xbar)*(y-ybar) )/Sxx # coefficient a <- ybar - b*xbar # interception e <- y - a - b*x # residuals # SSE (error sum of squares) SSE <- sum( e^2 ) # SST (total sum of squares) SST <- sum( (y-ybar)^2 ) # SSR (regression sum of squares) SSR <- b^2 * Sxx # Coefficient of determination r2 <- SSR / SST # unbiased estimator of sigma^2 s.square <- sum(e^2)/(size - 2) # standard error for b std.error.b <- sqrt( s.square/Sxx ) # standard error for intercept std.error.a <- sqrt( s.square*(1/size + xbar^2/Sxx) ) standard.errors <- list( intercept=std.error.a, coeficient=std.error.b ) coefficients <- list( intercept=a, coefficient=b ) new("ols", coefficients=coefficients, standard.errors=standard.errors, r.square=r2 ) } -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear everybody who responded to my question. As you probably read the correspondence with John Fox, it seems the bug for only Mac version. I am sorry that I caused some trouble posting the script with some bugs. Anyway, thank you again for all of your help. ------------- Wataru Shito -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Dear Wataru, On Sat May 11 2002 - 05:19:35 CEST Wataru Shito wrote:>Dear everybody who responded to my question. > >As you probably read the correspondence with John Fox, it seems the >bug for only Mac version. I am sorry that I caused some trouble >posting the script with some bugs. > >Anyway, thank you again for all of your help.Actually, since I wrote to you privately, people on the list will not have seen my message. (To clarify, I confirmed that the regression was calculated properly by R 1.5.0 under Windows 2000; I don't have a Mac.) Regards, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox ----------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._