使用 R 中的缺失值计算 R 和 Rsquare

Calculate R and Rsquare with missing values in R

我的数据是这样的:

> 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")

我正在尝试计算 R 和 Rsquare,但由于缺少值,我不知道如何计算。 我尝试了以下代码,但没有成功:

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

如有任何帮助,我们将不胜感激。

cor中有一个删除缺失值的参数,即use,默认为"everything"。我们可以把它改成complete.obs

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