applying a function to a datatable using rowfun
Show older comments
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
Accepted Answer
More Answers (0)
Categories
Find more on Linear and 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!