applying a function to a datatable using rowfun
이전 댓글 표시
I'm attempting to apply a function to a datable using rowfun. The table contains two variables of interest, "date" and "d18O", which are grouped by site and depth (site name also included).
I want to fit a sinusoidal function, "isofcn" to the date (x) and d18O (y). I would like a separate fit for each depth at each site. My current version below is only doing one fit per site.
I also think it might be possible to clean this up so that I only have to have 1 function below (i.e., combine function "doit" and iso_fcn", but I'm a bit lost.
Any help would be so appreciated.
*note the sample data table contains only a tiny subset of data, so the uncertainty on the sine fits will probably be extremely high. this issue should resolve when I use all the data.
tT=readtable("data1.csv", "VariableNamingRule","preserve");
tT.Properties.VariableNames(2)={'Site'}; % shorten to be more convenient to use
G=grpstats(tT,{'Site','Depth'},{'mean','median','std'},'DataVars',{'Date','d18O'});
hSc=rowfun(@doit,tT,'GroupingVariables',{'Site'},'InputVariables',{'Date','d18O','Site','Depth','name'},'OutputFormat','uniform');
function out=doit(x,y,s,d,n)
soil_params_guess= zeros(3,1);
mdl_soil = fitnlm(x,y,@iso_fcn,soil_params_guess(:,1));
soil_params_fit = table2array(mdl_soil.Coefficients(:,1));
out = {soil_params_fit};
end
function F = iso_fcn(isofcn_params,date)
F = isofcn_params(1).*(cos(2*pi.*(1/365).*date)) + isofcn_params(2).*(sin(2*pi.*(1/365).*date)) + isofcn_params(3);
end
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!