How do I use specific columns from a csv file
조회 수: 5 (최근 30일)
이전 댓글 표시
Hi, I have a csv file and I am trying to use specific columns to input into a function. Below is the code that I have so far where FVC and Height are the names of two of the columns from my csv data.
%% Load data
fvc = readtable("fvc-2022.csv");
%% Fit linear model
fitLinearModel(FVC, Height)
btw, 'fitLinearModel' is the function that I am using and I have attached the csv data set. I know that the 'FVC, Height' part of the code is incorrect so I was wondering what the right code is to add in here.
Thanks.
댓글 수: 0
채택된 답변
Star Strider
2022년 9월 5일
I am not certain what you want to regress.
Perhaps one of these —
fvc = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1117350/fvc-2022.csv')
mdl = fitlm(fvc, 'Weight~FVC+Height')
mdl = fitlm(fvc, 'FVC~Height')
mdl = fitlm(fvc, 'Height~FVC')
Make appropriate changes to get the desired result.
See the documentation on fitlm for details, and the links in See Also for related functions and documentation.
.
댓글 수: 2
Star Strider
2022년 9월 5일
‘I also have a premade function called 'fitLinearModel' which uses two vectors and finds the intercept, slope and residual variance.’
‘What I want to do is use two columns from the csv file as the two vectors in that premade function but I'm not too sure on how to call it. I believe right now that the proper coding is the following...’
The way you’re calling it is correct. However, it has three outputs, so you need to include them in the function call, for example:
[intercept, slope, resVar] = fitLinearModel(fvc.FVC, fvc.Height)
Otherwise, the call to it (without specifying the outputs) will return only the ‘Intercept’ output. Also, it’s ineffecient to do the regression twice as your function currently does. Use polyval with the returned polynomial and the ‘x’ vector, then calculate the residuals and their variance from that.
FWIW, using fitlm is likely easier to use and more robust. It does all the statistical calculations internally, and reports the results. If you want to get the residual variance from it yourself, use the predict function (there are several, the one I linked to appears to be the appropriate one, although MATLAB will likely choose the correct one given the correct arguments), then calculate the residuals and their variance from that.
.
추가 답변 (1개)
Image Analyst
2022년 9월 5일
It's not finding your file. Your file lives in a different folder than your script, and it is also not on the search path. Use fullfile to specify the full path and base file name of your file.
댓글 수: 6
Image Analyst
2022년 9월 5일
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
t = readtable('fvc-2022.csv')
% Fit line through Heights
x = t.FVC;
y = t.Height;
[intercept, slope, resVar] = fitLinearModel(x,y)
% Fit line through Heights
x = t.FVC;
y = t.Weight;
[intercept, slope, resVar] = fitLinearModel(x,y)
function [intercept, slope, resVar] = fitLinearModel(x,y)
%intercept
P = polyfit(x,y,1);
intercept=P(2);
%slope
slope=P(1);
%resVar
A = [ones(numel(x),1), x(:)];
b = y(:);
c = A\b;
format long
resVar=var(y-(c(1)+c(2)*x))*(numel(x)-1)/(numel(x)-2);
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Classification Trees에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!