how to compute a linear mixed effect using nlmefit?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 13 Mär. 2013
Do you in fact want algebraic matrix multiplication? Or do you want element-by-element multiplication which is the .* operator ?
Akzeptierte Antwort
Tom Lane
am 14 Mär. 2013
It looks like nlmefit invokes your model function with betas as a row vector. Try this:
model = @(Betas,X) (X*Betas(:))
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Nonlinear Regression finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!