0

w.r.t R/Rstudio I know there is RMSE and R2 function which i can leverage to calculate RMSE and Rsquare on test data. Is there a similar function for adjusted R square on test data?

Axeman
  • 32,068
  • 8
  • 81
  • 94
DSnew
  • 11
  • 1
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. It is very unclear what types of test you are talking about. – MrFlick Feb 14 '20 at 21:24
  • 1
    Base R has no functions called R2 or RMSE. Are you using some library? – G5W Feb 14 '20 at 21:25
  • Any answer would depend on the model you are using. There is no R square of your data. – Axeman Feb 14 '20 at 21:31
  • 1
    Questions such as this need to include code and data. – G. Grothendieck Feb 14 '20 at 23:08

3 Answers3

1

You can compute the Adjusted R-squared for any model by simply ... adjusting R-squared. The formula is available from Wikipedia.

AdjR2 = 1 - (1 - R2) * (n-1) / (n-k-1)
where n = number of points
and k = number of variables in the model

Here is a simple example:

library(MASS)
data(Boston)

LM <- lm(medv~., data=Boston)
R2 = summary(LM)$r.squared
R2
dim(Boston)

summary(LM)$adj.r.squared
[1] 0.7337897

1 - (1 - R2)*(dim(Boston)[1]-1)/(dim(Boston)[1]-dim(Boston)[2])
[1] 0.7337897
G5W
  • 36,531
  • 10
  • 47
  • 80
1

Assuming that you are referring to the adjusted R squared from a linear model it can be extracted from the output of summary.lm. Using the built in BOD data.frame for purposes of example:

s <- summary(lm(demand ~ Time, BOD))
s$adj.r.squared
## [1] 0.5561503

Try str(s) to see the various items you can get from summary.lm in much the same manner.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thanks for this. I did use it when building the model. Wanted on which I can use on the test data. No worries, i got it – DSnew Feb 14 '20 at 22:36
0

A number of base functions in R will output an adjusted R2. It just depends on what the objective or context is. For example, if the goal is to calculate the fit of a linear model with the lm() function,

x <- rnorm( 10 )
y <- rnorm( 10 )
linear_model <- lm( y~x )
summary( linear_model )

The output would include the adjusted R-square for fitting that linear model:

Call:
lm(formula = y ~ x)

Residuals:
    Min      1Q  Median      3Q     Max 
-1.5876 -0.5203  0.1511  0.4593  1.1989 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  -0.2018     0.2777  -0.727    0.488
x            -0.4355     0.3696  -1.178    0.272

Residual standard error: 0.8748 on 8 degrees of freedom
Multiple R-squared:  0.1479,    Adjusted R-squared:  0.0414 
F-statistic: 1.389 on 1 and 8 DF,  p-value: 0.2725

But again, this value is ONLY in reference for fitting a linear model to the data, and is simply an illustration that R may report it with certain functions.

Jason K Lai
  • 1,500
  • 5
  • 15