Neural Network architecture in ReinforcementLearning.jl in julia - julia

In a VBasedPolicy the neural network approximator tries to learn the V values of states. So it's first layer (input layer) should have same number of neurons as the size of state.
And I believe it's last (output) layer should have a size of 1, since it will produce a single scalar corresponding to input state.
But when I use such a network architecture, I get BoundsError from trajectory.
The details are as follows.
I saw various example experiments on github repo of the library, but all examples on BasicDQN employ QBasedPolicy where last layer of network has size equal to number of actions, which makes sense to me because given a state the network has to output Q values for each action.
I went through the code on github and precisely the error comes on line 79 of this page. But I couldn't solve it.
Code for single agent policy (VBasedPolicy)
STATE_SIZE = length(env.channels) # 2
ACTION_SIZE = length(action_set) # 7
model = Chain(
Dense(STATE_SIZE, 128, relu),
Dense(128, 128, relu),
Dense(128, 128, relu),
Dense(128, 128, relu),
Dense(128, 128, relu),
Dense(128, 128, relu),
Dense(128, 128, relu),
Dense(128, 128, relu),
Dense(128, 128, relu),
Dense(128, 1)
) |> cpu
# optimizer
η = 1f-3 # Learning rate
η_decay = 1f-4
opt = Flux.Optimiser(ADAM(η), InvDecay(η_decay))
function value_action_mapping(env, value_learner; explorer = EpsilonGreedyExplorer(0.4))
A = legal_action_space(env)
println("legal action space: ", A)
V = map(A) do a
value_learner(child(env, a))
end
println("V values: ", V)
c = A[explorer(V)]
println("Chosen action: ", c)
println("Action with max V val: ", findmax(V))
return c
end
single_agent_policy = Agent(
policy = VBasedPolicy(;
learner = BasicDQNLearner(;
approximator = NeuralNetworkApproximator(;
model = model,
optimizer = opt
),
min_replay_history = 50,
batch_size = 50,
γ = 0.99
),
mapping = value_action_mapping
),
trajectory = CircularArraySARTTrajectory(;
capacity = 100,
state=Array{Float64} => (STATE_SIZE)
)
)
Here is the error
BoundsError: attempt to access 1×50 Matrix{Float32} at index [CartesianIndex{2}[CartesianIndex(3, 1), CartesianIndex(1, 2), CartesianIndex(3, 3), CartesianIndex(4, 4), CartesianIndex(5, 5), CartesianIndex(4, 6), CartesianIndex(4, 7), CartesianIndex(3, 8), CartesianIndex(4, 9), CartesianIndex(4, 10) … CartesianIndex(5, 41), CartesianIndex(4, 42), CartesianIndex(3, 43), CartesianIndex(4, 44), CartesianIndex(5, 45), CartesianIndex(5, 46), CartesianIndex(6, 47), CartesianIndex(4, 48), CartesianIndex(4, 49), CartesianIndex(1, 50)]]
Stacktrace:
[1] throw_boundserror(A::Matrix{Float32}, I::Tuple{Vector{CartesianIndex{2}}})
# Base .\abstractarray.jl:651
[2] checkbounds
# .\abstractarray.jl:616 [inlined]
[3] _getindex
# .\multidimensional.jl:831 [inlined]
[4] getindex
# .\abstractarray.jl:1170 [inlined]
[5] adjoint
# C:\Users\vchou\.julia\packages\Zygote\i1R8y\src\lib\array.jl:31 [inlined]
[6] _pullback(__context__::Zygote.Context, 496::typeof(getindex), x::Matrix{Float32}, inds::Vector{CartesianIndex{2}})
# Zygote C:\Users\vchou\.julia\packages\ZygoteRules\OjfTt\src\adjoint.jl:57
[7] _pullback
# C:\Users\vchou\.julia\packages\ReinforcementLearningZoo\M308M\src\algorithms\dqns\basic_dqn.jl:79 [inlined]
[8] _pullback(::Zygote.Context, ::ReinforcementLearningZoo.var"#52#54"{BasicDQNLearner{NeuralNetworkApproximator{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, Flux.Optimise.Optimiser}, typeof(Flux.Losses.huber_loss), Random._GLOBAL_RNG}, Matrix{Float64}, Vector{Bool}, Vector{Float32}, Matrix{Float64}, typeof(Flux.Losses.huber_loss), Float32, NeuralNetworkApproximator{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, Flux.Optimise.Optimiser}})
# Zygote C:\Users\vchou\.julia\packages\Zygote\i1R8y\src\compiler\interface2.jl:0
[9] pullback(f::Function, ps::Zygote.Params)
# Zygote C:\Users\vchou\.julia\packages\Zygote\i1R8y\src\compiler\interface.jl:250
[10] gradient(f::Function, args::Zygote.Params)
# Zygote C:\Users\vchou\.julia\packages\Zygote\i1R8y\src\compiler\interface.jl:58
[11] update!(learner::BasicDQNLearner{NeuralNetworkApproximator{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, Flux.Optimise.Optimiser}, typeof(Flux.Losses.huber_loss), Random._GLOBAL_RNG}, batch::NamedTuple{(:state, :action, :reward, :terminal, :next_state), Tuple{Matrix{Float64}, Vector{Int64}, Vector{Float32}, Vector{Bool}, Matrix{Float64}}})
# ReinforcementLearningZoo C:\Users\vchou\.julia\packages\ReinforcementLearningZoo\M308M\src\algorithms\dqns\basic_dqn.jl:78
[12] update!(learner::BasicDQNLearner{NeuralNetworkApproximator{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, Flux.Optimise.Optimiser}, typeof(Flux.Losses.huber_loss), Random._GLOBAL_RNG}, traj::CircularArraySARTTrajectory{NamedTuple{(:state, :action, :reward, :terminal), Tuple{CircularArrayBuffers.CircularArrayBuffer{Float64, 2}, CircularArrayBuffers.CircularVectorBuffer{Int64}, CircularArrayBuffers.CircularVectorBuffer{Float32}, CircularArrayBuffers.CircularVectorBuffer{Bool}}}})
# ReinforcementLearningZoo C:\Users\vchou\.julia\packages\ReinforcementLearningZoo\M308M\src\algorithms\dqns\basic_dqn.jl:65
[13] update!
# C:\Users\vchou\.julia\packages\ReinforcementLearningCore\NWrFY\src\policies\q_based_policies\learners\abstract_learner.jl:35 [inlined]
[14] update!
# C:\Users\vchou\.julia\packages\ReinforcementLearningCore\NWrFY\src\policies\v_based_policies.jl:31 [inlined]
[15] (::Agent{VBasedPolicy{BasicDQNLearner{NeuralNetworkApproximator{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, Flux.Optimise.Optimiser}, typeof(Flux.Losses.huber_loss), Random._GLOBAL_RNG}, typeof(value_action_mapping)}, CircularArraySARTTrajectory{NamedTuple{(:state, :action, :reward, :terminal), Tuple{CircularArrayBuffers.CircularArrayBuffer{Float64, 2}, CircularArrayBuffers.CircularVectorBuffer{Int64}, CircularArrayBuffers.CircularVectorBuffer{Float32}, CircularArrayBuffers.CircularVectorBuffer{Bool}}}}})(stage::PreActStage, env::AdSpendEnv, action::Int64)
# ReinforcementLearningCore C:\Users\vchou\.julia\packages\ReinforcementLearningCore\NWrFY\src\policies\agents\agent.jl:74
[16] _run(policy::Agent{VBasedPolicy{BasicDQNLearner{NeuralNetworkApproximator{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, Flux.Optimise.Optimiser}, typeof(Flux.Losses.huber_loss), Random._GLOBAL_RNG}, typeof(value_action_mapping)}, CircularArraySARTTrajectory{NamedTuple{(:state, :action, :reward, :terminal), Tuple{CircularArrayBuffers.CircularArrayBuffer{Float64, 2}, CircularArrayBuffers.CircularVectorBuffer{Int64}, CircularArrayBuffers.CircularVectorBuffer{Float32}, CircularArrayBuffers.CircularVectorBuffer{Bool}}}}}, env::AdSpendEnv, stop_condition::StopAfterEpisode{ProgressMeter.Progress}, hook::ComposedHook{Tuple{RewardPerStep, ActionsPerStep, TotalRewardPerEpisode, NeuralOutputPerStep, StatePerStep}})
# ReinforcementLearningCore C:\Users\vchou\.julia\packages\ReinforcementLearningCore\NWrFY\src\core\run.jl:28
[17] run(policy::Agent{VBasedPolicy{BasicDQNLearner{NeuralNetworkApproximator{Chain{Tuple{Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(relu), Matrix{Float32}, Vector{Float32}}, Dense{typeof(identity), Matrix{Float32}, Vector{Float32}}}}, Flux.Optimise.Optimiser}, typeof(Flux.Losses.huber_loss), Random._GLOBAL_RNG}, typeof(value_action_mapping)}, CircularArraySARTTrajectory{NamedTuple{(:state, :action, :reward, :terminal), Tuple{CircularArrayBuffers.CircularArrayBuffer{Float64, 2}, CircularArrayBuffers.CircularVectorBuffer{Int64}, CircularArrayBuffers.CircularVectorBuffer{Float32}, CircularArrayBuffers.CircularVectorBuffer{Bool}}}}}, env::AdSpendEnv, stop_condition::StopAfterEpisode{ProgressMeter.Progress}, hook::ComposedHook{Tuple{RewardPerStep, ActionsPerStep, TotalRewardPerEpisode, NeuralOutputPerStep, StatePerStep}})
# ReinforcementLearningCore C:\Users\vchou\.julia\packages\ReinforcementLearningCore\NWrFY\src\core\run.jl:10
[18] top-level scope
# In[1211]:2
[19] eval
# .\boot.jl:360 [inlined]
[20] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
# Base .\loading.jl:1094

