Last active
March 30, 2019 10:07
-
-
Save nilforooshan/61caac472ba2c3f309f19b7b56eb94d1 to your computer and use it in GitHub Desktop.
f90: Pearson's Chi-square test. Test the frequency distributions of categorical variables in a table of frequencies. *.exe and *.out are the Windows and Linux executables.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
PROGRAM chisq_test | |
IMPLICIT NONE | |
! Declarations | |
INTEGER:: i, j, m, n, df, error | |
INTEGER,DIMENSION(:,:),ALLOCATABLE:: o | |
REAL:: chi | |
REAL,DIMENSION(:,:),ALLOCATABLE:: e, c | |
CHARACTER(20):: infile, exiit | |
! Opening prints | |
PRINT*, | |
PRINT*, | |
PRINT*, 'This program is written by Mohammad A. Nilforooshan.' | |
PRINT*, 'All rights reserved.' | |
PRINT*, 'http://sites.google.com/site/mannprofile/' | |
! Ask for the input file, open and check it | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Type the name of the input file.' | |
READ*, infile | |
OPEN(UNIT=10, FILE=infile, STATUS='OLD', IOSTAT=error) | |
IF (error/=0) THEN | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Error reading file.' | |
STOP | |
END IF | |
! Ask for the size of the matrix | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Insert the number of rows.' | |
READ*, m | |
PRINT*, 'Insert the number of columns.' | |
READ*, n | |
IF (m>9 .OR. n>9) THEN | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Sorry, up to 9 rows/columns is supprted.' | |
PRINT*, 'Contact the programmer to get more space.' | |
STOP | |
END IF | |
! Read the data | |
ALLOCATE(o(m,n)) | |
READ(10,*) ((o(i,j),j=1,n),i=1,m) | |
! Create the matrix of expectations | |
ALLOCATE(e(m,n)) | |
DO i=1,m | |
DO j=1,n | |
e(i,j)=SUM(o(i,:))*SUM(o(:,j))/REAL(SUM(o(:,:))) | |
END DO | |
END DO | |
! Estimate the Chisq table and the Chisq value | |
ALLOCATE(c(m,n)) | |
DO i=1,m | |
DO j=1,n | |
c(i,j)=((o(i,j)-e(i,j))**2)/REAL(e(i,j)) | |
END DO | |
END DO | |
df=(m-1)*(n-1) | |
chi=SUM(c(:,:)) | |
! Write the results on the screen | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Observed frequencies:' | |
PRINT*, '---------------------' | |
DO i=1,m | |
WRITE(*,'(9I6)') (o(i,j), j=1,n) | |
END DO | |
PRINT*, '---------------------' | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Expected frequencies:' | |
PRINT*, '---------------------' | |
DO i=1,m | |
WRITE(*,'(9F9.2)') (e(i,j), j=1,n) | |
END DO | |
PRINT*, '---------------------' | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Chi-sqrd proportions:' | |
PRINT*, '---------------------' | |
DO i=1,m | |
WRITE(*,'(9F7.3)') (c(i,j), j=1,n) | |
END DO | |
PRINT*, '---------------------' | |
PRINT*, | |
PRINT*, | |
WRITE(*,'(A,F6.3,A,I2)') 'Chi-squared = ',chi,', df = ',df | |
PRINT*, | |
PRINT*, | |
PRINT*, 'Press any key to exit.' | |
READ*, exiit | |
IF (exiit=='a') THEN | |
GO TO 120 | |
END IF | |
120 END PROGRAM chisq_test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment