Use variable from previous step (lag variable) #2000
Unanswered
AlexandraGlaz
asked this question in
Q&A
Replies: 2 comments 2 replies
-
Can you edit your question with a minimum self-contained code that can be built on, for a concrete starting point? |
Beta Was this translation helpful? Give feedback.
1 reply
-
Try this code: import mesa
class Household(mesa.Agent):
def __init__(self, unique_id, model, var1=0, var1_lag=1, var2=0):
super().__init__(unique_id, model)
self.var1_timeseries = [var1_lag, var1]
self.var2 = var2
def fun1(self):
self.var1_timeseries.append(self.var1_timeseries[-1] + 1)
self.var2 = self.var1_timeseries[-1] - self.var1_timeseries[-2]
def step(self):
self.fun1()
class Economy(mesa.Model):
def __init__(self, N):
super().__init__()
self.num_agents = N
self.schedule = mesa.time.RandomActivation(self)
for i in range(self.num_agents):
a = Household("hh_{0}".format(i), self)
self.schedule.add(a)
self.datacollector = mesa.DataCollector(
agent_reporters={
"Var1": lambda agent: agent.var1_timeseries[-1],
"Var2": "var2",
}
)
def step(self):
self.schedule.step()
self.datacollector.collect(self)
model_base = Economy(1)
for i in range(20):
model_base.step()
agent_data = model_base.datacollector.get_agent_vars_dataframe()
print(agent_data) |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! I'm building macroeconomic model and some of my variables have lagged variables, for exampe AR(1) prosses. So to do it now I create additional var for each lag in Class, save var on step t-1 and using in on step t. So that I have a lot of additional vars.
Is someone know how to do in more effective?
I will be grateful for any help!
Please find my current code in attach.
mesa_code.txt
Beta Was this translation helpful? Give feedback.
All reactions