I was about test the coefficients with lht command in the following model:
fashion.lm<-lm(LOGS~D2+D3+D4+I(D1*LOGA)+I(D2*LOGA)+I(D3*LOGA)+I(D4*LOGA)+I(D1*LOGC)+I(D2*LOGC)+I(D3*LOGC)+I(D4*LOGC))
However, when I try to put I(D1*LOGA) into lht() function, it generates errors:
library(car)
lht(fashion.lm,c("I(D1*LOGA)"))
> lht(fashion.lm,c("I(D1*LOGA)"))
Error in constants(lhs, cnames_symb) :
The hypothesis "I(D1*LOGA)" is not well formed: contains bad coefficient/variable names.
In addition: Warning message:
In constants(lhs, cnames_symb) : NAs introduced by coercion
I was wondering how to properly do the test in the model? I know one (not so smart method) is to create a variable with the values equal to D1*LOGA before I run the regression. But is there a more convenient way or doing it?
Function lht() treats I(D1*LOGA) as an invalid character. It does not perform operation inside of I()
Here is a solution which uses indirect coefficient specification:
mod.davis <- lm(weight ~ repwt + I(log(repwt)), data=Davis)
lht(mod.davis, hypothesis.matrix = names(coef(mod.davis)[3]))
Related
I am trying to implement ordinal logistic regression on my dataset in r. I use the function 'polr' for this, but cannot seem to find a lot of information regarding its implementation.
The following errors are the ones I'm stuck on:
> dat.polr <- polr(as.factor(relevance)~allterms+idf.title, data=dat.one)
Warning message:
In polr(as.factor(relevance) ~ allterms + idf.title + idf.desc + :
design appears to be rank-deficient, so dropping some coefs
> dat.pred <- predict(dat.polr,dat.test,type="class")
Error in X %*% object$coefficients : non-conformable arguments
I want to train my model to guess the relevance of a new dataset. dat.one is the dataset I'm using to train the data, dat.test is the dataset I'm using to test the data. I believe that the predict variable's error is caused by the warning in polr. However, I have no clue how to resolve this. Any help would be appreciated :)
I do not have a reproducible example here but I am hoping to see if the following error is diagnostic of an obvious problem.
Using miceadds, I am running correlations on data that has been imputed with MICE. I receive the following error when a continuous variable ("6") (-0.5 to 1.4) is added to the model.
micombine.cor(imp, variables=c(3:4:5:6))
Above results in the following error
Error in stats::cor(dat_ii, dat_jj, method = method, use =
"pairwise.complete.obs") :
'y' must be numeric
In addition: Warning messages:
1: In 3:4:5 : numerical expression has 2 elements: only the first used
2: In 3:4:5:6 :
numerical expression has 3 elements: only the first used
I'm trying to run a multinomial logistic regression using the mlogit package in R.
I've uploaded the data here https://drive.google.com/file/d/0B_o3xTWAYdbuRGw0dzNFRzd2NEk/view?usp=sharing.
The data contains two different choice variables which I want to run the same model on. I run the first model like so:
lfsm1 <- mlogit.data(lfs.models, shape="wide", choice="PWK")
f1 <- mFormula(PWK~1 | MIGGRP+SEX+AGE+EDU)
m1 <- mlogit(f1, lfsm1, weights=PWT14)
summary(m1)
This model runs without issues. Then I run the same exact model on the other choice variable:
lfsm2 <- mlogit.data(lfs.models, shape="wide", choice="multi")
f2 <- mFormula(multi~1 | MIGGRP+SEX+AGE+EDU)
m2 <- mlogit(f1, lfsm2, weights=PWT14)
I get the following errors:
Error in if (is.null(initial.value) || lnl <= initial.value) break :
missing value where TRUE/FALSE needed
In addition: There were 20 warnings (use warnings() to see them)
> warnings()
Warning messages:
1: In `[<-.factor`(`*tmp*`, is.na(x), value = FALSE) :
invalid factor level, NA generated
And that warning message repeats 20x.
I'm not sure what either of these errors mean in the context of my model. A previous post (mlogit: missing value where TRUE/FALSE needed) suggests that my first error occurs because my data are not in wide format, or because there are some individuals who do not select any of the alternatives. In my case neither of these explanations can be right. What I've seen about the warning messages suggest mlogit is reacting badly to variables being factors or numeric. But I don't quite understand why this would matter in a multinomial regression context, or how the problem only occurred twenty times in such a large dataset.
Any suggestions would be most appreciated!
Try
m2 <- mlogit(f2, lfsm2, weights=PWT14)
Note the f2 in the call to mlogit.
In your second call to mlogit.data, you have specified that multi is the choice variable, and the data are prepared accordingly. Yet, in the formula that you are using, f1, the dependent variable is specified as PWK, so that mlogit is expecting a dataframe with one row for each alternative as defined by PMK, not multi.
I'm using a leave-one-out method to evaluate how well a model with one datapoint excluded predicts that datapoint (rotating through all datapoints). The code below has successfully run on essentially the same data with a slightly different DV, so I'm stumped as to why I'm getting the error that I'm getting. Here's the relevant chunk of code:
dataPennTrim.lmer <- lmer(logDur.PENN~cNewNounDen*ContextCode+
Vowel.Contrasts+BlockCode+
(1|subject)+(0+ cNewNounDen +ContextCode|subject)+
(1|word)+(0+ContextCode|word),
data=pennTrim,
control = lmerControl(optimizer = "bobyqa"),REML=FALSE)
pennPred <- predict(dataPennTrim.lmer, newdata = dataFull2)
dataFull2 has the same columns as pennTrim, it just has more rows. Pretty standard use of the predict() function. I get this error:
Error in t(.Call(Csparse_dense_crossprod, y, x)) :
error in evaluating the argument 'x' in selecting a
method for function 't': Error: Cholmod error 'X and/or Y
have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 90
Any thoughts about what might be causing this error? I can use essentially the same code with the same dataframes swapping out logDur.PENN for logDur.Manual (measurements from a different source) and the code gives no errors.
I'm trying to evaluate a series of one-variable regression models by using an R-script. My data is formatted in a .csv file where the first 8 columns represent dependent variables that we would like to predict and the next 52 columns represent independent variable that might be used to fit any one of the 8 dependent variables.
I've read the data into the script successfully. I've also created a list of headers for the dependent and independent variables in a vector. So my script looks like this:
#... do some stuff to get data above
var_dep<-c("dep1","dep2",...)
var_indep<-c("indep1","indep2",...)
for(dep in var_dep){
for(indep in var_indep){
lm1<-lm(dep~indep, data=mydat)
}
}
I get this error message when I run
Rscript R_ScriptV2.R XLK_friendly.csv
in terminal
Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
Calls: lm ... model.matrix -> model.matrix.default -> contrasts<-
In addition: Warning message:
In model.response(mf, "numeric") : NAs introduced by coercion
Execution halted
So how can I specify the dependent and indepedent variables in my regression using variables?
This might be a hacky solution, but you can use as.formula in conjunction with paste to get this to work:
for (dep in var_dep){
for (indep in var_indep){
f <- as.formula(paste0(dep, " ~ ", indep))
lm1 <- lm(f, data = mydata)
}
}