I am looking to do multivariate prediction using the party package in R (Party package documentation below)
http://cran.r-project.org/web/packages/party/party.pdf
I however, cannot figure out how to do multivariate prediction (multiple response variables). It says that it can do it, and I try this:
f <-cbind(A,B,C~shopping_pt+n_A_0)
model_1 <- ctree(f, data= train)
But that produces the following error:
Error in [<-(tmp, nas, drop = FALSE, value = 0) : (subscript) logical subscript too long
The documentation says it supports multivariate... but doesn't suggest how one can write the syntax correctly, any ideas?
Use the following syntax:
ctree(A + B + C ~ shopping_pt + n_A_0, data=train)
Try the package partykit
Check the sample below where it shows how you can code the multivariate response for the Conditional Inference Tree
### multivariate responses
airq2 <- ctree(Ozone + Temp ~ ., data = airq)
airq2
plot(airq2)
Related
I have discovered some heteroscedasticity in my model that I would like to compensate for with more robust standard errors. I have tried to use the Huber-White robust standard errors from the merDeriv package in R but I beleive these only work for a GLMM with a binomial distribution. Is there a way I could achieve the same thing for a Negative Binomial distribition?
Model:
library(lme4)
model <- glmer.nb(Jobs ~ 1 + Month + Year + (1|Region), data = df)
Huber-White robust standard errors:
library(merDeriv)
bread.glmerMod(model)
Error:
Error in vcov.lmerMod(object, full = full) : estfun.lmerMod() only works for lmer() models.
Thank you for any help!
This looks like a bug in the package, as far as I can tell (the bread.glmerMod function was calling estfun.lmerMod rather than estfun.glmerMod; there's a broader question here about the design of the generic functions, but never mind ...)
You should be able to install a fixed version from my fork via remotes::install_github("bbolker/merDeriv"), then reload the package and try again.
Alternately, download the tarball, change vcov.lmerMod to vcov.glmerMod in the last line of R/bread.glmerMod.R, and re-install the package ...
Try something like this:
library(lme4)
model <- glmer.nb(Jobs ~ 1 + Month + Year + (1|Region), data = df)
cov <- vcovHC(model, type = "HC1", sandwich = T)
se <- sqrt(diag(cov_m1))
(Can't confirm if it works since this there isn't a reproducible example)
Load data
utils::data("InstInnovation", package = "sandwich")
df <- InstInnovation
Create group variable combining 'company' and 'year'
df[['cluster_var']] <- factor(paste0(df$company,"-",df$year))
Linear regression model
model <- lm(sales ~ competition + log(capital/employment) + year, data = df)
Why this:
lmtest::coeftest(model, vcov = vcovCL(model, type="HC3", cluster=~company+year))
Produces Standard Errors DIFFERENT than this?
lmtest::coeftest(model, vcov = vcovCL(model, type="HC3", cluster=~cluster_var))
Shouldn't cluster=~company+year and cluster=~cluster_var be equivalent?
In addition, I cannot find a place (e.g. Github) to report issues on R sandwich package, I found this but is just a read-only mirror: https://github.com/cran/sandwich
Thank you very much in advance.
cluster=~company+year is indeed something different: 'multiway clustering'. I found the explanation here:
http://fmwww.bc.edu/repec/bost10/BOS10.baum.pdf
https://francish.netlify.app/post/note-on-robust-standard-errors/
I have the following code:
library(lmtest)
library(sandwich)
library(plm)
library(multiwayvcov)
reg <- lm(Y ~ x1 + x1_sq + x2 + x2_sq + x1x2 + d1 + d2 + d3 + d4, df)
coeftest(reg, vcov = vcovHC(reg, type="HC1")
coeftest(reg, vcov = vcovHC(reg, type="sss", cluster="study"))
I want to compare the regression when I use typical heteroskedasticity-robust standard errors and when I cluster the standard errors at the study level with a small sample correction. The regression and first -coeftest- work, but the second spits out a clear error message:
Error in match.arg(type) : 'arg' should be one of “HC3”, “const”, “HC”, “HC0”, “HC1”, “HC2”, “HC4”, “HC4m”, “HC5”
I found the code online where they use -type="sss"- as a small sample correction but it doesn't seem to work here. Is there something I am doing wrong or is one of the above listed in the error message the heteroskedastic-adjusted covariance matrix and the code was maybe updated? Clearly I cannot use -type="sss"-, but I don't know how to incorporate the small sample correction otherwise.
Using -...vcovHC(df, type="sss", cluster="study")- is a dated way to incorporate small sample corrections. Upon understanding differences between the sandwich estimators HC0-HC4, using the code previous to it:
coeftest(reg, vcov = vcovHC(reg, type="HC1")
is appropriate with the corresponding sandwich estimator in the type argument. The issue was with the dated syntax that followed and this is the correct format.
My question is how do I run a post hoc on a type 2 Anova (mixed-effects model)? So far I am using the glmer() from the "lme4" package, the Anova() from the "car" package, and trying to run a HSD test from the "agricolae" package.
After searching for some time this is the best that I could find, however, I receive an error message when doing so. Does anyone know how to get around this or what I am doing wrong? Or a different way of doing this?
library(lme4)
totaldiversity.model <- glmer(totaldiversity ~ focalspecies + (1|site), family = "poisson", data = data, na.action=na.fail)
library(car)
totaldiv.anova = Anova(totaldiversity.model, type = "II")
library(agricolae)
totaldiv.tukey = HSD.test(totaldiv.anova, "focalspecies", group=TRUE, console=TRUE)
Error message that comes up: Error in HSD.test(totaldiv.anova, "focalspecies", group = TRUE, console = TRUE, : argument "MSerror" is missing, with no default
Thank you in advance!
I followed the link posted by Ben Bolker (Post-hoc test for glmer) which led me to use the glht() function in the multcomp package. This is what the solution looked like for a multiple comparisons analysis (Tukey) on a glmer() mixed effects model, with a type 2 Anova. Need "multcomp", "lme4", and "car" packages.
totaldiversity.model <- glmer(totaldiversity ~ focalspecies + (1|site), family = "poisson", data = data, na.action=na.fail)
summary(totaldiversity.model)
Anova(totaldiversity.model, type = "II")
summary(glht(totaldiversity.model, mcp(focalspecies="Tukey")))
Thanks everyone!
From the documentation for HSD.test, y is "model(aov or lm) or answer of the experimental unit". In all likelihood this means you should be sending it totaldiversity.model:
HSD.test(totaldiversity.model, "focalspecies", group=TRUE, console=TRUE)
I'm constructing a piecewise structural equation model using the piecewiseSEM package in R (Lefcheck - https://cran.r-project.org/web/packages/piecewiseSEM/vignettes/piecewiseSEM.html)
I already created the model set and I could evaluate the model fit, so the model itself works. Also, the data fits the model (p = 0.528).
But I do not succeed in extracting the path coefficients.
This is the error i get: Error in cbind(Xlarge, Xsmall) : number of rows of matrices must match (see arg 2)
I already tried (but this did not work):
standardising my data because of the warning: Some predictor variables are on very different scales: consider rescaling
adapted my data (threw some NA values away)
This is my modellist:
predatielijst = list(
lmer(plantgrootte ~ gapfraction + olsen_P + (1|plot_ID), data = d),
glmer(piek1 ~ gapfraction + olsen_P + plantgrootte + (1|plot_ID),
family = poisson, data = d),
glmer(predatie ~ piek1 + (1|plot_ID), family = binomial, data = d)
)
with "predatie" being a binary variable (yes or no) and all the rest continuous variables (gapfraction, plantgrootte, olsen_P & piek1)
Thanks in advance!
Try installing the development version:
library(devtools)
install_github("jslefche/piecewiseSEM#2.0")
Replace list with psem and run the coefs or summary function. It will likely get rid of your error. If not, open a bug on Github!
WARNING: this will overwrite your current version from CRAN. You will need to reinstall from CRAN to get version 1.4 back.
try to use lme (out of the nlme library) ilstead of glmer. As far as I understand, the fact that lmer does not provide p-values (while lme does) seems to be the problem here.
Hope this works.