Home > Enterprise >  Calculate R and Rsquare with missing values in R
Calculate R and Rsquare with missing values in R

Time:09-23

This is how my data looks like:

> dput(head(NDVINS,30))
structure(list(NDVIN = c(0.473423556691161, 0.477054534599308, 
0.476981842446724, 0.534850236167682, 0.494749776839649, 0.487290542051824, 
0.520846067679237, 0.480150934504084, 0.481785217497498, 0.48192643529568, 
0.481434727772958, 0.500768986540367, 0.479271005904589, 0.471903306084303, 
0.491082681011792, 0.504112964380976, 0.47717537029362, 0.48095597078326, 
0.481278024224638, 0.49770426200673, 0.46549746851826, 0.493456827694478, 
0.478417734411996, 0.527051712023175, 0.527993175833221, 0.533868783798418, 
NA, 0.586107357729844, 0.546745185860292, 0.557100901289835), 
    NDVIN_GPR = c(0.4905, 0.4905, 0.4912, 0.4922, 0.4933, 0.4942, 
    0.4947, 0.4946, 0.4939, 0.4925, 0.4906, 0.4882, 0.4857, 0.4833, 
    0.4812, 0.4799, 0.4797, 0.4807, 0.4831, 0.4871, 0.4926, 0.4994, 
    0.5072, 0.5158, 0.5247, 0.5335, 0.5418, 0.5492, 0.5555, 0.5604
    )), row.names = c(NA, 30L), class = "data.frame")

I'm trying to calculate the R and Rsquare but since I've missing values I don't know how to do it. I've tried the following code, but had no success:

rsq <- function (NDVIN, NDVIN_GPR, na.rm=TRUE) cor(NDVIN, NDVIN_GPR) ^ 2
rsq(NDVIN, NDVIN_GPR, na.rm=TRUE)

Any help will be much appreciatted.

CodePudding user response:

In the cor, there is an argument to remove the missing values i.e. use, by default, it is "everything". We can change it to complete.obs

rsq <- function (NDVIN, NDVIN_GPR, na.rm=TRUE) cor(NDVIN, NDVIN_GPR, use = "complete.obs") ^ 2
  • Related