Reputation: 599
I'm getting an error message from some of my calc.relimp()
calls on lm
objects and am not sure how to fix this or to troubleshoot it better. I have a data set with predictors that includes two categorical variables and five continuous variables. I have a few response variables and for each one I'm running a different set of predictors in the lm
, sometimes they include squared or interactive terms.
I do not have any missing data or NA
in my data.frame, however certain models appear to run well in the lm()
function but then give this error when running the lm
in calc.relimp()
:
Error in cov.wt(y, wt = wt) : 'x' must contain finite values only
This error only occurs when a particular categorical predictor Treatment
(2 levels, character) is part of the model, but it's inclusion does not always lead to this error.
I believe the error indicates that the calc.relimp()
function receives this error when calculating a covariance matrix, but I'm not sure what values are being used in that matrix or how I can better troubleshoot the problem. I have already double checked that I have no NA
or missing data.
I have included the dataset and model and calc.relimp()
call below that cause the error in one of the cases. Any help is much appreciated!
Dataframe:
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))
Model and relative importance call:
library(relaimpo)
mu2<-lm(response~Treatment+AP+WC+I(TMP^2), data=exp_df)
mu2RI<-calc.relimp(mu2, type = "lmg")
Upvotes: 2
Views: 2779
Reputation: 41
I had the same problem. I changed the fit function from fit <- lm(b ~ b1_categorical+ b2)
to fit <- lm(b ~ factor(b1_categorical)+ b2)
and then the calc.relimp(fit, type = c("lmg"), rela = TRUE)
function worked fine.
Upvotes: 3
Reputation: 599
I found a solution: I changed the categorical character factor that is a predictor in the lm
to a 0/1 dummy variable. Now the covariance matrix calculation and the calc.relimp()
calls work. I troubleshot a bit more by calling the covariance matrix function directly cov(cbind(y,x))
, where y
is a column vector of response values and x
a corresponding matrix of regressors.
While this fixes my current problem, it is unclear to me why this is necessary. I also run many lm()
and calc.relimp()
with this data set with the same categorical factor remaining as a character and they work fine without converting the categorical factor to a dummy variable prior. Those lm
have different response variables, but I'm unsure why that would matter.
Upvotes: 0