Hi,
In 2020 it worked properly, now I am going back to use it again and it gives me an error:

library(nlme)
model_chg <- gls(change ~ method * time,
correlation = corSymm(form = ~as.numeric(time)|patient_id),
weights = varIdent(form = ~ 1|time),
data = pd_diffs_sub,
method = "REML")
Error in Initialize.corSymm(X[[i]], ...) :
covariate must have unique values within groups for "corSymm" objects

As usual in R when it gives an error it gives an info that I completely don't understand and have got no clue what to do.
Please help if you can.
best

Without pd_diffs_sub all that can be offered is a chat

The error you're encountering when using the gls() function (from the nlme package in R) indicates an issue related to the use of a correlation structure in your generalized least squares (GLS) model. Specifically, the error:

Error in Initialize.corSymm(X[[i]], ...) :
covariate must have unique values within groups for "corSymm" objects

relates to the use of the corSymm correlation structure in your model. Let's break down what this error means and how you might address it:

Understanding corSymm in GLS Models: The corSymm (correlation symmetry) object is used in GLS models to specify a correlation structure for the residuals. This is often used when there's a reason to believe that the residuals are correlated in a particular way, such as in repeated measures data or longitudinal data.

Unique Values Within Groups: The error message is indicating that the covariate used for modeling the correlation structure must have unique values within each group. In the context of repeated measures or longitudinal data, this usually refers to the time variable or another variable that indicates the order of measurements. For the correlation structure to be meaningful and identifiable, each measurement within a group (e.g., individual subject or experimental unit) should have a unique value for this covariate.

Common Causes of the Error:

Duplicate Time Points: If your data contains repeated measures, ensure that there are no duplicate time points (or other covariates used for correlation) within the same group. Each time point should be unique within each group.

Incorrect Grouping Variable: The error can also occur if the grouping variable does not correctly partition the data into independent groups. Each group should correspond to a separate, independent unit (like a subject in a study).

How to Address the Error:

Check Your Data: Ensure that the covariate used in the corSymm function has unique values within each group. If it's a time variable, check for duplicate time points within the same group.

Verify Grouping Variable: Make sure that your grouping variable correctly identifies the independent units in your data. Each group should contain data from only one unit.

Example of Correct Usage:
If you're modeling repeated measures data, your gls call might look something like this:

Here, time_variable should have unique values within each group_variable.

If you continue to encounter difficulties, it might be helpful to examine your data closely and ensure that it is structured appropriately for the type of analysis you are attempting to perform with the gls function.

Thank you very much indeed for detailed explanation @technocrat,

I have additional questions:
1.

What do you mean by that ?

I have this code:

model <- nlme::gls(change ~ time * method,
correlation = corCompSymm(form = ~1|patient_id),
or:
correlation = corSymm(form = ~as.numeric(time)|patient_id),
weights = varIdent(form = ~ 1|time),
data = my_data,
method = "REML")

How can I understand this:

~1|patient_id

and what is the difference to:

~as.numeric(time)|patient_id

What are the weights for ?

weights = varIdent(form = ~ 1|time),

Is there somewhere a place I could read about all this a bit cryptic symbols like vertical bar or tilde used here, but in an understandable way, please ?

In the R programming language, especially in the context of the nlme package, understanding the varIdent function and its arguments is crucial for effective model fitting. Let's break down the use of varIdent in your model.

Purpose of varIdent: The varIdent function is used to allow for different variances for different levels of a factor in the residuals of a model. This is particularly useful when you suspect that the variability in your response variable changes across different levels of a factor.

Understanding the Argument form = ~ 1|time:

form = ~ 1|time is the formula used in varIdent to specify the variance structure.

In this formula, 1|time means that the variances are allowed to be different for each level of the factor time.

The 1 on the left side of the | indicates that it's a simple variance structure (i.e., it does not depend on any predictors).

time is the factor variable based on which the variance is being modeled. Each level of time will have its own variance estimate.

Model Context:

Your model is a Generalized Least Squares (GLS) model fitted using the gls function from the nlme package.

The model is predicting change based on time, method, and their interaction (time * method).

The correlation argument is used to specify a correlation structure for the residuals. You've shown two options: corCompSymm and corSymm, each with different formulations.

The weights argument with varIdent indicates that the model accounts for heteroscedasticity (non-constant variance) across different levels of time.

Implications for the Model:

By using varIdent(form = ~ 1|time), you are allowing the model to estimate different variances for the residuals for each level of time. This can be particularly useful if, for instance, the variability of change is different at different times.

This approach can lead to a more accurate model as it does not assume constant variance across all observations.

Further Considerations:

It's important to check whether this model specification is appropriate for your data. This can be done by looking at diagnostic plots of the residuals and conducting tests for homoscedasticity.

If you have many levels in time, the model might become complex and over-parameterized, so it's crucial to balance model complexity with the available data.

In summary, the varIdent function in your model is used to allow different residual variances for each level of the time factor, which can improve model fit and accuracy if the assumption of constant variance is violated in your data.

Professor Boker quote another expert on some of the definitional diffi culties in the concept of fixed and random effects in the section immediately following the table show in the post, Should I treat factor xxx as fixed or random? So, the answer to your question is in two parts:

What's random depends on the data. Advanced statistics often becomes so involved it's easy to get too focused on how to the detriment of what. Random is a classification of how an outcome of applying a model to particular data fits in the overall evaluation of a statistical tests. That's why trying to understand it is problematic in the abstract without motivating data.

Fortunately, intercept has the straightforward mechanical interpretation of a point lying at the intersection of two axes . When considering a model in Cartesian terms that point is the value of the measure of interest on the y-axis when the corresponding value of the measure on the x-axis is zero.