Error running bootstrap model on fitlm function
2 次查看(过去 30 天)
显示 更早的评论
I currently have the below code, but I keep receiving an error when trying to run the bootstrap. Could anybody explain why I might be receiving the below error? I have tried with and without the 'linear' argument.
X = [auto_df.displacement, auto_df.horsepower, auto_df.weight];
y = auto_df.mpg;
auto_mdl = fitlm(X,y, 'linear');
boot_auto_mdl = bootstrp(500, @fitlm, X, y, 'linear');
Error using internal.stats.parseArgs (line 42)
Wrong number of arguments.
Error in bootstrp>extractNameValuePairs (line 338)
= internal.stats.parseArgs(defSpecialArgs,defSpecialValues,specialArgs{:});
Error in bootstrp (line 142)
[weights, options, bootargs] = extractNameValuePairs(varargin{:});
0 个评论
回答(1 个)
Ayush Aniket
2024-8-30
The bootstrp function in MATLAB is used to perform bootstrapping, which involves resampling your data with replacement and computes a statistic by applying a function to each bootstrap sample. The fitlm function is not directly compatible with bootstrp because fitlm expects data in a specific format and returns a model object, not a simple numeric output that bootstrp can handle.
To use bootstrp with fitlm, you need to define a custom function that fits the model and returns a numeric statistic for instance the coefficients of a linear regression model as shown below:
% Define a custom function that fits a linear model and returns coefficients
function coefficients = fitlm_coefficients(data)
X = data(:, 1:end-1); % All columns except the last one
y = data(:, end); % The last column
mdl = fitlm(X, y, 'linear');
coefficients = mdl.Coefficients.Estimate'; % Return coefficients as a row vector
end
% Combine X and y into a single matrix for bootstrapping
data = [X, y];
% Perform bootstrapping
bootCoefficients = bootstrp(500, @fitlm_coefficients, data);
This approach allows you to bootstrap the coefficients or any other statistic derived from the linear model.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!