How to obtain analysis of variance table for a nonlinear regression model in R - r

Previously I used SAS to fit data into nonlinear regression model. SAS was able to produce an analysis of variance table for the model. The table displays the degrees of freedom, sums of squares, and mean squares along with the model F test.
Please refer to Table 69.4 in this pdf file.
Source: https://support.sas.com/documentation/onlinedoc/stat/132/nlin.pdf
How can I re-create something similar in R? Thanks in advance.

I'm not sure what type of nonlinear regression you're interested in- but the general approach would be to run the model and call for a summary. The typical linear model would be:
linearmodel = lm(`outcomevar` ~ `predictorvar`, data = dataset)
linearmodel #gives coefficients
summary(linearmod) # gives model fit
For nonlinear regression you would add the polynomial term. For quadratic fit it would be
y = b0 + b1(Var) + b2(Var * Var) or:
nonlinmodel = lm(`outcomevar` ~ `predictorvar` + I(`predictorvar`^2), data = dataset)
nonlinmodel
summary(nonlinmodel)
other methods here: https://data-flair.training/blogs/r-nonlinear-regression/

Related

How can I incorporate a categorical variable with ~200 levels in a nonlinear mixed effects model in R?

I am trying to fit a nonlinear mixed effects model with a categorical variable genotype that has around 200 levels.
So this is the linear version of the model.
mlinear <- lmer(WUE ~ moisture * genotype + (1|pot), data = d8)
Now I'm trying to make the same model, but with a logistic function instead of linear
mlogistic <- nlme(WUE ~ SSlogis(moisture, Asym, xmid, scal), data = d8, fixed = Asym + xmid + scal ~ 1, random = Asym + xmid + scal~1|pot)
Problem is, now I don't know how to incorporate genotype into this nonlinear model. Asym, xmid, and scal parameters should be able to vary between each genotype. Anyone know how to do this?
It looks like you’re using lme4::lmer for your linear model and nlme::nlme for the logistic? If you use lme4 for both, you should be able to keep the same model specification, and use lme4::glmer with family = binomial for the logistic model. The whole idea behind a GLM with a link function is that you shouldn’t have to do anything different with your predictor vs a linear model, as the link function takes care of that.
library(lme4)
mlinear <- lmer(WUE ~ moisture * genotype + (1|pot), data = d8)
mlogistic <- glmer(WUE ~ moisture * genotype + (1|pot), family = binomial, data = d8)
All that being said, how is WUE measured? You probably want to use either a logistic model (if binary) or linear (if continuous), not both.
Just to add to the answer by #zephryl, who has explained how you can do this, I would like to focus on:
Since my WUE responses are almost alway curved, I'm trying to fit a logistic function instead of a linear one.
Fitting a logistic model does not really make sense here. The distribution of WUE is not relevant. It is the conditional distribution that matters, and we typically assess this by inspecting the residuals. If the residuals show a nonlinear pattern then there are several ways to model this including
transformations
nonlinear terms
splines

Mixed model without an intercept

I want to use a mixed model without a random intercept but with a correlation structure. The reason is to get the AIC to help choose the best correlation structure (e.g., autoregressive versus compound symmetry). So it is essentially a GEE, but GEEs don't allow estimation of the AIC. They are also called covariance pattern models.
The code below simulates random data with a compound symmetry correlation. The model fits both a random intercept and a variance-covariance matrix. Is there any way to switch off the random intercept?
library(MASS)
library(nlme)
Sigma = toeplitz(c(1,0.5,0.5,0.5))
data = data.frame(mvrnorm(n=10, mu=1:4, Sigma=Sigma))
data$id = 1:nrow(data)
long = reshape(data, direction='long', varying=list(1:4), v.names='Y')
cs = corCompSymm(0.5, form = ~ 1 | id)
model = lme(Y~time , random=list(~1|id), data=long, correlation=cs)
summary(model)
If you are solely interested in comparing correlation structures, then I am pretty sure your goal could be served by a generalized least squares model fit with gls:
model = gls(Y~time, data=long, correlation=cs)
summary(model)
AIC(model)
Otherwise, a linear mixed effects model fit with lme must have random effects specified.

How to set up multiple regression in R?

I don't know how to set up a multiple regression in R and run a simple OLS estimation for it.
I only get an multiple regression equation (eg. Salary = 0.3 + 0.5*age + experience + residual ) and equations for each variables (eg. age = ... ). The error term is normally distributed with mean 0 and SD 0.3
How can I run a simple OLS estimation of salary on age and compute a standard error for it?
Thank you.
Since you have only one predictor, it will be an example of linear regression instead of multiple regression. In R you can do-
model <- lm(Salary~age, data= your_dataset)
summary(model) # will give you summary statistics such as standard error and coefficients

Constrained multinomial logistic regression in R using mlogit

I would like to add some constraints to a multinomial logistic regression model using mlogit in R. For example only look for negative values during coefficient estimation. But apparently the model doesn't have such capabilities. I was wondering if there is any way to add constraints or boundaries to mlogit or any other packages which can be used for multinomial logistic regression.
Here is the code:
Proc_MDC3 <- function(x){
fm <- mFormula(decision ~ term_f1yc + term_f2yc + eff_rate2 + term_special2 + peak -1 )
fit <- mlogit(fm, x)
Out2<-fit$coefficients
return( Out2)
}
Coeff4<-data.frame(do.call("rbind", by(MDC3, MDC3$SEG, Proc_MDC3)))
I only want negative values for eff_rate2 coefficients however this code gives me both negative and positive values.
I appreciate your help in advance.

How do you fit a linear mixed model with an AR(1) random effects correlation structure in R?

I am trying to use R to rerun someone else's project, so we need to use some macros in R.
Here comes a very basic question:
m1.nlme = lme(log.bp.dia ~ M25.9to9.ma5iqr + temp.c.9to9.ma4iqr + o3.ma5iqr + sea_spring + sea_summer + sea_fall + BMI + male + age_ini, data=barbara.1.clean, random = ~ 1|study_id)
Since the model is using AR(1) [autocorrelation 1 covariance model] in SAS for within person variance, I am not sure how to do this in R.
And where I can see the index for different models, like unstructured?
Thanks
I don't know what you mean by "index" for different models, but to specify an AR(1) covariance structure for the residuals, you can add corr=corAR1() to your lme call.
The correlation at lag $1$ is say $r$, where $-1< r <1$ for a stationary $AR(1)$ model. The correlation at lag $k \geq 1$ is $r^k$. This gives you the autocovariance matrix by just multiplying by the variance of $X_t$.

Resources