Related

How to apply a function to a rows of a dataset and get the result for each row in R

I wrote a function that uses Monte Carlo Simulation to calculate the value of the call option in R. I want to apply the function to 63 rows of my dataset that contains the real data. In other words, I want the function use the values of each row for its variables
I can put values for variables of the function, but it takes time to do that for a large number of data
# call put option monte carlo
callMC<-function(nSim=10000, tau, r, sigma, S0, K) {
Z <- rnorm(nSim, mean=0, sd=1)
BT <- sqrt(tau) * Z
ST = S0*exp((r - 0.5*sigma^2)*tau + sigma*BT)
# price and standard error of call option
simulated_call_payoffs <- exp(-r*tau)*pmax(ST-K,0)
price_call <- mean(simulated_call_payoffs)
sterr_call <- sd(simulated_call_payoffs)/sqrt(nSim)
output<-list(price_call=price_call, sterr_call=sterr_call)
return(output)
}
set.seed(1)
results<-callMC(n=10000, tau=70/365, r=0.0176,
sigma=0.208, S0=142.76, K=140)
results
tau <- c(1,
2,
3,
4,
5,
8,
9,
10,
12,
15,
15,
16,
17,
18,
19,
22,
24,
25,
26,
29,
30,
31,
32,
33,
36,
37,
38,
39,
40,
43,
44,
45,
46,
47,
50,
51,
52,
53,
54,
57,
58,
59,
60,
61,
64,
65,
66,
67,
68,
71,
72,
73,
74,
75,
78,
79,
80,
81,
82,
85,
86,
87,
88
)/365
r <- c(0.0168, 0.016, 0.0165, 0.0154, 0.0152, 0.0156, 0.0175, 0.0159, 0.0176,
0.0177, 0.0167, 0.0154, 0.0176, 0.0176, 0.0176, 0.0178, 0.018, 0.0177,
0.0179, 0.018, 0.0185, 0.0177, 0.0178, 0.0184, 0.0169, 0.0173, 0.0192, 0.0182, 0.0184, 0.0178, 0.0183, 0.0177, 0.0177, 0.0174, 0.0192, 0.0181, 0.0181, 0.0194, 0.0176, 0.0177, 0.0193, 0.0179, 0.0188, 0.0186, 0.0177, 0.0173, 0.018, 0.0179, 0.0184, 0.019, 0.0183, 0.0177, 0.0172, 0.0185, 0.0192, 0.0189, 0.0189, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0182
)
sigma <- c(0.2564,0.2564,0.2564,0.2564,0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564)
S0<-c(
135.59,
134.56,
134.41,
134.22,
134.13,
134.21,
135.32,
133.76,
133.91,
133.92,
133.22,
131.91,
131.99,
132.12,
132.91,
134.45,
133.77,135.09,
135.97,
134.34,
133.84,
133.2,
134.52,
134.31,
134.4,
134,
134.48,
135.59,
135.47,
137.61,
137.69,
138.78,
137.89,
137.67,
135.53,
133.73,
135.25,
133.82,
135.97,
135.44,
134.07,
134.38,
133.96,
132.58,
134.09,
134.26,
142.11,
143,
142.04,
142.76,
141.13,
139.67,
138.38,
141.28,
142.99,
142.02,
141.69,
143.66,
145.42,
143.24,
143.55,
143.16,
141.68
)
K <-rep(140, each=1, times=63)
df<- data.frame(tau,r,sigma, S0,K)
I use apply(df,1,callMC) to apply the function to each row, However, I got errors
apply(df,1,callMC)
Error in FUN(newX[, i], ...) :
argument "tau" is missing, with no default
Here is a tidyverse + mapply approach:
library(dplyr)
library(tidyr)
df |>
mutate(data = mapply(callMC,tau = tau, r = r, sigma = sigma, S0 = S0, K = K, SIMPLIFY = FALSE)) |>
unnest_wider(col = data)
##> + # A tibble: 63 × 7
##> tau r sigma S0 K price_call sterr_call
##> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##> 1 0.00274 0.0168 0.256 136. 140 0.00629 0.000921
##> 2 0.00548 0.016 0.256 135. 140 0.0181 0.00178
##> 3 0.00822 0.0165 0.256 134. 140 0.0531 0.00345
##> 4 0.0110 0.0154 0.256 134. 140 0.0920 0.00516
##> 5 0.0137 0.0152 0.256 134. 140 0.150 0.00722
##> 6 0.0219 0.0156 0.256 134. 140 0.358 0.0127
##> 7 0.0247 0.0175 0.256 135. 140 0.599 0.0168
##> 8 0.0274 0.0159 0.256 134. 140 0.419 0.0143
##> 9 0.0329 0.0176 0.256 134. 140 0.551 0.0176
##> 10 0.0411 0.0177 0.256 134. 140 0.825 0.0232
##> # … with 53 more rows
The function map in the package {purrr} is very useful for these situations.
The following bit iterates through each row in your df and feeds the values for each of the columns to your function (.x goes from 1 to the number of rows in df one by one. You can assign a value to .x to test that specific row; for example, .x = 1).
1:nrow(df) %>%
map_df(~ callMC(n=.x, tau=df$tau[.x], r=df$r[.x], sigma=df$sigma[.x], S0=df$S0[.x], K=df$K[.x]))
Here the full code:
# call put option monte carlo
callMC<-function(nSim=10000, tau, r, sigma, S0, K) {
Z <- rnorm(nSim, mean=0, sd=1)
BT <- sqrt(tau) * Z
ST = S0*exp((r - 0.5*sigma^2)*tau + sigma*BT)
# price and standard error of call option
simulated_call_payoffs <- exp(-r*tau)*pmax(ST-K,0)
price_call <- mean(simulated_call_payoffs)
sterr_call <- sd(simulated_call_payoffs)/sqrt(nSim)
output<-list(price_call=price_call, sterr_call=sterr_call)
return(output)
}
set.seed(1)
results<-callMC(n=10000, tau=70/365, r=0.0176,
sigma=0.208, S0=142.76, K=140)
results
#> $price_call
#> [1] 6.908401
#>
#> $sterr_call
#> [1] 0.09126226
tau <- c(1,
2,
3,
4,
5,
8,
9,
10,
12,
15,
15,
16,
17,
18,
19,
22,
24,
25,
26,
29,
30,
31,
32,
33,
36,
37,
38,
39,
40,
43,
44,
45,
46,
47,
50,
51,
52,
53,
54,
57,
58,
59,
60,
61,
64,
65,
66,
67,
68,
71,
72,
73,
74,
75,
78,
79,
80,
81,
82,
85,
86,
87,
88
)/365
r <- c(0.0168, 0.016, 0.0165, 0.0154, 0.0152, 0.0156, 0.0175, 0.0159, 0.0176,
0.0177, 0.0167, 0.0154, 0.0176, 0.0176, 0.0176, 0.0178, 0.018, 0.0177,
0.0179, 0.018, 0.0185, 0.0177, 0.0178, 0.0184, 0.0169, 0.0173, 0.0192, 0.0182, 0.0184, 0.0178, 0.0183, 0.0177, 0.0177, 0.0174, 0.0192, 0.0181, 0.0181, 0.0194, 0.0176, 0.0177, 0.0193, 0.0179, 0.0188, 0.0186, 0.0177, 0.0173, 0.018, 0.0179, 0.0184, 0.019, 0.0183, 0.0177, 0.0172, 0.0185, 0.0192, 0.0189, 0.0189, 0.0192, 0.0192, 0.0192, 0.0192, 0.0192, 0.0182
)
sigma <- c(0.2564,0.2564,0.2564,0.2564,0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564,
0.2564)
S0<-c(
135.59,
134.56,
134.41,
134.22,
134.13,
134.21,
135.32,
133.76,
133.91,
133.92,
133.22,
131.91,
131.99,
132.12,
132.91,
134.45,
133.77,135.09,
135.97,
134.34,
133.84,
133.2,
134.52,
134.31,
134.4,
134,
134.48,
135.59,
135.47,
137.61,
137.69,
138.78,
137.89,
137.67,
135.53,
133.73,
135.25,
133.82,
135.97,
135.44,
134.07,
134.38,
133.96,
132.58,
134.09,
134.26,
142.11,
143,
142.04,
142.76,
141.13,
139.67,
138.38,
141.28,
142.99,
142.02,
141.69,
143.66,
145.42,
143.24,
143.55,
143.16,
141.68
)
K <-rep(140, each=1, times=63)
df<- data.frame(tau,r,sigma, S0,K)
library(purrr)
DF = 1:nrow(df) %>%
map_df(~ callMC(n=.x, tau=df$tau[.x], r=df$r[.x], sigma=df$sigma[.x], S0=df$S0[.x], K=df$K[.x]))
DF
#> # A tibble: 63 × 2
#> price_call sterr_call
#> <dbl> <dbl>
#> 1 0 NA
#> 2 0 0
#> 3 0 0
#> 4 0 0
#> 5 0 0
#> 6 0.660 0.660
#> 7 0 0
#> 8 0.386 0.386
#> 9 0 0
#> 10 0 0
#> # … with 53 more rows
Created on 2022-03-22 by the reprex package (v2.0.1)
The problem is that you are passing a vector (of your 5 parameters) to your function which is expecting 5 separate parameters - 6 if you include n. You need to let your function know it's receiving 6 separate parameters
Here is one simple way to get a neat answer in base R. I've "hard coded" the first parameter since you seem to be using 10000, but you can make that a variable if required
the x[1], x[2]... are the separate elements of each row of your data frame df
then I've used do.call, rbind & as.data.frame to make the results into a nice data frame
as.data.frame(do.call(rbind, apply(df, 1, function(x) callMC(10000, x[1], x[2], x[3], x[4], x[5]))))
price_call sterr_call
1 0.006132845 0.0008339928
2 0.01769498 0.001826786
3 0.06270669 0.003937882
4 0.08954985 0.005085325
5 0.1461733 0.007129216
6 0.3558649 0.01246984
7 0.6200213 0.01750186
...
edit : alternate answer using the variables you defined in df. Again, as.data.frame and t are just to tidy up the results
as.data.frame(t(mapply(callMC, 10000, tau, r, sigma, S0, K)))
price_call sterr_call
1 0.006855707 0.0009683526
2 0.01869136 0.001834313
3 0.04815097 0.003332207
4 0.08621679 0.004994365
5 0.1532411 0.007166206
...

Set different colour for each geom line with ggplot

I have this data
diffdec <- structure(list(ma = structure(c(14975, 14975, 15006, 15006, 15034, 5034, 15065, 15065, 15095, 15095, 15126, 15126, 15156, 15156, 15187, 15187, 15218, 15218, 15248, 15248, 15279, 15279, 15309, 15309, 15340, 15340, 15371, 15371, 15400, 15400, 15431, 15431, 15461, 15461, 15492, 15492, 15522, 15522, 15553, 15553, 15584, 15584, 15614, 15614, 15645, 15645, 15675, 15675, 15706, 15706, 15737, 15737, 15765, 15765, 15796, 15796, 15826, 15826, 15857, 15857, 15887, 15887, 15918, 15918, 15949, 15949, 15979, 15979, 16010, 16010, 16040, 16040, 16071, 16071, 16102, 16102, 16130, 16130, 16161, 16161, 16191, 16191, 16222, 16222, 16252, 16252, 16283, 16283, 16314, 16314, 16344, 16344, 16375, 16375, 16405, 16405, 16436, 16436, 16467, 16467, 16495, 16495, 16526, 16526, 16556, 16556, 16587, 16587, 16617, 16617, 16648, 16648, 16679, 16679, 16709, 16709, 16740, 16740, 16770, 16770, 16801, 16801, 16832, 16832, 16861, 16861, 16892, 16892, 16922, 16922, 16953, 16953, 16983, 16983, 17014, 17014, 17045, 17045, 17075, 17075, 17106, 17106, 17136, 17136, 17167, 17167, 17198, 17198, 17226, 17226, 17257, 17257, 17287, 17287, 17318, 17318, 17348, 17348, 17379, 17379, 17410, 17410, 17440, 17440, 17471, 17471, 17501, 17501, 17532, 17532, 17563, 17563, 17591, 17591, 17622, 17622, 17652, 17652, 17683, 17683, 17713, 17713, 17744, 17744, 17775, 17775, 17805, 17805, 17836, 17836, 17866, 17866, 17897, 17897, 17928, 17928, 17956, 17956, 17987, 17987, 18017, 18017, 18048, 18048, 18078, 18078, 18109, 18109, 18140, 18140, 18170, 18170, 18201, 18201, 18231, 18231, 18262, 18262, 18293, 18293, 18322, 18322, 18353, 18353, 18383, 18383, 18414, 18414, 18444, 18444, 18475, 18475, 18506, 18506, 18536, 18536, 18567, 18567, 18597, 18597, 18628, 18628, 18659, 18659, 18687, 18687, 18718, 18718, 18748, 18748, 18779, 18779, 18809, 18809, 18840, 18840, 18871, 18871, 18901, 18901, 18932, 18932, 18962, 18962), class = "Date"), NOME_REGIONE = c("Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte" ), CLASSE = c("over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80", "over80", "under80"), decessi = c(5635, 3755, 5087, 3391, 5396, 3541, 5018, 3442, 4707, 3216, 4464, 3153, 4804, 3238, 4789, 3123, 4358, 2991, 4930, 3276, 5096, 3352, 5384, 3660, 5922, 3842, 5487, 3429, 5585, 3609, 5266, 3382, 4969, 3124, 4875, 2993, 5022, 3178, 4878, 3093, 4628, 2962, 5065, 3262, 5499, 3331, 5602, 3603, 5939, 3729, 5265, 3326, 5734, 3554, 5178, 3186, 4819, 3084, 4718, 2887, 4992, 2998, 4838, 3094, 4636, 2882, 5255, 3290, 5304, 3248, 5682, 3434, 5892, 3586, 5320, 3172, 5698, 3408, 5111, 3223, 4878, 2996, 4737, 2900, 5029, 2966, 4897, 2879, 4718, 2825, 5101, 3135, 5437, 3264, 5632, 3410, 6654, 3841, 5914, 3267, 6353, 3589, 5769, 3326, 5468, 3156, 5279, 2922, 5577, 3168, 5476, 3089, 5168, 2918, 5744, 3236, 5977, 3441, 6289, 3493, 6384, 3570, 5509, 3099, 5890, 3412, 5622, 3066, 5185, 2997, 5002, 2831, 5278, 3053, 5155, 2934, 4881, 2737, 5400, 3124, 5721, 3092, 6245, 3481, 6877, 3745, 5968, 3284, 6312, 3545, 5871, 3118, 5423, 3051, 5450, 2953, 5619, 3046, 5585, 3002, 5174, 2790, 5846, 3203, 5987, 3254, 6438, 3483, 6765, 3623, 5874, 3176, 6434, 3488, 6017, 3126, 5507, 3019, 5398, 2871, 5709, 2977, 5599, 2879, 5199, 2880, 5804, 2986, 6028, 3137, 6406, 3362, 6937, 3442, 5953, 2946, 6454, 3175, 5962, 3036, 5576, 2901, 5439, 2752, 5701, 2881, 5549, 2820, 5299, 2677, 5765, 3050, 5929, 3156, 6632, 3382, 8694, 4100, 7472, 3585, 8246, 3792, 7368, 3605, 7005, 3340, 6827, 3299, 7108, 3420, 7115, 3439, 6680, 3150, 7265, 3494, 7695, 3531, 8055, 3823, 7282, 3711, 6414, 3333, 6686, 3530, 6170, 3237, 5791, 3080, 5641, 3031, 5958, 2995, 5910, 2979, 5473, 2850, 6186, 3298, 6421, 3198, 6630, 3562), deccov = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 702, 579, 1435, 710, 418, 156, 215, 64, 50, 10, 7, 4, 13, 5, 233, 85, 1283, 707, 920, 456, 560, 230, 416, 162, 511, 368, 313, 323, 87, 111, 11, 23, 2, 1, 11, 7, 26, 20, 31, 14, 43, 25, 37, 31), diff = c(5635, 3755, 5087, 3391, 5396, 3541, 5018, 3442, 4707, 3216, 4464, 3153, 4804, 3238, 4789, 3123, 4358, 2991, 4930, 3276, 5096, 3352, 5384, 3660, 5922, 3842, 5487, 3429, 5585, 3609, 5266, 3382, 4969, 3124, 4875, 2993, 5022, 3178, 4878, 3093, 4628, 2962, 5065, 3262, 5499, 3331, 5602, 3603, 5939, 3729, 5265, 3326, 5734, 3554, 5178, 3186, 4819, 3084, 4718, 2887, 4992, 2998, 4838, 3094, 4636, 2882, 5255, 3290, 5304, 3248, 5682, 3434, 5892, 3586, 5320, 3172, 5698, 3408, 5111, 3223, 4878, 2996, 4737, 2900, 5029, 2966, 4897, 2879, 4718, 2825, 5101, 3135, 5437, 3264, 5632, 3410, 6654, 3841, 5914, 3267, 6353, 3589, 5769, 3326, 5468, 3156, 5279, 2922, 5577, 3168, 5476, 3089, 5168, 2918, 5744, 3236, 5977, 3441, 6289, 3493, 6384, 3570, 5509, 3099, 5890, 3412, 5622, 3066, 5185, 2997, 5002, 2831, 5278, 3053, 5155, 2934, 4881, 2737, 5400, 3124, 5721, 3092, 6245, 3481, 6877, 3745, 5968, 3284, 6312, 3545, 5871, 3118, 5423, 3051, 5450, 2953, 5619, 3046, 5585, 3002, 5174, 2790, 5846, 3203, 5987, 3254, 6438, 3483, 6765, 3623, 5874, 3176, 6434, 3488, 6017, 3126, 5507, 3019, 5398, 2871, 5709, 2977, 5599, 2879, 5199, 2880, 5804, 2986, 6028, 3137, 6406, 3362, 6937, 3442, 5953, 2946, 6454, 3175, 5962, 3036, 5576, 2901, 5439, 2752, 5701, 2881, 5549, 2820, 5299, 2677, 5765, 3050, 5929, 3156, 6632, 3382, 8694, 4100, 7470, 3584, 7544, 3213, 5933, 2895, 6587, 3184, 6612, 3235, 7058, 3410, 7108, 3435, 6667, 3145, 7032, 3409, 6412, 2824, 7135, 3367, 6722, 3481, 5998, 3171, 6175, 3162, 5857, 2914, 5704, 2969, 5630, 3008, 5956, 2994, 5899, 2972, 5447, 2830, 6155, 3284, 6378, 3173, 6593, 3531)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -264L), groups = structure(list( ma = structure(c(14975, 15006, 15034, 15065, 15095, 15126, 15156, 15187, 15218, 15248, 15279, 15309, 15340, 15371, 15400, 15431, 15461, 15492, 15522, 15553, 15584, 15614, 15645, 15675, 15706, 15737, 15765, 15796, 15826, 15857, 15887, 15918, 15949, 15979, 16010, 16040, 16071, 16102, 16130, 16161, 16191, 16222, 16252, 16283, 16314, 16344, 16375, 16405, 16436, 16467, 16495, 16526, 16556, 16587, 16617, 16648, 16679, 16709, 16740, 16770, 16801, 16832, 16861, 16892, 16922, 16953, 16983, 17014, 17045, 17075, 17106, 17136, 17167, 17198, 17226, 17257, 17287, 17318, 17348, 17379, 17410, 17440, 17471, 17501, 17532, 17563, 17591, 17622, 17652, 17683, 17713, 17744, 17775, 17805, 17836, 17866, 17897, 17928, 17956, 17987, 18017, 18048, 18078, 18109, 18140, 18170, 18201, 18231, 18262, 18293, 18322, 18353, 18383, 18414, 18444, 18475, 18506, 18536, 18567, 18597, 18628, 18659, 18687, 18718, 18748, 18779, 18809, 18840, 18871, 18901, 18932, 18962 ), class = "Date"), NOME_REGIONE = c("Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte", "Piemonte" ), .rows = structure(list(1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18, 19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34, 35:36, 37:38, 39:40, 41:42, 43:44, 45:46, 47:48, 49:50, 51:52, 53:54, 55:56, 57:58, 59:60, 61:62, 63:64, 65:66, 67:68, 69:70, 71:72, 73:74, 75:76, 77:78, 79:80, 81:82, 83:84, 85:86, 87:88, 89:90, 91:92, 93:94, 95:96, 97:98, 99:100, 101:102, 103:104, 105:106, 107:108, 109:110, 111:112, 113:114, 115:116, 117:118, 119:120, 121:122, 123:124, 125:126, 127:128, 129:130, 131:132, 133:134, 135:136, 137:138, 139:140, 141:142, 143:144, 145:146, 147:148, 149:150, 151:152, 153:154, 155:156, 157:158, 159:160, 161:162, 163:164, 165:166, 167:168, 169:170, 171:172, 173:174, 175:176, 177:178, 179:180, 181:182, 183:184, 185:186, 187:188, 189:190, 191:192, 193:194, 195:196, 197:198, 199:200, 201:202, 203:204, 205:206, 207:208, 209:210, 211:212, 213:214, 215:216, 217:218, 219:220, 221:222, 223:224, 225:226, 227:228, 229:230, 231:232, 233:234, 235:236, 237:238, 239:240, 241:242, 243:244, 245:246, 247:248, 249:250, 251:252, 253:254, 255:256, 257:258, 259:260, 261:262, 263:264), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame" ), row.names = c(NA, -132L), .drop = TRUE))
and used this code to plot them
ggplot(data=diffdec,aes(x=ma,y=decessi,col=CLASSE))+
geom_line(size=1,lty=2)+
geom_line(data=diffdec,aes(x=ma,y=diff,col=CLASSE),size=2)+
geom_line(aes(x=ma,y=deccov, col=CLASSE))+
xlim(c(my("0111"),my("1221")))+
theme(legend.position="bottom",
axis.text = element_text(size = 15),
axis.title=element_text(size = 20),
strip.text = element_text(size = 16),
plot.caption = element_text(size = 20, face = "italic"),
legend.title = element_text(size = 0),
legend.text = element_text(size = 15))+
ylab("Decessi")+
xlab("Data")
I'd like to show both the dashed lines in a different colour. Is there any way using the above code, or I need to use a different approach?
OP clarified that they were looking to change the dotted lines to have the same color, but be different than their respective groups. In other words, the dashed line for "over80" and for "under80" should both be a different color. OP did not indicate if the dashed lines should be included in the legend as an entry, but I am assuming that the answer is "yes".
Here's a descriptive example, and OP can extend to their own question, since we don't have any data to work from.
library(ggplot2)
df <- iris[which(iris$Species!="versicolor"),]
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width), lty=2)
Since color= is specified globally for the plot via ggplot(aes(...)), every geom will by default have the color aesthetic mapped as if col=Species. To override that default, you can refer to the aesthetic inside a particular geom. In this case, we can color the lines by changing the call to the geom_line(). When col is specified there, it overwrites the original mapping.
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width), col="blue", lty=2) +
theme_classic()
To get the line color to appear in the legend, you will need to add the col= aesthetic back inside aes(), except this time you will want to map to a simple character value (the name of our line). Doing this effectively "maps" all observations to a new variable where every value is the same. This means all observations are colored the same (what we want), and a legend entry is created.
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width, col="Petals"), lty=2) +
labs(y="Width") + theme_classic()
Now the line is correct and legend entry is correct, but we didn't specify a color and now the legend entry for "Petals" is a solid line, whereas the line is a dashed line. One of the best options to fix this is likely to map the linetype aesthetic in the same manner as col and then use scale_*_manual() functions to explicitly define the colors and linetypes:
ggplot(df, aes(Sepal.Length, y=Sepal.Width, col=Species, lty=Species)) +
geom_line() +
geom_line(aes(y=Petal.Width, col="Petals", lty="Petals")) +
scale_color_manual(
values = c("setosa" = "red", "virginica" = "blue", "Petals" = "green3")) +
scale_linetype_manual(
values = c("setosa" = 1, "virginica" = 1, "Petals" = 2)) +
labs(y="Width") + theme_classic()

Date containing weekday/Month/Day/Year can't be correctly parsed by Lubridate

The problem
I am downloaded a database from a website and the Daye column is in the following format:
x <- c("Fri, Mar 1, 2019", "Sat, Mar 2, 2019", "Sun, Mar 3, 2019", "Mon, Mar 4, 2019", "Tue, Mar 5, 2019", "Wed, Mar 6, 2019", "Thu, Mar 7, 2019", "Fri, Mar 8, 2019", "Sat, Mar 9, 2019", "Sun, Mar 10, 2019", "Mon, Mar 11, 2019", "Tue, Mar 12, 2019", "Wed, Mar 13, 2019", "Thu, Mar 14, 2019", "Fri, Mar 15, 2019", "Sat, Mar 16, 2019", "Sun, Mar 17, 2019", "Mon, Mar 18, 2019", "Tue, Mar 19, 2019", "Wed, Mar 20, 2019", "Thu, Mar 21, 2019", "Fri, Mar 22, 2019", "Sat, Mar 23, 2019", "Sun, Mar 24, 2019", "Mon, Mar 25, 2019", "Tue, Mar 26, 2019", "Wed, Mar 27, 2019", "Thu, Mar 28, 2019", "Fri, Mar 29, 2019", "Sat, Mar 30, 2019", "Sun, Mar 31, 2019")
Which contains the dates from March 1st to the 31st. I am trying to transform it to a Date format, so I used y ,dy function in lubridate:
library("lubridate")
mdy(x)
Which resulted in the following vector:
[1] "2019-03-01" "2019-03-02" "2019-03-20" "2019-04-20" "2019-05-20" "2019-03-06"
[7] "2019-03-07" "2019-03-08" "2019-03-09" "2019-10-20" "2019-11-20" "2019-12-20"
[13] "2019-03-13" "2019-03-14" "2019-03-15" "2019-03-16" "2019-03-17" "2019-03-18"
[19] "2019-03-19" "2019-03-20" "2019-03-21" "2019-03-22" "2019-03-23" "2019-03-24"
[25] "2019-03-25" "2019-03-26" "2019-03-27" "2019-03-28" "2019-03-29" "2019-03-30"
[31] "2019-03-31"
As you can see most of the dates are correct, but it does not work for the 4th, 5th, 10th, 11th and 12th day of the month, where it reads the day as if it was the month. I have been trying several solutions but none has worked so far
Some possible solutions that have not worked
Using regex to remove weekday from the character vector:
I thought one way of solving this was removing the weekday part of the string, so I tried to remove everything before the comma, which I was able to do imperfectly:
library(stringr)
y <- str_extract(Dt,",.*$")
y
[1] ", Mar 1, 2019" ", Mar 2, 2019" ", Mar 3, 2019" ", Mar 4, 2019"
[5] ", Mar 5, 2019" ", Mar 6, 2019" ", Mar 7, 2019" ", Mar 8, 2019"
[9] ", Mar 9, 2019" ", Mar 10, 2019" ", Mar 11, 2019" ", Mar 12, 2019"
[13] ", Mar 13, 2019" ", Mar 14, 2019" ", Mar 15, 2019" ", Mar 16, 2019"
[17] ", Mar 17, 2019" ", Mar 18, 2019" ", Mar 19, 2019" ", Mar 20, 2019"
[21] ", Mar 21, 2019" ", Mar 22, 2019" ", Mar 23, 2019" ", Mar 24, 2019"
[25] ", Mar 25, 2019" ", Mar 26, 2019" ", Mar 27, 2019" ", Mar 28, 2019"
[29] ", Mar 29, 2019" ", Mar 30, 2019" ", Mar 31, 2019"
But now when I use mdy I get all the first 12 days wrong.
mdy(y)
[1] "2019-01-20" "2019-02-20" "2019-03-20" "2019-04-20" "2019-05-20" "2019-06-20"
[7] "2019-07-20" "2019-08-20" "2019-09-20" "2019-10-20" "2019-11-20" "2019-12-20"
[13] "2019-03-13" "2019-03-14" "2019-03-15" "2019-03-16" "2019-03-17" "2019-03-18"
[19] "2019-03-19" "2019-03-20" "2019-03-21" "2019-03-22" "2019-03-23" "2019-03-24"
[25] "2019-03-25" "2019-03-26" "2019-03-27" "2019-03-28" "2019-03-29" "2019-03-30"
[31] "2019-03-31"
Any ideas on how to solve this?
SessionInfo
I added SessionInfo as requested
R version 3.4.4 (2018-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 16.04.5 LTS
Matrix products: default
BLAS: /usr/lib/libblas/libblas.so.3.6.0
LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=es_CL.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=es_CL.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=es_CL.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=es_CL.UTF-8 LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] stringr_1.3.1 dplyr_0.7.6 rvest_0.3.2 xml2_1.2.0 XML_3.98-1.16
[6] lubridate_1.7.4
loaded via a namespace (and not attached):
[1] Rcpp_0.12.18 rstudioapi_0.7 knitr_1.20 bindr_0.1.1
[5] magrittr_1.5 tidyselect_0.2.4 R6_2.2.2 rlang_0.2.2
[9] httr_1.3.1 tools_3.4.4 pacman_0.4.6 selectr_0.4-1
[13] htmltools_0.3.6 yaml_2.2.0 rprojroot_1.3-2 digest_0.6.17
[17] assertthat_0.2.0 tibble_1.4.2 crayon_1.3.4 bindrcpp_0.2.2
[21] purrr_0.2.5 curl_3.2 glue_1.3.0 evaluate_0.11
[25] rmarkdown_1.10 stringi_1.2.4 pillar_1.3.0 compiler_3.4.4
[29] backports_1.1.2 pkgconfig_2.0.2
just as #duckmayr thought it was a locale promblem, as shown above in my sessioninfo my locale was set up as follows:
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
[3] LC_TIME=es_CL.UTF-8 LC_COLLATE=en_US.UTF-8
[5] LC_MONETARY=es_CL.UTF-8 LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=es_CL.UTF-8 LC_NAME=C
[9] LC_ADDRESS=C LC_TELEPHONE=C
[11] LC_MEASUREMENT=es_CL.UTF-8 LC_IDENTIFICATION=C
When I changed the LC_TIME to en_US.UTF-8 everything was fixed, when I did it:
Sys.setlocale("LC_TIME", 'en_US.UTF-8')
Then using mdy worked just fine. Hope this helps someone with a similar problem in the future

