## Group-specific predictors in nlmefit

Asked by the cyclist

### the cyclist (view profile)

on 26 Jul 2012
Accepted Answer by Tom Lane

### Tom Lane (view profile)

Does anyone have a simple example of estimating group-specific predictors (non-empty VFUN model parameter) for the nlmefit function?

## Products

### Tom Lane (view profile)

Answer by Tom Lane

### Tom Lane (view profile)

on 27 Jul 2012

If you are happy with a toy illustration rather than a realistic example, here's one. Set up some fake data following a linear model:

```subject =  kron((1:6)',ones(4,1));   % 6 subjects measure at 4 times
time = kron(ones(6,1),(1:4)');
weight = [100 150 140 200 190 140]'; % weight of each subject
effect = [1 -1 0 -2 2 0]';           % a "random" effect of each subject
y = 5 + effect(subject) - time + .03*weight(subject) + randn(size(subject));
```

Fit a linear function of time and weight by including weight in the first input as a column. Since weight is measured only once per subject, we have to expand it to the right length.

```model = @(phi,t) phi(1) + phi(2)*t(:,1) + phi(3)*t(:,2);
phi0 = [1 1 1];
[beta,PSI,stats,br] = nlmefit([time weight(subject)],y,subject,[],model,phi0,'reparam',1);
beta
br
```

Now move weight to the separate V input where it does not have to be expanded to have one value per observation:

```model = @(phi,t,v) phi(1) + phi(2)*t + phi(3)*v;
phi0 = [1 1 1];
[beta,PSI,stats,br] = nlmefit(time,y,subject,weight,model,phi0,'reparam',1);
beta
br
```

the cyclist

### the cyclist (view profile)

on 27 Jul 2012

Thanks, Tom. I often prefer toy examples to "real" examples, because it is easier to make the connection between the data and the output.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi