how to compute a linear mixed effect using nlmefit?
조회 수: 5 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2013년 3월 13일
Do you in fact want algebraic matrix multiplication? Or do you want element-by-element multiplication which is the .* operator ?
채택된 답변
Tom Lane
2013년 3월 14일
It looks like nlmefit invokes your model function with betas as a row vector. Try this:
model = @(Betas,X) (X*Betas(:))
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Get Started with Statistics and Machine Learning Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!