4 | Data Import/Export & Regression

In this section, we look at importing and exporting datasets in Matlab. We then look at different ways of using regression on experimental data to produce a model.

At the end of this section you should be able to:

  1. Import data files as csv or Excel files

  2. Export data files to csv or Excel files

  3. Model functional relationships from data using the fit function

  4. Assess if a fit is “good” using qualitative and qualitative arguments

  5. Use the results of the fit to assess the model parameters and predict dependent variable values outside of the original dataset

Data Import and Export

Data Import: Import raw numerical data from csv or Excel files into Matlab.

Data Export: Export matrices from matlab as csv or Excel files for use within Matlab or with other programs. We can also save variables into mat files that are readable by Matlab.

Regression

You will need the curve-fitting toolbox (which contains the “fit” function) installed to follow these examples.

Linear Regression: In this example, we fit data with a linear (slope and intercept) fit. We assess the quality of the fit using the R² value from the goodness of fit. When using the fit function, the goodness of fit is returned as a struct. You can think of a struct as a variable that contains other variables. To access the “subvariables,” use the syntax of gof.rsquare where “gof” is the struct name and “rsquare” is the field you want the value of.

Polynomial Regression: In this example, we fit data with a quadratic fit. We assess the quality of the fit using the R² value or adjusted-R² value from the goodness of fit and compare it to using a linear or cubic fit.

Exponential Regression: using regression to perform parameter estimation when a theoretical relationship between the independent and dependent variables is known. In this example, we look for the time constant of a simple RC circuit using experimental data and compare it to the theoretical relationship between the voltage and time. Information on RC circuits.

Anthropometric Regression: using datasets of body measurements to use height as a predictor for other body length measurements. We use clinically collected data to find the correlation between measurements.

The r2 and adjusted r2 measures are just two metrics for determining how “good” the data are fit by the function. However, these measures are sensitive to outliers and a low value could still mean the data are fit well and a high value may still mean the data are not fit well.

These four data sets have the same

  1. Line of best fit

  2. r2

  3. mean values of x and y

Lecture Code