How do I use specific columns from a csv file

32 views (last 30 days)
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)
Unrecognized function or variable 'FVC'.
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.

Accepted Answer

Star Strider
Star Strider on 5 Sep 2022
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')
fvc = 127×3 table
FVC Height Weight ______ ______ ______ 2.8132 156 51 2.6895 141.94 37 2.2441 148.07 35 4.4088 178 58 2.6369 146.01 38 2.3772 143.93 35 3.3349 159 47 3.0284 150.07 52 2.7341 149.91 43 3.0715 156.1 46 4.3504 167.09 55 3.072 153.03 51 2.2389 148.08 39 2.6339 153.99 39 2.3419 148.04 43 2.9945 155.08 44
mdl = fitlm(fvc, 'Weight~FVC+Height')
mdl =
Linear regression model: Weight ~ 1 + FVC + Height Estimated Coefficients: Estimate SE tStat pValue ________ _______ _______ __________ (Intercept) -60.355 12.623 -4.7814 4.8412e-06 FVC 5.0559 1.5982 3.1636 0.0019603 Height 0.58529 0.10432 5.6106 1.2504e-07 Number of observations: 127, Error degrees of freedom: 124 Root Mean Squared Error: 5.61 R-squared: 0.592, Adjusted R-Squared: 0.586 F-statistic vs. constant model: 90.1, p-value = 6.93e-25
mdl = fitlm(fvc, 'FVC~Height')
mdl =
Linear regression model: FVC ~ 1 + Height Estimated Coefficients: Estimate SE tStat pValue ________ _________ _______ __________ (Intercept) -4.9119 0.55321 -8.8788 6.0377e-15 Height 0.051196 0.0036219 14.135 1.0884e-27 Number of observations: 127, Error degrees of freedom: 125 Root Mean Squared Error: 0.314 R-squared: 0.615, Adjusted R-Squared: 0.612 F-statistic vs. constant model: 200, p-value = 1.09e-27
mdl = fitlm(fvc, 'Height~FVC')
mdl =
Linear regression model: Height ~ 1 + FVC Estimated Coefficients: Estimate SE tStat pValue ________ _______ ______ __________ (Intercept) 117.73 2.5002 47.088 2.083e-81 FVC 12.016 0.85006 14.135 1.0884e-27 Number of observations: 127, Error degrees of freedom: 125 Root Mean Squared Error: 4.81 R-squared: 0.615, Adjusted R-Squared: 0.612 F-statistic vs. constant model: 200, p-value = 1.09e-27
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 Comments
Declan
Declan on 5 Sep 2022
Hi, so basically I have a file called 'fvc-2022.csv' and in the file there are 3 columns. The first column is called 'FVC', the second is called 'Height', and the third is called 'Weight'. 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
fitLinearModel(fvc.FVC, fvc.Height)
Star Strider
Star Strider on 5 Sep 2022
I also have a premade function called 'fitLinearModel' which uses two vectors and finds the intercept, slope and residual variance.
I just found that function in an earlier Comment.
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.
.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Sep 2022
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 Comments
Declan
Declan on 5 Sep 2022
have you checked the csv file? Height is the name of another column in the data set. Also, fitLinearModel is a function that I made beforehand. Ill paste it below.
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
Image Analyst
Image Analyst on 5 Sep 2022
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

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!