how to compute a linear mixed effect using nlmefit?
2 views (last 30 days)
Show older comments
Hi,
I'd like to fit a simple linear mixed model Y=XB+Zb+e with X the design matrix of the fixed effect (always the same size but not always the same values) and Z described subjects (which I specify as random)
here is the code I used
% generate data
for subject=1:10
x(:,subject) = [1:10]+randi(30,1);
coef(subject) = (3+rand(1));
y(:,subject) = coef(subject)*x(:,subject)+3*randn(10,1)- 5*mean(x(:,subject));
end
% create X, Y, subject for nlmefit
Y = y(:);
X = [x(:) ones(100,1)];
subject = sum(kron(diag(1:10),ones(10,1)),2);
% fit the data using 'model'
model = @(Betas,X) (X*Betas)
[Betas,PSI,stats] = nlmefit(X,Y,subject,[],model,[1 0])
The error is: Error using * Inner matrix dimensions must agree. Error in @(Betas,X)(X*Betas)
in a fixed effect Betas=pinv(X)Y and the fitted data = X*Betas, and that why i defined model this way, assuming that for each subject, parameters are fitted using 'model' ?? any idea what I am doing wrong ?
Thanks Cyril
2 Comments
Walter Roberson
on 13 Mar 2013
Do you in fact want algebraic matrix multiplication? Or do you want element-by-element multiplication which is the .* operator ?
Accepted Answer
Tom Lane
on 14 Mar 2013
It looks like nlmefit invokes your model function with betas as a row vector. Try this:
model = @(Betas,X) (X*Betas(:))
1 Comment
More Answers (0)
See Also
Categories
Find more on Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!