Load data and calculate regression coefficients.

  • Emilio Castro
  • 12/01/2021

Description

This example illustrates the use of FU_Prec, FU_Files and FU_Statistics modules using a simple program. It reads a dataset from an external file with the proper format and performs different types of regressions to adjust the data.

The dataset can be downloaded here. As stated in the documentation of readMatrix the file must have the following format: The first line indicates the number of rows, columns and the presence of a header line in the second line. In this example, the second line of the file is a header, which will be automatically skipped. After that, the values of the matrix are given. The first lines of this dataset file are:

200 5 T
#X       LINEAR                 LOG                      EXPONENTIAL             POTENTIAL
0.1     0.718079165878583   -1.35638917702126         1.458853476235          1.8948792140233
0.05    1.02619591597279    -2.31765310767541         1.1843683916489         1.34889955912852
0.15    0.19591897475649    -1.82120101012939         0.595024973297403       1.23781397861872
0.2     0.313168781703984   -1.45626913073012         0.678804329891996       1.49480956820386
0.25    0.786822384357196   -0.799471976762694        1.11906961381613        2.0868223843572

Functions used

Code

PROGRAM FU_example3
   ! Example program for FU_Prec, FU_Files and FU_Statistics modules of ecasglez's FortranUtilities,
   ! showing how to load data from a file and how to perform different types of regressions.
   ! The dataset can be downloaded from: https://ecasglez.github.io/FortranUtilities/page/Examples/Example03/example3.dat
   ! compile using: gfortran example3.f90 -o example3 -I/path/to/include/ -lFortranUtilities -L/path/to/lib/ -O2
   ! before running: export LD_LIBRARY_PATH=/path/to/lib:${LD_LIBRARY_PATH}
   ! run using: ./example3
   ! license: MIT.

   USE FU_Prec      , ONLY: dp
   USE FU_Files     , ONLY: readMatrix
   USE FU_statistics, ONLY: linreg, logreg, expreg, potreg

   IMPLICIT NONE

   LOGICAL :: exists
   REAL(KIND=dp), DIMENSION(:,:), ALLOCATABLE :: matrix
   REAL(KIND=dp) :: a, b, r2

   !First check if the dataset exists.
   INQUIRE(FILE='example3.dat', EXIST=exists)
   IF (.NOT.exists) THEN
      WRITE(*,*) 'ERROR: Dataset named "example03.dat" not found.'
      STOP
   END IF

   !Load the data in the file.
   CALL readMatrix('example3.dat',matrix)

   !Calculate regression parameters and print the results.
   CALL linreg(matrix(:,1),matrix(:,2),a,b,r2)
   WRITE(*,'(A,F6.4,A,F6.4,A,F6.4)') 'Linear regression:      f(x) = ',a,' x + ',b,'.      Determination coefficient R2 = ',r2
   CALL logreg(matrix(:,1),matrix(:,3),a,b,r2)
   WRITE(*,'(A,F6.4,A,F6.4,A,F6.4)') 'Logarithmic regression: f(x) = ',a,' ln(x) + ',b,'.  Determination coefficient R2 = ',r2
   CALL expreg(matrix(:,1),matrix(:,4),a,b,r2)
   WRITE(*,'(A,F6.4,A,F6.4,A,F6.4)') 'Exponential regression: f(x) = ',b,' exp(',a,'x).    Determination coefficient R2 = ',r2
   CALL potreg(matrix(:,1),matrix(:,5),a,b,r2)
   WRITE(*,'(A,F6.4,A,F6.4,A,F6.4)') 'Potential regression:   f(x) = ',b,' x^',a,'.        Determination coefficient R2 = ',r2

   DEALLOCATE(matrix)

END PROGRAM FU_example3

Compilation

Compile using the following command. Adjust paths accordingly.

gfortran example3.f90 -o example3 -I/path/to/include/ -lFortranUtilities -L/path/to/lib/ -O2

Execution

Before running, since it has been compiled against the shared library:

export LD_LIBRARY_PATH=/path/to/lib:${LD_LIBRARY_PATH}

Then run with:

./example3

The output of the execution is:

Linear regression:      f(x) = 0.7948 x + 0.5358.      Determination coefficient R2 = 0.9846
Logarithmic regression: f(x) = 0.9877 ln(x) + 0.5260.  Determination coefficient R2 = 0.9150
Exponential regression: f(x) = 0.8921 exp(0.1926x).    Determination coefficient R2 = 0.9214
Potential regression:   f(x) = 3.6021 x^0.4330.        Determination coefficient R2 = 0.9764

These results and the dataset can be plotted using a external tool to see the adjustment:

Regression plots