Python Forum

Full Version: SIR Model: Saving predicted value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I am experimenting with SIR model for spread of disease using the following code. In particular, at each iteration I feed in different N, I0, and R0 values in the model from the CSV file. For each set of parameters, the model makes predictions for S, I, and R outcomes 1-75 days ahead. Usually, I make a plot and examine the result visually, but how can I record the predicted value for "I" parameter after each iteration in a 7-day time and store it as a separate column in the CSV file?

Your help would be greatly appreciated.

import pandas as pd
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

df = pd.read_csv('params_temp.csv',  encoding="utf-8")

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.5, 0.05
    t = np.linspace(0, 75, 75) # Days ahead

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
Say, I do:

element=I[6]
print(element)
Then how do I save the result in a column in a table? I have no clue :( Feedback is greatly appreciated.
Okay, say I do:

import pandas as pd
import numpy as np
from scipy.integrate import odeint
import matplotlib.pyplot as plt

df = pd.read_csv('params_temp.csv', index_col = 'FIPS', encoding="utf-8”)

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.4, 1./15 
    t = np.linspace(0, 160, 160)

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
   
    element=I[6]
    df['pred'] = element
    df.to_csv('params_temp.csv')
But it saves the same value (guess the first predicted "I" for the first FIPS) value across all FIPS in the CSV. What am I doing wrong?..

Trying another approach:

vals=[]

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.4, 1./15 
    t = np.linspace(0, 160, 160)

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
    
    vals.append(element)
    
vals
[99.43974077754876,
 99.43974077754876,
 99.43974077754876,
 99.43974077754876,
 99.43974077754876,
 99.43974077754876]
And I still get repeated values :(( What a frustration...
The solution is:
vals=[]

for n, i, r in zip(df["N"], df["I0"], df["R0"]):
    N = n
    I0, R0 = i, r
    S0 = N - I0 - R0
    beta, gamma = 0.4, 1./15 
    t = np.linspace(0, 160, 160)

    def deriv(y, t, N, beta, gamma):
        S, I, R = y
        dSdt = -beta * S * I / N
        dIdt = beta * S * I / N - gamma * I
        dRdt = gamma * I
        return dSdt, dIdt, dRdt

    y0 = S0, I0, R0
    ret = odeint(deriv, y0, t, args=(N, beta, gamma))
    S, I, R = ret.T
    element=I[6]
    vals.append(element)
    
vals
Thanks everybody for help!