How can I fit a Beta regression in R? - r

I'm a beginner with R, and I have a vector distributed according to Beta distribution. I would like to fit a regression using this data and two explanatory variables.
I don't know the appropriate syntax though.

You can use the betareg package. Below is an example, with two explanatory variables batch and temp:
install.packages("betareg")
library(betareg)
data("GasolineYield", package = "betareg")
gy <- betareg(yield ~ batch + temp, data = GasolineYield)
summary(gy)
There's a paper on how it works here:
https://cran.r-project.org/web/packages/betareg/vignettes/betareg.pdf
And full documentation, including examples here:
https://cran.r-project.org/web/packages/betareg/betareg.pdf

Related

how to run a GLMM on R

I am trying to run a Generalized linear mixed model (GLMM) on r, I have two fixed factors and two random factors
however there are a lot of holes in my data set and the I am struggling to find a code to run the glmm all I found is the glm
Can someone please walk me through this, I know very little about R and coding
You can use the lme4 package as well. The command for a generalized linear mixed model is glmer().
Example:
install.packages("lme4") #If you still haven't done it.
library(lme4)
myfirstmodel <- glmer(variable_to_study ~ fixed_variable + (1|random_effect_varible), data = mydataset, family = poisson)
Family = poisson was just an example. Choose the 'family' according to the nature of the variable_to_study (eg. poisson for discrete data).

Clustering standard errors by a variable in a logistic regression - for graphing interaction plot (R)

I'm running a logistic regression/survival analysis where I cluster standard errors by a variable in the dataset. I'm using R.
Since this is not as straight forward as it is in STATA, I'm using a solution I found in the past : https://www.rdocumentation.org/packages/miceadds/versions/3.0-16/topics/lm.cluster
As an illustrative example of what I'm talking about:
model <- miceadds::glm.cluster(data = data, formula = outcome ~ a + b + c + years + years^2 + years^3, cluster = "cluster.id", family = "binomial")
This works well for getting the important values, this produces the coefficients, std. errors (clustered), and z-values. It took me forever just to get at this solution; and even now it is not ideal (like not being able to output to Stargazer). I've explored a lot of the other common suggestions on this issue - such as the Economic Theory solution (https://economictheoryblog.com/2016/12/13/clustered-standard-errors-in-r/); however, this is for lm() and I cannot get it to work for logistic regression.
I'm not beyond just running two models, one with glm() and one with glm.cluster() and replacing the standard errors in stargazer manually.
My concern is that I am at a loss as to how I would graph the above function, say if I were to do the following instead:
model <- miceadds::glm.cluster(data = data, formula = outcome ~ a*b + c + years + years^2 + years^3, cluster = "cluster.id", family = "binomial")
In this case, I want to graph a predicted probability plot to look at the interaction between a*b on my outcome; however, I cannot do so with the glm.cluster() object. I have to do it with a glm() model, but then my confidence intervals are awash.
I've been looking into a lot of the options on clustering standard errors for logistic regression around here, but am at a complete loss.
Has anyone found any recent developments on how to do so in r?
Are there any packages that let you cluster SE by a variable in the dataset and plot the objects? (Bonus points for interactions)
Any and all insight would be appreciated. Thanks!

Can I do a mulitvariate regression with the segmented package in 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))

R: how to extract r squared from lmekin in coxme package

I'm trying to find out how well my mixed model with family effect fits the data. Is it possible to extract r squared values from lmekin functions? And if so, is it possible to extract partial r squared values for each of the covariables?
Example:
model= lmekin(formula = height ~ score + sex + age + (1 | IID), data = phenotype_df, varlist = kinship_matrix)
I have tried the MuMin package but it doesn't seem to work with lmekin models. Thanks.
I am able to use the r.squaredLR() function,
library(coxme)
library(MuMIn)
data(ergoStool, package="nlme") # use a data set from nlme
fit1 <- lmekin(effort ~ Type + (1|Subject), data=ergoStool)
r.squaredLR(fit1)
(I am pretty sure that works, but one thing that is great to do is to create a reproducible example so I can run your code to double check, for example I am not exactly sure what phenotype_df looks like, and I am not able to run your code as it is, a great resource for this is the reprex package).

exponential transformation of dependent variable in Zelig

I apologize in advance if this question is too esoteric. I am using the Zelig package in R with a log-log regression model:
z.out <- zelig(lnDONATIONS ~ lnPRICE + lnFUNDRAISING + lnAGE, model = "ls", data = mydata)
x.out <- setx(z.out)
s.out <- sim(z.out, x = x.out)
summary(s.out)
plot(s.out)
This works fine, but I am trying to implement something that is allowed in the Stata-based 'precursor' to Zelig (clarify); specifically, in the clarify package, after the 'setx' command, you can type in simqi, tfunc(exp) in order to get the expected values based on the exponential transformation of the dependent variable (the simqi command in Stata is analogous to the sim comamnd in R/Zelig). My question is, can this post-setx exponential transformation be done in R with the Zelig package, and if so, how? The very extensive Zelig documentation does not seem to have an analogue to the 'tfunc' command in the clarify package.
Thanks in advance for any insights.

Resources