Function to calculate R2 (R-squared) in R

前端 未结 6 774
悲&欢浪女
悲&欢浪女 2020-11-27 15:59

I have a dataframe with observed and modelled data, and I would like to calculate the R2 value. I expected there to be a function I could call for this, but can\'t locate o

6条回答
  •  半阙折子戏
    2020-11-27 16:54

    Not sure why this isn't implemented directly in R, but this answer is essentially the same as Andrii's and Wordsforthewise, I just turned into a function for the sake of convenience if somebody uses it a lot like me.

    r2_general <-function(preds,actual){ 
      return(1- sum((preds - actual) ^ 2)/sum((actual - mean(actual))^2))
    }
    

提交回复
热议问题