Convert fractional year and day of year to date in R

Is it possible to convert a fractional year and day of year to an actual date format in R?
For example, in the time column in my example data below, 1900.00 corresponds to January of 1900, 1900.08 corresponds to February. dayofyr corresponds to the year day.
myData <- structure(list(time = c(1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900, 1900,
1900, 1900, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08, 1900.08,
1900.08, 1900.08, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.17,
1900.17, 1900.17, 1900.17, 1900.17, 1900.17, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25, 1900.25,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33, 1900.33,
1900.33, 1900.33, 1900.33, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.42,
1900.42, 1900.42, 1900.42, 1900.42, 1900.42, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.5,
1900.5, 1900.5, 1900.5, 1900.5, 1900.5, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58, 1900.58,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67, 1900.67,
1900.67, 1900.67, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.75,
1900.75, 1900.75, 1900.75, 1900.75, 1900.75, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83, 1900.83,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92, 1900.92,
1900.92, 1900.92, 1900.92), dayofyr = 1:366), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -366L), .Names = c("time",
"dayofyr"))
Use floor to remove the decimal part, convert to a character string appending "-01-01", convert that to "Date" class and add the number of days minus 1. No packages are used.
transform(myData, date = as.Date(paste0(floor(time), "-01-01")) + dayofyr - 1)

