Can I do a mulitvariate regression with the segmented package in r? - r

I have FINALLY figured out how to use the segmented package with a uni-variate analysis giving results comparable to what I was expecting. Ultimately though, I have to do a GLM piece-wise regression on a multivariate analysis. The model has some variables that need to be segmented and some that do not as well as categorical variables. Is this possible with the segmented package?
If so, how?
Do I have to keep interactively keep developing models adding one variable to the segmented package at at time?
piecewise <- glm(y ~ x, family = quasipoisson(link = "log"), data = data)
piecewise_seg <- segmented(piecewise, seg.z = ~ x1, psi = 3)
piecewise_seg2 <- segmented(piecewise_seg, seg.z = ~x2 psi = 400)
Or can I do this in one go? If so, how can I set the different psi parameters for each different variable?

Wait, I think I found it towards the end of the package documentation.
2 segmented variables: starting values requested via a named list
o1<-update(o,seg.Z=~x+z,psi=list(x=c(30,60),z=.3))

Related

How to run Beta Regression with lots of independent variables?

Why is it that Beta Regression that is bound between 0 and 1 is unable to handle lots of independent variables as Regressors? I have around 30 independent variables that I am trying to fit and it shows error like:
Error in optim(par = start, fn = loglikfun, gr = gradfun, method =
method, : non-finite value supplied by optim
Only few variables it is accepting.Now If I combine all these independent variables in X <- (df$x1 + … + df$x30) and make dependent variable in Y <- df$y and then run Beta Regression then it works but I won’t be getting coefficients for individual independent variables which I want.
betareg(Y ~ X, data = df)
So, what’s the solution?
Probably, the model did not converge because of the multicollinearity problem. In most cases, regression models can not be estimated properly when lots of variables are considered. You can overcome this problem with an appropriate variable selection procedure using information criteria.
You can benefit gamlss package in R. Also, stepGAIC() function can help you when considering gamlss(...,family=BE) function during the modeling.

Specifying that model is logit transformed to plot backtransformed trends

I have fitted a lme model in R with a logit transformed response. I have not been able to find a direct command that does the logit transformation so I have done it manually.
logitr<-log(r/1-r)
I then use this as response in my lme model with interaction between two factors and a numerical variable.
model<-lme(logitr<-factor1*factor2*numeric,random=1|random)
Now, R obviously do not know that this model is logit transformed. How can I specify this to R?
I have without luck tried:
update(model, tran="logit")
The reason why I want to specify that the model is logit transformed is because I want to plot the backtransformed results using the function emmip in the emmeans package, showing the trends of the interaction between my variables.
Normally (if I only had factors) I would just use:
update_refgrid_model<-update(ref_grid(model, tran="logit"))
But this approach does not work when I want to use emmip to plot the trends of the interaction between a numerical variable and factors. If I specify:
emmip(update_refgrid_model, factor1~numeric|factor2, cov.reduce = range, type = "response")
then I do not get any trends plotted, only the estimate for the average level on the numerical variable.
So, how can I specify the logit transformation and plot the backtransformed trends of a lme model with factors interacting with numerical variables?
You don't update the model object, you update the reference grid:
rg = update(ref_grid(model, cov.reduce = range), tran = "logit")
emmip(rg, factor1~numeric|factor2, type = "response")
It is possible to update a model with other things, just not the transformation; that is in the update method for emmGrid objects.
Update
Here's an example showing how it works
require(emmeans)
## Loading required package: emmeans
foo = transform(fiber, p = (strength - 25)/25)
foo.lm = lm(log(p/(1-p)) ~ machine*diameter, data = foo)
emm = emmeans(foo.lm, ~diameter|machine,
tran = "logit", at = list(diameter = 15:32))
## Warning in ref_grid(object, ...): There are unevaluated constants in the response formula
## Auto-detection of the response transformation may be incorrect
emmip(emm, machine ~ diameter)
emmip(emm, machine ~ diameter, type = "r")
Created on 2020-06-02 by the reprex package (v0.3.0)

Conduct quantile regression with several dependent variables in R

I'm interested in doing a multivariate regression in R, looking at the effects of a grouping variable (2 levels) on several dependent variables. However, due to my data being non-normal and the 2 groups not having homogenous variances, I'm looking to use a quantile regression instead. I'm using the rq function from the quantreg toolbox to do this.
My code is as follows
# Generate some fake data
DV = matrix(rnorm(40*5),ncol=5) #construct matrix for dependent variables
IV = matrix(rep(1:2,20)) #matrix for grouping factor
library(quantreg)
model.q = rq(DV~IV,
tau = 0.5)
I get the following error message when this is run:
Error in y - x %*% z$coef : non-conformable arrays
In addition: Warning message:
In rq.fit.br(x, y, tau = tau, ...) : Solution may be nonunique
I believe this is due to my having several DVs, as the model works fine when I try using a DV of one column. Is there a specific way I should be formatting my data? Or perhaps there is another function I may be able to use?
Thank you!
If you just want to run several regressions, each with the same set of independent variables, but with a different dependent variable, you could write a function and then apply it to all columns of your DV matrix and save the models in a list:
reg <- function(col_number) {
model.q <- rq(DV[, col_number] ~ IV, tau = 0.5)
}
model_list <- lapply(1:ncol(DV), reg)
However, as pointed out in the comments, it might be that you want a multivariate model accounting for the correlation of the outcome - but then I do not think the rq method would be appropriate
If you have multiple responses, what you most likely need is:
DV = matrix(rnorm(40*5),ncol=5) #construct matrix for dependent variables
IV = matrix(rep(1:2,20)) #matrix for grouping factor
library(quantreg)
rqs.fit(x=IV, y=DV, tau=0.5, tol = 0.0001)
Unfortunately, there's really not a lot of documentation about how this works.. I can update if i do find it

How to extract slope and intercept values for different groups from interact_plots in jtools when plotting linear fixed effects models

I am trying to extract the slope and intercept for each of my groups from my linear mixed effects models. The model was constructed using lmer in the lme4 library, and I can view the results for each group using interact_plot from the jtools library. How do I get the slope and intercept for each of these lines?
I know that I can use the summary() or summ() to see the estimates for the fixed effects and the variances of the random effects but I cannot see the estimates of the random effects. Therefore, I cannot accurately calculate the slope and intercepts of the models.
>library(lme4)
> cond_waterxsilver <- lmer(LnAg ~ LnVolume + (LnVolume | FilterID) + SilverType + WaterType + SilverType*WaterType + SilverType*LnVolume + WaterType*LnVolume, data=capwater_removed.data)
> library(jtools)
> interact_plot(cond_ranin_waterxsilver, pred = LnVolume, modx = WaterType, mod2 = SilverType)
I am just trying to get the slope and intercepts for the six lines from the model (two different WaterType and three different SilverType). Is there a tool within jtools or another package that can help me with extracting the slope and intercepts from my model?
I'm the developer of this package!
A short note: this and the other function I'm going to mention have just been moved to a new package, called interactions, which is in the process of being added to CRAN. Assuming you haven't updated to the newest version of jtools (2.0.0; just came out days ago), these functions are still available in the jtools package. If you do update to jtools 2.0.0, you'll need to follow this link for instructions on how to download interactions before it gets to CRAN.
There should be a simple answer to your question. The sim_slopes (short for "simple slopes") function should give you what you're looking for.
sim_slopes(cond_ranin_waterxsilver, pred = LnVolume, modx = WaterType, mod2 = SilverType, cond.int = TRUE)
This will print out the conditional slopes and intercepts (the intercepts are only printed when cond.int = TRUE.
If you need to program with those values, you can save the sim_slopes object.
ss <- sim_slopes(cond_ranin_waterxsilver, pred = LnVolume, modx = WaterType, mod2 = SilverType, cond.int = TRUE)
ss$slopes # Matrix of slopes with test statistics, etc.
ss$ints # Matrix of intercepts with test statistics, etc.

Calculate probabilities from Probit Model - R Command?

I'm working with a Probit model, and would like to calculate the probabilities from my model for each observation in my DF. I know I can calculate this using the formula, however I am wondering if there is a quick way to output the probabilities and append them to my DF.
I am running the following model:
attach(non.part.2)
y <- cbind(E)
x1 <- cbind(tech.ems, med.com, tech.nonemerg)
probit <- glm(y ~ x1, family = binomial (link = "probit"))
summary(probit)
I am running several models, so it would be nice to be able to have R spit out the probabilities and allow me to name them in my DF(non.part.2) - something like p_x1 - so that I can run summary stats on the various models later.
Any help is much appreciated!
The following should work.
non.part2$p_x1 <- predict(probit, yourDataToPredictOn, type = "response")

Resources