R中的协方差矩阵计算误差,通过calc.relimp()

时间:2016-07-03 21:32:37

标签: r matrix covariance

我收到来自calc.relimp()lmlm个对象的错误消息,我不知道如何解决此问题或更好地解决问题。我有一个包含两个分类变量和五个连续变量的预测变量的数据集。我有一些响应变量,对于每一个我在NA运行一组不同的预测变量,有时它们包括平方或交互式术语。

我的data.frame中没有任何缺失数据或lm(),但某些模型似乎在lm函数中运行良好,但在运行calc.relimp()时出现此错误在Error in cov.wt(y, wt = wt) : 'x' must contain finite values only

Treatment

仅当特定的分类预测变量calc.relimp()(2级,字符)是模型的一部分时才会出现此错误,但它的包含并不总是会导致此错误。

我认为错误表明NA函数在计算协方差矩阵时会收到此错误,但我不确定该矩阵中使用的值是什么,或者我如何更好地解决问题。我已经仔细检查过我没有calc.relimp()或缺少数据。

我在下面列出了数据集和模型以及exp_df<-structure(list(response = c(-7.59854557534419, -6.11372322139816, -5.26231304004255, -5.72145463155338, -6.36862211476643, -7.61900265561851, -7.13058686497619, -8.96170609504813, -6.48673799056386, -7.58453773635567, -7.09550547527033, -7.52680975468282, -8.86371619889984, -7.15280231344095, -9.90531908445649, -6.45437296041698, -7.49902186125677, -6.78590841237406, -7.68349533437632, -8.1696817768836, -10.6868849216951, -8.28785299034027, -5.0933635546202, -6.56749746635158, -5.5276877520019, -5.68522798427618, -8.0704844321303, -6.081176708526, -6.50662406338498, -7.54892094332027, -9.62372913076138, -2.35278086991753, -3.26091576932667, -3.34094262346613, -5.58578457844078, -3.9732208814056, -3.60094958539171, -6.21489091641707, -3.87303561313608, -4.43970202965744, -8.50176936097569, -5.98062942793468, -4.89816563068379, -4.2396572077735, -4.08205998285175, -4.89644338119708, -6.1461653509185, -6.03116565171581, -6.4195726682198, -5.4338431378733, -5.25805046427446, -6.66342099838612, -3.63392750579693, -2.98206365328999, -3.16710535360004, -3.30545943440104, -3.43455326583898, -3.88162711949682, -4.91310540493929, -4.52380144247737, -4.45238421982284, -3.59236170371706 ), Treatment = c("C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "C", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N", "N"), AP = c(0.958910006922412, 1.38724149886511, 0.915165514128266, 1.49660273085048, 0.623535562167281, 0.854105492936431, 0.709247427735812, 0.738182587031943, 0.701728843036816, 0.715171161135021, 0.754586771829749, 1.409864741623, 0.950389659841629, 1.51630684551058, 0.992966501396659, 0.890959485171062, 0.666544049474755, 0.79409717063337, 0.749968048396645, 0.75573366235722, 0.778130855050235, 0.742650153754373, 0.860254485174763, 1.51175993687244, 1.05695004517418, 1.10903938319774, 0.958058048210046, 0.938427364850423, 0.972052198723841, 0.93337392160933, 0.926959935957174, -1.10819297078147, -1.29741848120788, -0.809216664307748, -0.619991153881339, -0.777679079236677, -1.01105720876258, -1.52448909371956, -1.55634205464134, -1.55192679273139, -1.5314273624352, -1.53710412774799, -1.41983224733315, -0.718927708431874, -0.91471109918642, -0.882080534060665, -1.22404885657861, -1.12354671599127, -1.65477231623861, -1.68772918701563, -1.66195104056628, -1.68316090789802, -0.302479735698982, -0.442629469594843, -0.904562992515607, -0.650051075760721, -0.736383311840574, -1.11254519761707, -1.10637860932565, -1.13076466302353, -1.11759058803732, -1.13468885557262), WC = c(-1.11305930626715, -1.87377094505647, -1.03536960698654, -2.067995193258, -0.517438278449125, -0.926927735074016, -0.669661532977074, -0.721050031980395, -0.656308615913218, -0.68018201308799, -0.750183669210624, -1.87377094505647, -1.03536960698654, -2.067995193258, -1.11305930626715, -0.926927735074016, -0.517438278449125, -0.750183669210624, -0.669661532977074, -0.68018201308799, -0.721050031980395, -0.656308615913218, -0.517438278449125, -1.87377094505647, -0.926927735074016, -1.03536960698654, -0.721050031980395, -0.68018201308799, -0.750183669210624, -0.669661532977074, -0.656308615913218, 0.910109945832115, 1.15289025608403, 0.526517055634094, 0.283736745382183, 0.486053670592109, 0.7854827199028, 1.44422662838632, 1.48509464727872, 1.47942977337285, 1.45312857309556, 1.46041198240311, 1.15289025608403, 0.283736745382183, 0.526517055634094, 0.486053670592109, 0.910109945832115, 0.7854827199028, 1.44422662838632, 1.48509464727872, 1.45312857309556, 1.47942977337285, 0.283736745382183, 0.486053670592109, 1.15289025608403, 0.7854827199028, 0.910109945832115, 1.45312857309556, 1.44422662838632, 1.47942977337285, 1.46041198240311, 1.48509464727872), TMP = c(-0.80106357660266, -1.68703515117027, 0.787293670102007, 0.0952956537299076, 0.965950736485032, 1.14248787378464, -0.470545353882089, -0.668673973823901, -0.588752627689013, 0.145145797581795, -1.15864270056773, -1.33851872672067, 1.34822704019927, 0.288633231521372, -0.638252984175761, 1.31420217075319, 1.36661742941165, -1.13345793701235, -0.404149159054249, 0.252753423682086, -0.316341675345151, -0.453925105370426, 2.50629157868947, -0.809384300760865, 1.61184028549868, 2.04907576312762, 0.290382173943721, 1.0197439501416, -0.838109346226441, -0.289672961075216, -0.279666893145624, -1.98721718117201, -1.84915676749726, 0.0914797797007637, 0.965950736485032, 1.09669739459303, 0.686809007177131, -0.620339579376033, -1.16551127244647, 0.117671510066826, -0.47512440180125, -0.554409768295302, -1.84915676749726, 1.10586608931612, 0.14871787869028, 1.14248787378464, -1.68703515117027, 1.02514977060172, -0.513495127165762, -1.05332459842702, -0.289672961075216, 0.451942008165602, 1.7240375584029, 1.31420217075319, -0.80106357660266, 1.70564716919052, -0.988730343498175, 0.0697823005789453, 0.613459443541268, 1.28532872945296, 0.375391350591117, -0.634341713823762 )), .Names = c("response", "Treatment", "AP", "WC", "TMP"), class = "data.frame", row.names = c(NA, -62L)) 调用,导致其中一个案例出现错误。非常感谢任何帮助!

数据帧:

library(relaimpo)
mu2<-lm(response~Treatment+AP+WC+I(TMP^2), data=exp_df)
mu2RI<-calc.relimp(mu2, type = "lmg")

模型和相对重要性调用:

{{1}}

2 个答案:

答案 0 :(得分:1)

我有同样的问题。我从fit <- lm(b ~ b1_categorical+ b2)更改了fit函数 到fit <- lm(b ~ factor(b1_categorical)+ b2)
然后calc.relimp(fit, type = c("lmg"), rela = TRUE)函数运行正常。

答案 1 :(得分:0)

我找到了一个解决方案:我将作为lm预测变量的分类字符因子更改为0/1虚拟变量。现在协方差矩阵计算和calc.relimp()调用工作。通过直接调用协方差矩阵函数cov(cbind(y,x))来解决问题,其中y是响应值的列向量,x是相应的回归矩阵。

虽然这解决了我当前的问题,但我不清楚为什么这是必要的。我还运行了许多lm()calc.relimp()这个数据集,其中相同的分类因子仍然是一个字符,它们工作正常,而不会将分类因子转换为虚拟变量。那些lm有不同的响应变量,但我不确定为什么这很重要。