extracting missing elements after comparing two data frames in R

So I have a master list of 96 elements.
masterlist <- list(c(paste0(rep(LETTERS[1:8], each=12), rep(1:12, 8))))
And I have a list of 26 data frames, where each data frames contains any or all of the elements from the master list. I called these the rowcol.list. From here, I used the following code to see which of the data frames in my list of 26 data frames are different from the master list.
rowcol.list[!(rowcol.list%in%masterlist)]
The above code returned 10 data frames from the list of 26 data frames, as shown below:
$p01_control.txt
[1] "A1" "A2" "A3" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9"
[21] "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4" "D5"
[41] "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12" "F1"
[61] "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p03_control.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12"
[61] "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p04_pq.txt
[1] "A1" "A2" "A3" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9"
[21] "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4" "D5"
[41] "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12" "F1"
[61] "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p07_docetaxel.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12"
[61] "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p08_docetaxel_b.txt
[1] "E10" "E11" "E12" "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5"
[21] "G6" "G7" "G8" "G9" "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p08_docetaxel.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8"
$p12_dactinomycin.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "F1"
[61] "F2" "F3" "F4" "F5" "F6" "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p16_cisplatin.txt
[1] "A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8"
[21] "B9" "B10" "B11" "B12" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4"
[41] "D5" "D6" "D7" "D8" "D9" "D10" "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12"
[61] "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9"
[81] "G10" "G11" "G12" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p18_cisplatin_b.txt
[1] "A7" "A8" "A9" "A10" "A11" "A12" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "B9" "B10" "B11" "B12" "C1" "C2"
[21] "C3" "C4" "C5" "C6" "C7" "C8" "C9" "C10" "C11" "C12" "D1" "D2" "D3" "D4" "D5" "D6" "D7" "D8" "D9" "D10"
[41] "D11" "D12" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "E9" "E10" "E11" "E12" "F1" "F2" "F3" "F4" "F5" "F6"
[61] "F7" "F8" "F9" "F10" "F11" "F12" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "G9" "G10" "G11" "G12" "H1" "H2"
[81] "H3" "H4" "H5" "H6" "H7" "H8" "H9" "H10" "H11" "H12"
$p18_cisplatin.txt
[1] "A1" "A2" "A3" "A4" "A5"
Instead of seeing the elements that each of those data frames above have because they are different from the master list, I want to see which elements the data frames do NOT have when they are compared to the master list. As a short example, the first data frame that's different, $p01_control.txt should show "A4" because that's what it is missing. (I only know that it's missing "A4" because I went through the list and found which one was missing).
Please let me know how I can make the problem more clear if it's confusing.
Thank you!
I would use setdiff. you can try something like this:
lapply(rowcol.list,function(x) setdiff(masterlist[[1]],x))

Resources