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
Play games and win prizes!Learn more