Skip to content

Instantly share code, notes, and snippets.

@cheery
Created January 25, 2024 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cheery/9a423e3a2a44af87df5e4a9062f8dda8 to your computer and use it in GitHub Desktop.
Save cheery/9a423e3a2a44af87df5e4a9062f8dda8 to your computer and use it in GitHub Desktop.
Carnivore mortality study on NHANES I dataset.
is_carnivore_in_1971 = {}
is_alive_in_1992 = {}
age_of_death = {}
with open('nhanes/DU4701.txt', 'r') as fd:
rows = fd.readlines()
for row in rows:
sample = row[0:5]
meat_freq = int(row[224:228])
diet = [0, 0, 0]
carnivore = 0
if 1010 <= meat_freq <= 1090:
carnivore += 1
fish_freq = int(row[229:232])
if 1010 <= fish_freq <= 1090:
carnivore += 1
egg_freq = int(row[232:236])
if 1010 <= egg_freq <= 1090:
carnivore += 1
beans_freq = int(row[240:244])
if 1010 <= beans_freq <= 1090:
carnivore -= 3
fruits_freq = int(row[244:248])
if 0000 <= fruits_freq <= 0000:
carnivore += 3
if 1010 <= fruits_freq <= 1090:
carnivore -= 3
is_carnivore_in_1971[sample] = (carnivore >= 1)
with open('nhanes/n92vitl.txt', 'r') as fd:
rows = fd.readlines()
for row in rows:
sample = row[11:16]
current_vital_status = int(row[16])
if current_vital_status == 1:
is_alive_in_1992[sample] = True
elif current_vital_status == 3:
is_alive_in_1992[sample] = False
age_of_death[sample] = int(row[66:68])
dead_carnivores = 0
live_carnivores = 0
dead_others = 0
live_others = 0
dead_carnivores_ages = []
dead_others_ages = []
for sample in is_alive_in_1992:
if is_alive_in_1992[sample]:
if sample not in is_carnivore_in_1971:
pass
elif is_carnivore_in_1971[sample]:
live_carnivores += 1
else:
live_others += 1
else:
if sample not in is_carnivore_in_1971:
pass
elif is_carnivore_in_1971[sample]:
dead_carnivores += 1
dead_carnivores_ages.append(age_of_death[sample])
else:
dead_others += 1
dead_others_ages.append(age_of_death[sample])
print("mortality of carnivores: ", dead_carnivores / (live_carnivores + dead_carnivores))
print("mortality of others: ", dead_others / (live_others + dead_others))
print("average age of death of carnivore: ", sum(dead_carnivores_ages) / len(dead_carnivores_ages))
print("average age of death of others: ", sum(dead_others_ages) / len(dead_others_ages))
# mortality of carnivores: 0.48297604035308955
# mortality of others: 0.37023797364926975
# average age of death of carnivore: 72.35770234986946
# average age of death of others: 74.23310344827586
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment