In [6]:
## 6.13

## initial values

stockA <- 100
stockB <- 100

addA <- 0.05
removeA <- 0.05
addA_dependingB <- 0.01

addB <- 0.05
removeB <- 0.05
addB_dependingA <- 0.01

for( round in 1:50 ) {
    stockA <- stockA + stockA * addA + stockB * addA_dependingB
    stockA <- stockA - stockA * removeA
    
    stockB <- stockB + stockB * addB + stockB * addB_dependingA
    stockB <- stockB - stockB * removeB
    
}
    
print( stockA )
print( stockB )
[1] 141.7338
[1] 141.7338
In [9]:
## 6.14

## initial values

stockA <- 100
stockB <- 100

addA <- 0.05
removeA <- 0.05
addA_dependingB <- 0.01

addB <- 0.05
removeB <- 0.05
addB_dependingA <- 0.01

listA <- list()
listB <- list()

for( round in 1:50 ) {
    
    listA <- c( listA , stockA )
    listB <- c( listB , stockB )
    
    stockA <- stockA + stockA * addA + stockB * addA_dependingB
    stockA <- stockA - stockA * removeA
    
    stockB <- stockB + stockB * addB + stockB * addB_dependingA
    stockB <- stockB - stockB * removeB

}
    
print( unlist( listA ) )
print( unlist( listB ) )
 [1] 100.0000 100.7000 101.4049 102.1147 102.8295 103.5493 104.2742 105.0041
 [9] 105.7391 106.4793 107.2247 107.9752 108.7311 109.4922 110.2586 111.0304
[17] 111.8077 112.5903 113.3784 114.1721 114.9713 115.7761 116.5865 117.4026
[25] 118.2244 119.0520 119.8854 120.7246 121.5697 122.4206 123.2776 124.1405
[33] 125.0095 125.8846 126.7658 127.6531 128.5467 129.4465 130.3527 131.2651
[41] 132.1840 133.1093 134.0410 134.9793 135.9242 136.8756 137.8338 138.7986
[49] 139.7702 140.7486
 [1] 100.0000 100.7000 101.4049 102.1147 102.8295 103.5493 104.2742 105.0041
 [9] 105.7391 106.4793 107.2247 107.9752 108.7311 109.4922 110.2586 111.0304
[17] 111.8077 112.5903 113.3784 114.1721 114.9713 115.7761 116.5865 117.4026
[25] 118.2244 119.0520 119.8854 120.7246 121.5697 122.4206 123.2776 124.1405
[33] 125.0095 125.8846 126.7658 127.6531 128.5467 129.4465 130.3527 131.2651
[41] 132.1840 133.1093 134.0410 134.9793 135.9242 136.8756 137.8338 138.7986
[49] 139.7702 140.7486
In [18]:
## 6.15a

agent_red_neighbours <- list( 0, 5, 7, 4 )
agent_blue_neighbours <- list( 2, 7, 4, 3 )
agent_own_colour <- list( 'blue', 'red', 'blue', 'red' )

for( colour in agent_own_colour ) {
    print( colour )
}
[1] "blue"
[1] "red"
[1] "blue"
[1] "red"
In [19]:
## 6.15b

agent_red_neighbours <- list( 0, 5, 7, 4 )
agent_blue_neighbours <- list( 2, 7, 4, 3 )
agent_own_colour <- list( 'blue', 'red', 'blue', 'red' )

for( index in 1:length( agent_own_colour ) ) {
    print( paste( agent_own_colour[[index]], agent_red_neighbours[[index]], agent_blue_neighbours[[index]] ) )
}
[1] "blue 0 2"
[1] "red 5 7"
[1] "blue 7 4"
[1] "red 4 3"
In [26]:
## 6.15c

agent_red_neighbours <- list( 0, 5, 7, 4 )
agent_blue_neighbours <- list( 2, 7, 4, 3 )
agent_own_colour <- list( 'blue', 'red', 'blue', 'red' )

for( index in 1:length( agent_own_colour ) ) {
    
    neigbours_total <- agent_red_neighbours[[index]] + agent_blue_neighbours[[index]]
    
    if( agent_own_colour[[index]] == "red" ) {
        if( agent_blue_neighbours[[index]] > neigbours_total / 2 ) {
            print( paste( "agent", index, "moves") )
        }
    }
        
    if( agent_own_colour[[index]] == "blue" ) {
        if( agent_red_neighbours[[index]] > neigbours_total / 2 ) {
            print( paste( "agent", index, "moves") )
        }
    }
}
[1] "agent 2 moves"
[1] "agent 3 moves"
In [40]:
## 6.16

