Esme_ - 1 year ago 99

R Question

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 one. I know I can write my own and apply it, but am I missing something obvious? I am wanting something like

`obs <- 1:5`

mod <- c(0.8,2.4,2,3,4.8)

df <- data.frame(obs, mod)

R2 <- rsq(df)

> R2 0.85

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You need a little statistical knowledge to see this. R squared between two vectors is just the square of their correlation. So you can define you function as:

```
rsq <- function (x, y) cor(x, y) ^ 2
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**