entries <- list()

data <- read.csv("microsimulation.csv", header = FALSE )

for( rn in 1:nrow(data) ) {
    
    entry = list()
    
    entry[['id']] <- data[ rn, 1 ]
    entry[['sex']] <- data[ rn, 2 ]
    entry[['age']] <- data[ rn, 3 ]
    entry[['number_of_children']] <- data[ rn, 4 ]
    entry[['income']] <- data[rn, 5]
    entry[['tax']] <- data[rn, 6 ]

    entries <- c( entries, list( entry ) ) ## note: the entry is wrapped again as a list to ensure we correctly append not just a flat list with keys
}

print( entries )
[[1]]
[[1]]$id
[1] 1

[[1]]$sex
[1] "men"

[[1]]$age
[1] 35

[[1]]$number_of_children
[1] 0

[[1]]$income
[1] 50

[[1]]$tax
[1] 10


[[2]]
[[2]]$id
[1] 2

[[2]]$sex
[1] "women"

[[2]]$age
[1] 34

[[2]]$number_of_children
[1] 0

[[2]]$income
[1] 50

[[2]]$tax
[1] 10


[[3]]
[[3]]$id
[1] 3

[[3]]$sex
[1] "men"

[[3]]$age
[1] 20

[[3]]$number_of_children
[1] 0

[[3]]$income
[1] 30

[[3]]$tax
[1] 5


[[4]]
[[4]]$id
[1] 4

[[4]]$sex
[1] "women"

[[4]]$age
[1] 29

[[4]]$number_of_children
[1] 3

[[4]]$income
[1] 40

[[4]]$tax
[1] 6


[[5]]
[[5]]$id
[1] 5

[[5]]$sex
[1] "women"

[[5]]$age
[1] 80

[[5]]$number_of_children
[1] 0

[[5]]$income
[1] 44

[[5]]$tax
[1] 6


In [57]:
## 6.17

## age bins
agebin = list()
## this a dummy approach for this
agebin[['<25']] <- 0
agebin[['25-49']] <- 0
agebin[['50-74']] <- 0
agebin[['>74']] <- 0


for( entry in entries ) {
    age <- entry[['age']]
    if( age < 25 ) {
        agebin[['<25']] <- agebin[['<25']] + 1
    }
    if( age >= 25 & age < 50 ) {
        agebin[['25-49']] <- agebin[['25-49']] + 1
    }
    if( age >= 50 & age < 75 ) {
        agebin[['50-74']] <- agebin[['50-74']] + 1
    }
    if( age > 74 ) {
        agebin[['>74']] <- agebin[['>74']] + 1
    }
}

for( bin_name in names( agebin ) ) {
    count <- agebin[[bin_name]]
    print( paste( bin_name, strrep('*', count ) ) )
}
[1] "<25 *"
[1] "25-49 ***"
[1] "50-74 "
[1] ">74 *"
In [71]:
## 6.18

## simple rule to age people

for(year in 1:50 ) {
    for( row in 1:length(entries) ) {
        entries[[row]][["age"]] <- entries[[row]][["age"]] + 1
    }
}

## reusing code from prior 6.17
        
## age bins
agebin = list()
## this a dummy approach for this
agebin[['<25']] <- 0
agebin[['25-49']] <- 0
agebin[['50-74']] <- 0
agebin[['>74']] <- 0


for( entry in entries ) {
    age <- entry[['age']]
    if( age < 25 ) {
        agebin[['<25']] <- agebin[['<25']] + 1
    }
    if( age >= 25 & age < 50 ) {
        agebin[['25-49']] <- agebin[['25-49']] + 1
    }
    if( age >= 50 & age < 75 ) {
        agebin[['50-74']] <- agebin[['50-74']] + 1
    }
    if( age > 74 ) {
        agebin[['>74']] <- agebin[['>74']] + 1
    }
}

for( bin_name in names( agebin ) ) {
    count <- agebin[[bin_name]]
    print( paste( bin_name, strrep('*', count ) ) )
}
[1] "<25 "
[1] "25-49 "
[1] "50-74 "
[1] ">74 *****"
In [ ]: