Bug when training a custom model using Flux - julia

I am pretty new to Julia and Flux. I am trying to build a simple neural network but using an attention layer. I wrote the code as follows, which works fine in the inference(feed-forward) mode:
using Flux
struct Attention
W
v
end
Attention(vehile_embedding_dim::Integer) = Attention(
Dense(vehile_embedding_dim => vehile_embedding_dim, tanh),
Dense(vehile_embedding_dim, 1, bias=false, init=Flux.zeros32)
)
function (a::Attention)(inputs)
alphas = [a.v(e) for e in a.W.(inputs)]
alphas = sigmoid.(alphas)
output = sum([alpha.*input for (alpha, input) in zip(alphas, inputs)])
return output
end
Flux.#functor Attention
struct AttentionNet
embedding
attention
fc_output
vehicle_num::Integer
vehicle_dim::Integer
end
AttentionNet(vehicle_num::Integer, vehicle_dim::Integer, embedding_dim::Integer) = AttentionNet(
Dense(vehicle_dim+1 => embedding_dim, relu),
Attention(embedding_dim),
Dense(1+embedding_dim => 1),
vehicle_num,
vehicle_dim
)
function (a_net::AttentionNet)(x)
time_idx = x[[1], :]
vehicle_states = [x[2+a_net.vehicle_dim*(i-1):2+a_net.vehicle_dim*i-1, :] for i in 1:a_net.vehicle_num]
vehicle_states = [vcat(time_idx, vehicle_state) for vehicle_state in vehicle_states]
vehicle_embeddings = a_net.embedding.(vehicle_states)
attention_output = a_net.attention(vehicle_embeddings)
x = a_net.fc_output(vcat(time_idx, attention_output))
return x
end
Flux.#functor AttentionNet
Flux.trainable(a_net::AttentionNet) = (a_net.embedding, a_net.attention, a_net.fc_output,)
fake_inputs = rand(22, 640)
fake_outputs = rand(1, 640)
a_net = AttentionNet(3, 7, 64)|> gpu
opt = Adam(.01)
opt_state = Flux.setup(opt, a_net)
data = Flux.DataLoader((fake_inputs, fake_outputs)|>gpu, batchsize=32, shuffle=true)
Flux.train!(a_net, data, opt_state) do m, x, y
Flux.mse(m(x), y)
end
But when I trained it, I got the following error message and a warning:
┌ Warning: trainable(x) should now return a NamedTuple with the field names, not a Tuple
└ # Optimisers C:\Users\Herr LU\.julia\packages\Optimisers\SoKJO\src\interface.jl:164
ERROR: MethodError: no method matching +(::Base.RefValue{Any}, ::NamedTuple{(:contents,), Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}}})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:591
+(::Union{InitialValues.NonspecificInitialValue, InitialValues.SpecificInitialValue{typeof(+)}}, ::Any) at C:\Users\Herr LU\.julia\packages\InitialValues\OWP8V\src\InitialValues.jl:154
+(::ChainRulesCore.Tangent{P}, ::P) where P at C:\Users\Herr LU\.julia\packages\ChainRulesCore\C73ay\src\tangent_arithmetic.jl:146
...
Stacktrace:
[1] accum(x::Base.RefValue{Any}, y::NamedTuple{(:contents,), Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}}})
# Zygote C:\Users\Herr LU\.julia\packages\Zygote\SmJK6\src\lib\lib.jl:17
[2] accum(x::Base.RefValue{Any}, y::NamedTuple{(:contents,), Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}}}, zs::Base.RefValue{Any}) (repeats 2 times)
# Zygote C:\Users\Herr LU\.julia\packages\Zygote\SmJK6\src\lib\lib.jl:22
[3] Pullback
# e:\Master Thesis\lu_jizhou\toy exmaple\dqn_model.jl:39 [inlined]
[4] (::typeof(∂(λ)))(Δ::CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer})
# Zygote C:\Users\Herr LU\.julia\packages\Zygote\SmJK6\src\compiler\interface2.jl:0
[5] Pullback
# e:\Master Thesis\lu_jizhou\toy exmaple\dqn_model.jl:62 [inlined]
[6] #208
# C:\Users\Herr LU\.julia\packages\Zygote\SmJK6\src\lib\lib.jl:206 [inlined]
[7] #2066#back
# C:\Users\Herr LU\.julia\packages\ZygoteRules\AIbCs\src\adjoint.jl:67 [inlined]
[8] Pullback
# C:\Users\Herr LU\.julia\packages\Flux\ZdbJr\src\train.jl:102 [inlined]
[9] (::typeof(∂(λ)))(Δ::Float32)
# Zygote C:\Users\Herr LU\.julia\packages\Zygote\SmJK6\src\compiler\interface2.jl:0
[10] (::Zygote.var"#60#61"{typeof(∂(λ))})(Δ::Float32)
# Zygote C:\Users\Herr LU\.julia\packages\Zygote\SmJK6\src\compiler\interface.jl:45
[11] withgradient(f::Function, args::AttentionNet)
# Zygote C:\Users\Herr LU\.julia\packages\Zygote\SmJK6\src\compiler\interface.jl:133
[12] macro expansion
# C:\Users\Herr LU\.julia\packages\Flux\ZdbJr\src\train.jl:102 [inlined]
[13] macro expansion
# C:\Users\Herr LU\.julia\packages\ProgressLogging\6KXlp\src\ProgressLogging.jl:328 [inlined]
[14] train!(loss::Function, model::AttentionNet, data::MLUtils.DataLoader{Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}}, Random._GLOBAL_RNG, Val{nothing}}, opt::Named
Tuple{(:embedding, :attention, :fc_output, :vehicle_num, :vehicle_dim), Tuple{NamedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArr
ay{Float32, 2, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{Float64
, Float64}}}, Tuple{}}}, NamedTuple{(:W, :v), Tuple{NamedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.De
viceBuffer}, Tuple{Float64, Float64}}}, Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Tuple{}}}, N
amedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Tuple{}, Tupl
e{}}}}}, NamedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Opt
imisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Tuple{}}}, Tuple{}, Tuple{}}}; cb::Nothing)
# Flux.Train C:\Users\Herr LU\.julia\packages\Flux\ZdbJr\src\train.jl:100
[15] train!(loss::Function, model::AttentionNet, data::MLUtils.DataLoader{Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}}, Random._GLOBAL_RNG, Val{nothing}}, opt::Named
Tuple{(:embedding, :attention, :fc_output, :vehicle_num, :vehicle_dim), Tuple{NamedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArr
ay{Float32, 2, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{Float64
, Float64}}}, Tuple{}}}, NamedTuple{(:W, :v), Tuple{NamedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.De
viceBuffer}, Tuple{Float64, Float64}}}, Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Tuple{}}}, N
amedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Tuple{}, Tupl
e{}}}}}, NamedTuple{(:weight, :bias, :σ), Tuple{Optimisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 2, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Opt
imisers.Leaf{Optimisers.Adam{Float64}, Tuple{CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, CUDA.CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}, Tuple{Float64, Float64}}}, Tuple{}}}, Tuple{}, Tuple{}}})
# Flux.Train C:\Users\Herr LU\.julia\packages\Flux\ZdbJr\src\train.jl:97
[16] top-level scope
# e:\Master Thesis\lu_jizhou\toy exmaple\dqn_model.jl:61
I followed the instruction from the official tutorial on custom layers, but it doesn’t specify how to get custom layers properly trained. Could someone help me out?

For anyone who is interested, this problem is well solved by #ToucheSir on this GitHub thread.

Related

Frequency table of a categorical variable based on a different variable value- R

I have a dataframe with two categorical variables. Column 1 is variable 1 and column 2 is variable 2. I want to create a frequency table with the number of times Var1 status is 1, 2 and 3 when Var2 status is 1. Similarly when Var2 status is 2 & 3, I want the frequency of Var1 status- 1, 2 and 3. At the end I want to plot a histogram with Var2 status (1,2,3) on x-axis and on y-axis a frequency of Var1 statuses for each of Var2 status. Thanks for the help.
structure(list(`1` = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1), `2` = c(3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 3, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2)), row.names = c(NA, -101L), class = c("tbl_df", "tbl",
"data.frame"))
You are probably looking to plot barplot of frequency instead of histogram in your description.
library(tidyr)
# add column names for the dataframe example
names(df) <- paste0("Var", 1:2)
# group and summarise to find the number of occurrence for each paired Var2, Var1 combination
plotting_df <- df %>% group_by(Var2, Var1) %>% summarise(n=n())
# plot using the new summary data frame
ggplot(plotting_df, aes(x=factor(Var2), y=n, fill=factor(Var1))) +
geom_col(position="dodge")

Julia CUDA: LoadError: GPU broadcast resulted in non-concrete element type Any

I have to analyze some data using maximum likelihood methods, but CUDA doesn't like how I handle type instability. Any idea on how I could fix this?
I tried my best in forcing concrete return types by declaring the type of every function argument, but it doesn't seem to work.
EDIT: I moved back some function declarations inside where they belonged.
Here is an extract of the problematic part of the program:
function ln_likelihood( a_c::Float64,
a_p::Float64,
θ_1::Float64,
θ_2p::CuArray{Float64},
θ_2c::CuArray{Float64},
ϵ_p::CuArray{Float64},
σ_p::CuArray{Float64},
ϵ_c::CuArray{Float64},
σ_c::CuArray{Float64})
...
#return Float64
end
function trova_max_likelihood( θ_1::Float64,
θ_2p::CuArray{Float64},
θ_2c::CuArray{Float64},
ϵ_p::CuArray{Float64},
σ_p::CuArray{Float64},
ϵ_c::CuArray{Float64},
σ_c::CuArray{Float64})
...
function funzione_likelirobin(a_c::Float64, a_p::Float64)
global θ_1,θ_2p,θ_2c, ϵ_p, σ_p, ϵ_c, σ_c
ln_likelihood(a_c,a_p,θ_1,θ_2p,θ_2c, ϵ_p, σ_p, ϵ_c, σ_c)
end
funzione_likelihood(x::Tuple{Float64, Float64}) = funzione_likelirobin(x[1],x[2])
#code_warntype funzione_likelihood.(range)
#Where range::CuArray{Tuple{Float64,Float64}}
...
end
trova_max_likelihood(gθ_1, gθ_2p, gθ_2c, gϵ_p, gσ_p, gϵ_c, gσ_c)
And the output I get:
Variables
#self#::Core.Const(var"##dotfunction#274#175"{var"#funzione_likelihood#174"{var"#funzione_likelirobin#173"}}(var"#funzione_likelihood#174"{var"#funzione_likelirobin#173"}(var"#funzione_likelirobin#173"())))
x1::CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}
Body::Union{}
1 ─ %1 = Core.getfield(#self#, :funzione_likelihood)::Core.Const(var"#funzione_likelihood#174"{var"#funzione_likelirobin#173"}(var"#funzione_likelirobin#173"()))
│ %2 = Base.broadcasted(%1, x1)::Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{1}, Nothing, var"#funzione_likelihood#174"{var"#funzione_likelirobin#173"}, Tuple{CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}}}
│ Base.materialize(%2)
└── Core.Const(:(return %3))
ERROR: LoadError: GPU broadcast resulted in non-concrete element type Any.
This probably means that the function you are broadcasting contains an error or type instability.
Stacktrace:
[1] error(s::String)
# Base .\error.jl:33
[2] copy
# ~\.julia\packages\GPUArrays\gkF6S\src\host\broadcast.jl:44 [inlined]
[3] materialize
# .\broadcast.jl:883 [inlined]
[4] trova_max_likelihood(θ_1::Float64, θ_2p::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, θ_2c::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, ϵ_p::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, σ_p::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, ϵ_c::CuArray{Float64, 1, C, σ_c::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer})
# Main ~\Documents\GitHub\lab2\Lab2\Esercizio 5\esercizio5.jl:82
[5] top-level scope
# ~\Documents\GitHub\lab2\Lab2\Esercizio 5\esercizio5.jl:99
[6] eval
# .\boot.jl:360 [inlined]
[7] include_string(mapexpr::typeof(identity), mod::Module, code::String, filename::String)
# Base .\loading.jl:1094
in expression starting at C:\Users\marce\Documents\GitHub\lab2\Lab2\Esercizio 5\esercizio5.jl:99
EDIT 2: I tried switching to regular arrays and the code above wouldn't work. I had to delete a line and define:
function funzione_likelirobin(a_c::Float64, a_p::Float64)
ln_likelihood(a_c,a_p,θ_1,θ_2p,θ_2c, ϵ_p, σ_p, ϵ_c, σ_c)
end
So I made the same change in the code with CuArrays. The output I get is now:
Variables
#self#::var"##dotfunction#260#56"{var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}}
x1::CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}
Body::CuArray{_A, 1, CUDA.Mem.DeviceBuffer} where _A
1 ─ %1 = Core.getfield(#self#, :funzione_likelihood)::var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}
│ %2 = Base.broadcasted(%1, x1)::Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{1}, Nothing, var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}, Tuple{CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}}}
│ %3 = Base.materialize(%2)::CuArray{_A, 1, CUDA.Mem.DeviceBuffer} where _A
└── return %3
ERROR: LoadError: InvalidIRError: compiling kernel broadcast_kernel(CUDA.CuKernelContext, CuDeviceVector{Float64, 1}, Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}}}, Tuple{Base.Broadcast.Extruded{CuDeviceVector{Tuple{Float64, Float64}, 1}, Tuple{Bool}, Tuple{Int64}}}}, Int64) resulted in invalid LLVM IR
Reason: unsupported dynamic function invocation (call to ln_likelihood)
Stacktrace:
[1] funzione_likelirobin
# ~\Documents\GitHub\lab2\Lab2\Esercizio 5\esercizio5.jl:76
[2] funzione_likelihood
# ~\Documents\GitHub\lab2\Lab2\Esercizio 5\esercizio5.jl:79
[3] _broadcast_getindex_evalf
# .\broadcast.jl:648
[4] _broadcast_getindex
# .\broadcast.jl:621
[5] getindex
# .\broadcast.jl:575
[6] broadcast_kernel
# ~\.julia\packages\GPUArrays\gkF6S\src\host\broadcast.jl:59
Stacktrace:
[1] check_ir(job::GPUCompiler.CompilerJob{GPUCompiler.PTXCompilerTarget, CUDA.CUDACompilerParams, GPUCompiler.FunctionSpec{GPUArrays.var"#broadcast_kernel#17", Tuple{CUDA.CuKernelContext, CuDeviceVector{Float64, 1}, Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}}}, Tuple{Base.Broadcast.Extruded{CuDeviceVector{Tuple{Float64, Float64}, 1}, Tuple{Bool}, Tuple{Int64}}}}, Int64}}}, args::LLVM.Module)
# GPUCompiler ~\.julia\packages\GPUCompiler\HeCT6\src\validation.jl:111
[2] macro expansion
# ~\.julia\packages\GPUCompiler\HeCT6\src\driver.jl:326 [inlined]
[3] macro expansion
# ~\.julia\packages\TimerOutputs\YJq3h\src\TimerOutput.jl:252 [inlined]
[4] macro expansion
# ~\.julia\packages\GPUCompiler\HeCT6\src\driver.jl:324 [inlined]
[5] emit_asm(job::GPUCompiler.CompilerJob, ir::LLVM.Module; strip::Bool, validate::Bool, format::LLVM.API.LLVMCodeGenFileType)
# GPUCompiler ~\.julia\packages\GPUCompiler\HeCT6\src\utils.jl:64
[6] cufunction_compile(job::GPUCompiler.CompilerJob)
# CUDA ~\.julia\packages\CUDA\sCev8\src\compiler\execution.jl:326
[7] cached_compilation(cache::Dict{UInt64, Any}, job::GPUCompiler.CompilerJob, compiler::typeof(CUDA.cufunction_compile), linker::typeof(CUDA.cufunction_link))
# GPUCompiler ~\.julia\packages\GPUCompiler\HeCT6\src\cache.jl:90
[8] cufunction(f::GPUArrays.var"#broadcast_kernel#17", tt::Type{Tuple{CUDA.CuKernelContext, CuDeviceVector{Float64, 1}, Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}}}, Tuple{Base.Broadcast.Extruded{CuDeviceVector{Tuple{Float64, Float64}, 1}, Tuple{Bool}, Tuple{Int64}}}}, Int64}}; name::Nothing, kwargs::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
# CUDA ~\.julia\packages\CUDA\sCev8\src\compiler\execution.jl:297
[9] cufunction(f::GPUArrays.var"#broadcast_kernel#17", tt::Type{Tuple{CUDA.CuKernelContext, CuDeviceVector{Float64, 1}, Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}, CuDeviceVector{Float64, 1}}}, Tuple{Base.Broadcast.Extruded{CuDeviceVector{Tuple{Float64, Float64}, 1}, Tuple{Bool}, Tuple{Int64}}}}, Int64}})
# CUDA ~\.julia\packages\CUDA\sCev8\src\compiler\execution.jl:291
[10] macro expansion
# ~\.julia\packages\CUDA\sCev8\src\compiler\execution.jl:102 [inlined]
[11] launch_heuristic(::CUDA.CuArrayBackend, ::GPUArrays.var"#broadcast_kernel#17", ::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, ::Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}, Tuple{Base.Broadcast.Extruded{CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}, Tuple{Bool}, Tuple{Int64}}}}, ::Int64; elements::Int64, elements_per_thread::Int64)
# CUDA ~\.julia\packages\CUDA\sCev8\src\gpuarrays.jl:17
[12] copyto!(dest::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, bc::Base.Broadcast.Broadcasted{Nothing, Tuple{Base.OneTo{Int64}}, var"#funzione_likelihood#55"{var"#funzione_likelirobin#54"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}, Tuple{CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}}})
# GPUArrays ~\.julia\packages\GPUArrays\gkF6S\src\host\broadcast.jl:65
in expression starting at C:\Users\marce\Documents\GitHub\lab2\Lab2\Esercizio 5\esercizio5.jl:100
The code you have provided seems to be a bit short of an MWE. However, filling in some random data of the specified types, we have:
using CUDA
a_c, a_p, θ_1 = rand(3)
N = 1000
θ_2p, θ_2c, ϵ_p, σ_p, ϵ_c, σ_c = ntuple(x->CUDA.randn(Float64, N), 6)
function ln_likelihood( a_c::Float64,
a_p::Float64,
θ_1::Float64,
θ_2p::CuArray{Float64},
θ_2c::CuArray{Float64},
ϵ_p::CuArray{Float64},
σ_p::CuArray{Float64},
ϵ_c::CuArray{Float64},
σ_c::CuArray{Float64})
# ...
return a_c + a_p + θ_1 + sum(θ_2p) + sum(θ_2c) + sum(ϵ_p) + sum(σ_p) + sum(ϵ_c) + sum(σ_c)
end
function trova_max_likelihood( θ_1::Float64,
θ_2p::CuArray{Float64},
θ_2c::CuArray{Float64},
ϵ_p::CuArray{Float64},
σ_p::CuArray{Float64},
ϵ_c::CuArray{Float64},
σ_c::CuArray{Float64})
# ...
function funzione_likelirobin(a_c::Float64, a_p::Float64)
global θ_1, θ_2p, θ_2c, ϵ_p, σ_p, ϵ_c, σ_c
ln_likelihood(a_c, a_p, θ_1, θ_2p, θ_2c, ϵ_p, σ_p, ϵ_c, σ_c)
end
funzione_likelihood(x::Tuple{Float64, Float64}) = funzione_likelirobin(x[1],x[2])
# Better make a range if we want to broadcast over it
range = CUDA.fill((1., 2.), 10)
#code_warntype funzione_likelihood.(range)
#Where range::CuArray{Tuple{Float64,Float64}}
end
which runs for me without any error, and gives blue, stably-inferred types in the #code_warntype output:
julia> trova_max_likelihood(θ_1, θ_2p, θ_2c, ϵ_p, σ_p, ϵ_c, σ_c)
MethodInstance for (::var"##dotfunction#413#25"{var"#funzione_likelihood#24"{var"#funzione_likelirobin#23"}})(::CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer})
from (::var"##dotfunction#413#25")(x1) in Main
Arguments
#self#::Core.Const(var"##dotfunction#413#25"{var"#funzione_likelihood#24"{var"#funzione_likelirobin#23"}}(var"#funzione_likelihood#24"{var"#funzione_likelirobin#23"}(var"#funzione_likelirobin#23"())))
x1::CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}
Body::Union{}
1 ─ %1 = Core.getfield(#self#, :funzione_likelihood)::Core.Const(var"#funzione_likelihood#24"{var"#funzione_likelirobin#23"}(var"#funzione_likelirobin#23"()))
│ %2 = Base.broadcasted(%1, x1)::Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{1}, Nothing, var"#funzione_likelihood#24"{var"#funzione_likelirobin#23"}, Tuple{CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}}}
│ Base.materialize(%2)
└── Core.Const(:(return %3))
So it would seem that the instability is likely coming from somewhere in the code you have elided with ....
That said, I would heavily recommend avoiding global variables; either just specifying the variables explicitly in the function signature, or if you have to, capturing local variables in a closure, would be preferable to using globals -- which can be a major source of type-instability.
Using a closure instead of globals as follows
using CUDA
a_c, a_p, θ_1 = rand(3)
N = 1000
θ_2p, θ_2c, ϵ_p, σ_p, ϵ_c, σ_c = ntuple(x->CUDA.randn(Float64, N), 6)
function ln_likelihood( a_c::Float64,
a_p::Float64,
θ_1::Float64,
θ_2p::CuArray{Float64},
θ_2c::CuArray{Float64},
ϵ_p::CuArray{Float64},
σ_p::CuArray{Float64},
ϵ_c::CuArray{Float64},
σ_c::CuArray{Float64})
# ...
return a_c + a_p + θ_1 + sum(θ_2p) + sum(θ_2c) + sum(ϵ_p) + sum(σ_p) + sum(ϵ_c) + sum(σ_c)
end
function trova_max_likelihood( θ_1::Float64,
θ_2p::CuArray{Float64},
θ_2c::CuArray{Float64},
ϵ_p::CuArray{Float64},
σ_p::CuArray{Float64},
ϵ_c::CuArray{Float64},
σ_c::CuArray{Float64})
# ...
function funzione_likelirobin(a_c::Float64, a_p::Float64)
ln_likelihood(a_c, a_p, θ_1, θ_2p, θ_2c, ϵ_p, σ_p, ϵ_c, σ_c)
end
funzione_likelihood(x::Tuple{Float64, Float64}) = funzione_likelirobin(x[1],x[2])
# Better make a range if we want to broadcast over it
range = CUDA.fill((1., 2.), 10)
#code_warntype funzione_likelihood.(range)
#Where range::CuArray{Tuple{Float64,Float64}}
end
yields a slightly different #code_warntype output, but still with blue, stably-inferred types and no error
julia> trova_max_likelihood(θ_1, θ_2p, θ_2c, ϵ_p, σ_p, ϵ_c, σ_c)
MethodInstance for (::var"##dotfunction#414#30"{var"#funzione_likelihood#29"{var"#funzione_likelirobin#28"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}})(::CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer})
from (::var"##dotfunction#414#30")(x1) in Main
Arguments
#self#::var"##dotfunction#414#30"{var"#funzione_likelihood#29"{var"#funzione_likelirobin#28"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}}
x1::CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}
Body::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}
1 ─ %1 = Core.getfield(#self#, :funzione_likelihood)::var"#funzione_likelihood#29"{var"#funzione_likelirobin#28"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}
│ %2 = Base.broadcasted(%1, x1)::Base.Broadcast.Broadcasted{CUDA.CuArrayStyle{1}, Nothing, var"#funzione_likelihood#29"{var"#funzione_likelirobin#28"{Float64, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}, CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}}}, Tuple{CuArray{Tuple{Float64, Float64}, 1, CUDA.Mem.DeviceBuffer}}}
│ %3 = Base.materialize(%2)::CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}
└── return %3

how to manually set linecolors and linetype to factor variables using ggplot

This is my sample data:
x <- structure(list(Zeit = c(104, 104.01, 104.02, 104.03, 104.04,
104.05, 104.06, 104.07, 104.08, 104.09, 104.1, 104.11, 104.12,
104.13, 104.14, 104.15, 104.16, 104.17, 104.18, 104.19, 104.2,
104.21, 104.22, 104.23, 104.24, 104.25, 104.26, 104.27, 104.28,
104.29, 104.3, 104.31, 104.32, 104.33, 104.34, 104.35, 104.36,
104.37, 104.38, 104.39, 104.4, 104.41, 104.42, 104.43, 104.44,
104.45, 104.46, 104.47, 104.48, 104.49, 104.5, 104.51, 104.52,
104.53, 104.54, 104.55, 104.56, 104.57, 104.58, 104.59, 104.6,
104.61, 104.62, 104.63, 104.64, 104.65, 104.66, 104.67, 104.68,
104.69, 104.7, 104.71, 104.72, 104.73, 104.74, 104.75, 104.76,
104.77, 104.78, 104.79, 104.8, 104.81, 104.82, 104.83, 104.84,
104.85, 104.86, 104.87, 104.88, 104.89, 104.9, 104.91, 104.92,
104.93, 104.94, 104.95, 104.96, 104.97, 104.98, 104.99, 105,
105.01, 105.02, 105.03, 105.04, 105.05, 105.06, 105.07, 105.08,
105.09, 105.1, 105.11, 105.12, 105.13, 105.14, 105.15, 105.16,
105.17, 105.18, 105.19, 105.2, 105.21, 105.22, 105.23, 105.24,
105.25, 105.26, 105.27, 105.28, 105.29, 105.3, 105.31, 105.32,
105.33, 105.34, 105.35, 105.36, 105.37, 105.38, 105.39, 105.4,
105.41, 105.42, 105.43, 105.44, 105.45, 105.46, 105.47, 105.48,
105.49, 105.5, 105.51, 105.52, 105.53, 105.54, 105.55, 105.56,
105.57, 105.58, 105.59, 105.6, 105.61, 105.62, 105.63, 105.64,
105.65, 105.66, 105.67, 105.68, 105.69, 105.7, 105.71, 105.72,
105.73, 105.74, 105.75, 105.76, 105.77, 105.78, 105.79, 105.8,
105.81, 105.82, 105.83, 105.84, 105.85, 105.86, 105.87, 105.88,
105.89, 105.9, 105.91, 105.92, 105.93, 105.94, 105.95, 105.96,
105.97, 105.98, 105.99, 106, 106.01, 106.02, 106.03, 106.04,
106.05, 106.06, 106.07, 106.08, 106.09, 106.1, 106.11, 106.12,
106.13, 106.14, 106.15, 106.16, 106.17, 106.18, 106.19, 106.2,
106.21, 106.22, 106.23, 106.24, 106.25, 106.26, 106.27, 106.28,
106.29, 106.3, 106.31, 106.32, 106.33, 106.34, 106.35, 106.36,
106.37, 106.38, 106.39, 106.4, 106.41, 106.42, 106.43, 106.44,
106.45, 106.46, 106.47, 106.48, 106.49, 106.5, 106.51, 106.52,
106.53, 106.54, 106.55, 106.56, 106.57, 106.58, 106.59, 106.6,
106.61, 106.62, 106.63, 106.64, 106.65, 106.66, 106.67, 106.68,
106.69, 106.7, 106.71, 106.72, 106.73, 106.74, 106.75, 106.76,
106.77, 106.78, 106.79, 106.8, 106.81, 106.82, 106.83, 106.84,
106.85, 106.86, 106.87, 106.88, 106.89, 106.9, 106.91, 106.92,
106.93, 106.94, 106.95, 106.96, 106.97, 106.98, 106.99, 107),
FSB_Farbe_links = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1), FSB_Typ_links = c(2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2), Fahrspurabweichung = c(-0.310505085597022,
-0.309542196127488, -0.308607585269534, -0.309529936571746,
-0.310452287873959, -0.31137463917617, -0.312296990478383,
-0.305541752737464, -0.296619779070938, -0.287697805404398,
-0.279268412031491, -0.278781032433747, -0.278293652836003,
-0.277806273238259, -0.27734375, -0.27734375, -0.27734375,
-0.27734375, -0.277307028841091, -0.276817413388985, -0.276327797936878,
-0.27583818248477, -0.275471723942599, -0.276415832703928,
-0.277359941465258, -0.278304050226587, -0.279248158987916,
-0.276909218709719, -0.274391656193606, -0.271874093677493,
-0.269594394915214, -0.270504264010528, -0.271414133105843,
-0.272324002201155, -0.27323387129647, -0.273848567143439,
-0.274378156351682, -0.274907745559925, -0.275263407198508,
-0.273821028496787, -0.272378649795066, -0.270936271093347,
-0.269582007243196, -0.271541746362726, -0.273501485482253,
-0.275461224601782, -0.277324467368697, -0.27683506048286,
-0.276345653597023, -0.275856246711185, -0.275342947128182,
-0.274361920959113, -0.273380894790045, -0.272399868620975,
-0.271129261363634, -0.265813188962985, -0.260497116562329,
-0.255181044161673, -0.249941348984442, -0.247632253883712,
-0.245323158782982, -0.243014063682255, -0.240704968581525,
-0.243957329572663, -0.248633233331343, -0.253309137090024,
-0.257757983878592, -0.256280582214447, -0.254803180550301,
-0.253325778886158, -0.251711166051994, -0.248298486954593,
-0.244885807857196, -0.241473128759795, -0.238344631661443,
-0.239324255485894, -0.240303879310346, -0.241283503134797,
-0.24192691616877, -0.238551477940446, -0.235176039712127,
-0.231800601483803, -0.228475938574099, -0.226995101786797,
-0.225514264999493, -0.224033428212189, -0.222560651396139,
-0.221194957055284, -0.219829262714432, -0.218463568373577,
-0.217097874032723, -0.21515240460553, -0.213043027496151,
-0.210933650386773, -0.208837716721903, -0.206905460093489,
-0.204973203465074, -0.203040946836663, -0.201026177776153,
-0.196570605181605, -0.192115032587063, -0.187659459992515,
-0.182792637221818, -0.173637062614099, -0.16448148800638,
-0.155325913398674, -0.146670502011139, -0.152096945484571,
-0.157523388957995, -0.162949832431427, -0.167823533370045,
-0.165889889828726, -0.163956246287403, -0.162022602746084,
-0.159963594812924, -0.154427526218816, -0.148891457624715,
-0.143355389030607, -0.137819320436499, -0.13671875, -0.13671875,
-0.13671875, -0.136388331368362, -0.131514900223315, -0.126641469078268,
-0.121768037933228, -0.117069961448919, -0.115114245124791,
-0.113158528800661, -0.11120281247653, -0.109087597656251,
-0.104693066406248, -0.100298535156245, -0.095904003906248,
-0.0916052351095185, -0.0886749615548059, -0.0857446880000976,
-0.0828144144453848, -0.0799165604538399, -0.0774760083970585,
-0.0750354563402769, -0.072594904283499, -0.0706295692129058,
-0.075522612621854, -0.0804156560307954, -0.0853086994397436,
-0.0902017428486919, -0.0903344045097887, -0.0881645669872215,
-0.0859947294646541, -0.0837002630065289, -0.0798347937076307,
-0.0759693244087383, -0.0721038551098403, -0.0683284255948609,
-0.0673396267086446, -0.066350827822427, -0.0653620289362091,
-0.0641811014836224, -0.0608144738057093, -0.0574478461277956,
-0.0540812184498869, -0.0507053688589885, -0.0468729880000489,
-0.0430406071411036, -0.0392082262821583, -0.0353758454232185,
-0.0309318896264039, -0.0264507605571378, -0.0219696314878779,
-0.0175406360967141, -0.015666190932482, -0.0137917457682524,
-0.0119173006040199, -0.0100428554397878, -0.00626247747066144,
-0.00215128450112778, 0.00195990846840011, 0.00603664571741724,
0.00947879557013387, 0.0129209454228458, 0.0163630952755625,
0.0196466142805918, 0.0210959142880107, 0.0225452142954319,
0.0239945143028528, 0.025643890937711, 0.0325448701669515,
0.0394458493961822, 0.0463468286254227, 0.0528784270446119,
0.0548146104399012, 0.0567507938351932, 0.0586869772304852,
0.0605855254897036, 0.0615665023857366, 0.0625474792817693,
0.0635284561778009, 0.0647462074738052, 0.0698521739162818,
0.0749581403587509, 0.0800641068012273, 0.0851700732436964,
0.088986019218912, 0.0925737786856788, 0.0961615381524406,
0.0996283662176822, 0.100115320517216, 0.10060227481675,
0.101089229116284, 0.101589879118756, 0.102564224981917,
0.103538570845078, 0.104512916708239, 0.105515676372029,
0.107985485426153, 0.110455294480273, 0.112925103534396,
0.115266227152696, 0.115756260271094, 0.116246293389493,
0.116736326507891, 0.117416481094767, 0.120304010663071,
0.123191540231376, 0.126079069799677, 0.128935432379541,
0.130331718481913, 0.131728004584287, 0.133124290686661,
0.134520576789033, 0.132640421681579, 0.130062855497926,
0.127485289314277, 0.1250530260181, 0.126534199708033, 0.128015373397963,
0.129496547087895, 0.130898017701937, 0.131381656024044,
0.131865294346152, 0.13234893266826, 0.1326290307242, 0.12820808431943,
0.123787137914665, 0.119366191509894, 0.115297914956724,
0.116269473928021, 0.117241032899319, 0.118212591870618,
0.119184534170744, 0.120164649589137, 0.121144765007527,
0.122124880425919, 0.123101959330116, 0.124030868775564,
0.124959778221013, 0.125888687666462, 0.126817597111909,
0.128719499483801, 0.130787611512604, 0.132855723541404,
0.134580766090116, 0.132164309751811, 0.129747853413503,
0.127331397075195, 0.125052047846167, 0.126530679839505,
0.128009311832843, 0.129487943826179, 0.130999529467622,
0.132932694538269, 0.134865859608913, 0.136799024679559,
0.138656476632385, 0.138162939208824, 0.137669401785262,
0.137175864361702, 0.136860652119074, 0.138783445060425,
0.140706238001773, 0.142629030943124, 0.144536249506591,
0.145003493113203, 0.145470736719816, 0.145937980326428,
0.14640522393304, 0.147777497774738, 0.149334351584919, 0.150891205395101,
0.152311024520469, 0.151822584527471, 0.151334144534473,
0.150845704541475, 0.150456748940469, 0.151424888625212,
0.152393028309955, 0.153361167994696, 0.154329985514398,
0.155318359078489, 0.15630673264258), Querabstand_links = c(1.64228559917915,
1.63554537289242, 1.62890625, 1.62890625, 1.62890625, 1.62890625,
1.62890625, 1.62317592872497, 1.6158284209996, 1.60848091327421,
1.60144864812596, 1.59949912973499, 1.59754961134401, 1.59560009295304,
1.59364978936117, 1.59168487487424, 1.58971996038732, 1.5877550459004,
1.58579061536437, 1.58383215355594, 1.58187369174751, 1.57991522993908,
1.5782060989426, 1.57915020770393, 1.58009431646526, 1.58103842522659,
1.58198253398792, 1.57630087490333, 1.57025872486465, 1.56421657482598,
1.55916205423693, 1.56735087609476, 1.57553969795258, 1.5837285198104,
1.59191734166823, 1.59457213428688, 1.59563131270336, 1.59669049111985,
1.59740181439702, 1.59451705699357, 1.59163229959013, 1.58874754218669,
1.58603901448639, 1.58995849272545, 1.59387797096451, 1.59779744920356,
1.6015625, 1.6015625, 1.6015625, 1.6015625, 1.60146714425636,
1.59950509191823, 1.59754303958009, 1.59558098724195, 1.59316890495867,
1.58446987739398, 1.57577084982927, 1.56707182226456, 1.55849990837511,
1.55480535621394, 1.55111080405277, 1.54741625189161, 1.54372169973044,
1.55041465914533, 1.55976646666269, 1.56911827418005, 1.57805231183812,
1.5760824429526, 1.57411257406707, 1.57214270518154, 1.56996684435999,
1.56509158850656, 1.56021633265314, 1.55534107679971, 1.55090801332289,
1.55286726097179, 1.55482650862069, 1.55678575626959, 1.55822148738396,
1.55339943277207, 1.54857737816018, 1.54375532354829, 1.53900958476547,
1.53703513571573, 1.53506068666599, 1.53308623761625, 1.53125,
1.53125, 1.53125, 1.53125, 1.53125, 1.52878329440829, 1.52561922874423,
1.52245516308016, 1.51931126258285, 1.51641287764023, 1.51351449269761,
1.51061610775499, 1.50765061419573, 1.50269997797956, 1.4977493417634,
1.49279870554724, 1.48684767976536, 1.47046401994102, 1.45408036011668,
1.43769670029236, 1.42224725402228, 1.43310014096914, 1.44395302791599,
1.45480591486285, 1.46462592505507, 1.46172545974309, 1.45882499443111,
1.45592452911913, 1.45280390802154, 1.44357712703136, 1.43435034604119,
1.42512356505101, 1.41589678406083, 1.4140625, 1.4140625,
1.4140625, 1.41353383018938, 1.4057363403573, 1.39793885052523,
1.39014136069317, 1.38263619217338, 1.37970261768719, 1.37676904320099,
1.3738354687148, 1.3707744140625, 1.3658916015625, 1.36100878906249,
1.3561259765625, 1.35124310018253, 1.34635931092468, 1.34147552166683,
1.33659173240897, 1.33177274672614, 1.32786786343529, 1.32396298014444,
1.3200580968536, 1.31704038842581, 1.32682647524371, 1.33661256206159,
1.34639864887949, 1.35618473569738, 1.35718129426468, 1.35392653798083,
1.35067178169698, 1.34730111000816, 1.34246927338454, 1.33763743676092,
1.3328056001373, 1.32806310118972, 1.32608550341729, 1.32410790564485,
1.32213030787242, 1.3199238949766, 1.3151144268653, 1.31030495875399,
1.3054954906427, 1.30066742828848, 1.29491885700007, 1.28917028571166,
1.28342171442324, 1.27767314313483, 1.2698337760025, 1.2618673243238,
1.25390087264512, 1.24603751664507, 1.24322584889872, 1.24041418115238,
1.23760251340603, 1.23479084565968, 1.23087185247066, 1.22676065950113,
1.2226494665316, 1.21844610734157, 1.21254527902263, 1.20664445070369,
1.20074362238475, 1.19515868095921, 1.19322628094932, 1.19129388093942,
1.18936148092953, 1.18710201066931, 1.17625761473765, 1.165413218806,
1.15456882287434, 1.14431517193308, 1.14141089684015, 1.13850662174721,
1.13560234665427, 1.1327738495103, 1.13179287261426, 1.13081189571823,
1.1298309188222, 1.12847994821992, 1.12105308793995, 1.11362622766,
1.10619936738003, 1.09877250710008, 1.09330122254441, 1.0881758518776,
1.0830504812108, 1.07816298243536, 1.07913689103443, 1.0801107996335,
1.08108470823257, 1.08200387088124, 1.08102952501808, 1.08005517915492,
1.07908083329176, 1.07806868835357, 1.07510491748862, 1.07214114662367,
1.06917737575872, 1.06653365861078, 1.06849379108438, 1.07045392355797,
1.07241405603156, 1.07391344187364, 1.07006340244924, 1.06621336302483,
1.06236332360043, 1.05855484016061, 1.05669312535745, 1.05483141055428,
1.05296969575112, 1.05110798094796, 1.05843198194632, 1.06771122020747,
1.0769904584686, 1.08590214932127, 1.08491470019464, 1.08392725106802,
1.0829398019414, 1.08203125, 1.08203125, 1.08203125, 1.08203125,
1.08256127235231, 1.09533289529943, 1.10810451824652, 1.12087614119364,
1.13274896004328, 1.13177740107198, 1.13080584210068, 1.12983428312938,
1.12890625, 1.12890625, 1.12890625, 1.12890625, 1.12885116566988,
1.12792225622444, 1.12699334677899, 1.12606443733354, 1.12513552788809,
1.1223504382743, 1.11924827023109, 1.11614610218789, 1.11379885494768,
1.12056493269493, 1.12733101044219, 1.13409708818945, 1.14055560287178,
1.13858409354733, 1.13661258422288, 1.13464107489843, 1.13260226829857,
1.1297025206926, 1.12680277308663, 1.12390302548066, 1.12112454673523,
1.12211162158235, 1.12309869642947, 1.1240857712766, 1.12478714682139,
1.12190295740936, 1.11901876799734, 1.11613457858531, 1.11328125,
1.11328125, 1.11328125, 1.11328125, 1.11328125, 1.11241916815017,
1.11138126561005, 1.11034336306993, 1.10944045095906, 1.11041733094506,
1.11139421093105, 1.11237109091705, 1.11321512605953, 1.11224698637479,
1.11127884669005, 1.1103107070053, 1.10927566845681, 1.10631054776453,
1.10334542707226)), row.names = c(NA, -301L), class = c("data.table",
"data.frame"))
I try to plot one of the two lines with different linetypes (red line). But instead of one red line with 2 different linetypes I get 2 red lines. There is also a chance that the linecolor might also change (not in this example). How can I achieve this?
Can I change the color or linetype manually depending on the factor variable?
This is the code I use but that is not working like expected. If I try to set the group to 1 I get an error.
library(data.table)
library(ggplot2)
ggplot(x) +
geom_line(aes(x = Zeit, y = Fahrspurabweichung), colour = "black", size = 1.5) +
geom_line(aes(x = Zeit, y = Fahrspurabweichung + Querabstand_links, color = factor(FSB_Farbe_links), linetype = factor(FSB_Typ_links)), size = 1.5)
I think what you're looking for is for the red line to alternate between linetypes based on the running value of FSB_Farbe_links. In ggplot2 (among others), the linetype= cannot change within a line (similarly for color=). If you need it to vary within a line, then ... you need multiple lines.1
Try this:
x[, grp := rleid(FSB_Typ_links), by = .(FSB_Farbe_links)]
# add "group=grp" to your geom_line:
ggplot(x) +
geom_line(aes(x = Zeit, y = Fahrspurabweichung), colour = "black", size = 1.5) +
geom_line(aes(x = Zeit, y = Fahrspurabweichung + Querabstand_links,
color = factor(FSB_Farbe_links),
linetype = factor(FSB_Typ_links),
group = grp), size = 1.5)
Explanation: data.table::rleid assigns a unique id (integer) based on the "run-length encoding" (rle) useful in grouping operations, assigning the group pairwise either the values. As a demonstration of that function, shown in a matrix solely for visualization (the return value is integer):
vec <- c(99,99,99,105,105,105,99,102,105)
cbind(vec, rleid(vec))
# vec
# [1,] 99 1
# [2,] 99 1
# [3,] 99 1
# [4,] 105 2
# [5,] 105 2
# [6,] 105 2
# [7,] 99 3
# [8,] 102 4
# [9,] 105 5
Where the first three 99s are unchanging, so they are group 1. 105 is repeated 3 times as well. The next is 99 again, though since it is not contiguous with the first batch of 99, it gets a new id. Hope that makes sense.
This method of grouping is sensitive to the order of data. If not already sorted (by Zeit here), then its results will be different (and likely logically incorrect). This should be a "grouped by" operation (by=.(FSB_Farbe_links) here); while it's feasible to also sort by the groups and achieve the same effect, it is also possible that the RLE variable FSB_Typ_links does not change between two different groups, which would appear as one rle-group. Therefore, whenever talking rle-like operations, I tend to urge grouping functions (whether data.table's by=.(), dplyr::group_by, or base R's grouping functions such as aggregate/ave/by.)
data.table::rleid is the data.table convenience function that mimics somewhat the base::rle function, which does the same basic thing, but with a key difference:
str(rle(vec))
# List of 2
# $ lengths: int [1:5] 3 3 1 1 1
# $ values : num [1:5] 99 105 99 102 105
# - attr(*, "class")= chr "rle"
rleid(vec)
# [1] 1 1 1 2 2 2 3 4 5
rle does not assign groups, it just tells you what ($values) and how long ($lengths) for each contiguous value. You can see there that 99 has 3, 105 has 3, 99 has 1, etc.
It is easy to mimic rleid without the data.table package, if you're curious:
my_rleid <- function(z) {
r <- rle(z)
rep.int(seq_along(r$lengths), times = r$lengths)
}
my_rleid(vec)
# [1] 1 1 1 2 2 2 3 4 5
While my base-R variant is simple, data.table::rleid takes half the time with this small vector and is much faster with larger datasets.
bench::mark(my_rleid(vec), data.table::rleid(vec))
# # A tibble: 2 x 13
# expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time result memory time gc
# <bch:expr> <bch:t> <bch:t> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm> <list> <list> <list> <list>
# 1 my_rleid(vec) 16.1us 19.9us 36076. 17.2KB 0 10000 0 277ms <int [~ <Rprofm~ <bch:~ <tibbl~
# 2 data.table::rleid(vec) 8.3us 9.7us 93380. 0B 9.34 9999 1 107ms <int [~ <Rprofm~ <bch:~ <tibbl~
vec <- sample(1e4)
bench::mark(my_rleid(vec), data.table::rleid(vec))
# # A tibble: 2 x 13
# expression min median `itr/sec` mem_alloc `gc/sec` n_itr n_gc total_time result memory time gc
# <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> <int> <dbl> <bch:tm> <list> <list> <list> <list>
# 1 my_rleid(vec) 239.4us 265.6us 3135. 1016.9KB 47.7 1052 16 336ms <int [~ <Rprof~ <bch:~ <tibb~
# 2 data.table::rleid(vec) 14.7us 16.3us 51087. 39.1KB 25.6 9995 5 196ms <int [~ <Rprof~ <bch:~ <tibb~
(If you aren't familiar with bench::mark, the \itr/sec`` column is iterations per second, one metric for the relative speed of the functions.)
Note:
Technically, unfortunately, this method does not produce a perfectly contiguous line: at the transition from solid to dash (and vice versa), there will be a gap as big as the gap in Zeit With linetype this may be masked, since every line type except "solid" has gaps built in to the visual structure. However, if the gap in Zeit is not small, then you may notice a larger gap. To make this more evident, plot just the top (formerly red) line and use color= instead of linetype=:
ggplot(x) +
geom_line(aes(x = Zeit, y = Fahrspurabweichung + Querabstand_links,
color = factor(FSB_Typ_links),
group = grp), size = 1.5)
I don't know of an easy way to mitigate this; the only way I know of is to then "copy" the first point of each contiguous group into the previous group. It is not hard (really just a shift operation without grouping and then reassign a couple of values, by-group), but if it isn't a problem, then I suggest sticking with the simpler more-readable approach that allows the minute gaps.
This is my solution to your problem.
I transformed the dataframe so you can reduce the geom_line() calls by one.
x <- x %>% mutate(FahrspurabstandPlusAbstand = Fahrspurabweichung + Querabstand_links) %>%
pivot_longer(cols = c(Fahrspurabweichung, FahrspurabstandPlusAbstand), names_to = "name", values_to = "Fahrspurabstand")
As one line can't have two linetypes, I used the filter function to introduce a second line which overlays the dashed line.
ggplot(x, aes(Zeit, Fahrspurabstand)) +
geom_line(aes(group = name, linetype = name, color = name)) +
scale_linetype_manual(values = c("dashed", "solid"), guide = FALSE) +
geom_line(data = filter(x, FSB_Typ_links == 1 & name == "FahrspurabstandPlusAbstand"), col = "red") +
scale_color_manual(values = c("red", "black"))
The resulting plot looks like this
I had a similar problem a while ago and solved it like this:
library(data.table)
library(ggplot2)
ggplot(x) +
geom_line(aes(x = Zeit, y = Fahrspurabweichung), colour = "black", size = 1.5) +
geom_line(aes(x=Zeit,y =(Fahrspurabweichung + Querabstand_links)[ifelse(FSB_Typ_links==1, NA , T)]),na.rm=TRUE,linetype="dashed",colour = "red", size = 1)+
geom_line(aes(x=Zeit,y =(Fahrspurabweichung + Querabstand_links)[ifelse(FSB_Typ_links==1, T , NA)]),na.rm=TRUE, linetype="solid",colour = "red", size = 1)
Remainig problem is, that it doesnt show up automatically in the legend.
But maybe it helps already.

R: "Error: unexpected string constant in" with read_fwf()

I am trying to read a fixed width file from U.S. Census Bureau into R using read_fwf(). I keep getting an error in the same place in the list of column names. I have tried to change the particular column name at the location in multiple attempts and R keeps throwing the error. I restarted R to a new session and I keep getting the error. In the list of column names, it's the 39th item that seems to have the problem. I've changed the name of the 39th, and sometimes the 38th, position in one of the attempts I've included in code. The first line of code in the code block has the original column name values. In that line, the 39th name is "cbsac", but the error prints that as "... "" ". It's close to the name in the 38th position, "cbsa", but a lot of the names in succession in other parts of the list are very similar and they don't cause an error. I don't know what that is supposed to indicate. Does "cbsac" mean something in R that I'm not aware of?
library(readr)
> tf <- read_fwf("D:/projects_and_data/data/PostgreSQL/data/data/or2010.sf1/orgeo2010.sf1", fwf_widths( c(6, 2, 3, 2, 3, 2, 7, 1, 1, 2, 3, 2, 2, 5, 2, 2, 5, 2, 2, 6, 1, 4, 2, 5, 2, 2, 4, 5, 2, 1, 3, 5, 2, 6, 1, 5, 2, 5, 2, 5, 3, 5, 2, 5, 3, 1, 1, 5, 2, 1, 1, 2, 3, 3, 6, 1, 3, 5, 5, 2, 5, 5, 5, 14, 14, 90, 1, 1, 9, 9, 11, 12, 2, 1, 6, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 5, 18), c("fileid", "stusab", "sumlev", "geocomp", "chariter", "cifsn", "logrecno", "region", "division", "state", "county", "countycc", "countysc", "cousub", "cousubcc", "cousubsc", "place", "placecc", "placesc", "tract", "blkgrp", "block", "iuc", "concit", "concitcc", "concitsc", "aianhh", "aianhhfp", "aianhhcc", "aihhtli", "aitsce", "aits", "aitscc", "ttract", "tblkgrp", "anrc", "anrccc", "cbsa", "cbsac", "metdiv", "csa", "necta", "nectasc", "nectadiv" "cnecta", "cbsapci", "nectapci", "ua", "uasc", "uatype", "ur", "cd", "sldu", "sldl", "vtd", "vtdi", "reserve2", "zcta5", "submcd", "submcdcc", "sdelem", "sdsec", "sduni", "arealand", "areawatr", "name", "funcstat", "gcuni", "pop100", "hu100", "intptlat", "intptlon", "lsadc", "partflag", "reserve3", "uga", "statens", "countyns", "cousubns", "placens", "concitns", "aianhhns", "aitsns", "anrcns", "submcdns", "cd113", "cd114", "cd115", "sldu2", "sldu3", "sldu4", "sldl2", "sldl3", "sldl4", "aianhhsc", "csasc", "cnectasc", "memi", "nmemi", "puma", "reserved")))
Error: unexpected string constant in ""tract", "blkgrp", "block", "iuc", "concit", "concitcc", "concitsc", "aianhh", "aianhhfp", "aianhhcc", "aihhtli", "aitsce", "aits", "aitscc", "ttract", "tblkgrp", "anrc", "anrccc", "cbsa", ""
> tf <- read_fwf("D:/projects_and_data/data/PostgreSQL/data/data/or2010.sf1/orgeo2010.sf1", fwf_widths( c(6, 2, 3, 2, 3, 2, 7, 1, 1, 2, 3, 2, 2, 5, 2, 2, 5, 2, 2, 6, 1, 4, 2, 5, 2, 2, 4, 5, 2, 1, 3, 5, 2, 6, 1, 5, 2, 5, 2, 5, 3, 5, 2, 5, 3, 1, 1, 5, 2, 1, 1, 2, 3, 3, 6, 1, 3, 5, 5, 2, 5, 5, 5, 14, 14, 90, 1, 1, 9, 9, 11, 12, 2, 1, 6, 5, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 5, 18), c("fileid", "stusab", "sumlev", "geocomp", "chariter", "cifsn", "logrecno", "region", "division", "state", "county", "countycc", "countysc", "cousub", "cousubcc", "cousubsc", "place", "placecc", "placesc", "tract", "blkgrp", "block", "iuc", "concit", "concitcc", "concitsc", "aianhh", "aianhhfp", "aianhhcc", "aihhtli", "aitsce", "aits", "aitscc", "ttract", "tblkgrp", "anrc", "anrccc", "BCas", "CBsac", "metdiv", "csa", "necta", "nectasc", "nectadiv" "cnecta", "cbsapci", "nectapci", "ua", "uasc", "uatype", "ur", "cd", "sldu", "sldl", "vtd", "vtdi", "reserve2", "zcta5", "submcd", "submcdcc", "sdelem", "sdsec", "sduni", "arealand", "areawatr", "name", "funcstat", "gcuni", "pop100", "hu100", "intptlat", "intptlon", "lsadc", "partflag", "reserve3", "uga", "statens", "countyns", "cousubns", "placens", "concitns", "aianhhns", "aitsns", "anrcns", "submcdns", "cd113", "cd114", "cd115", "sldu2", "sldu3", "sldu4", "sldl2", "sldl3", "sldl4", "aianhhsc", "csasc", "cnectasc", "memi", "nmemi", "puma", "reserved")))
Error: unexpected string constant in ""tract", "blkgrp", "block", "iuc", "concit", "concitcc", "concitsc", "aianhh", "aianhhfp", "aianhhcc", "aihhtli", "aitsce", "aits", "aitscc", "ttract", "tblkgrp", "anrc", "anrccc", "BCas", ""
> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] readr_1.3.1
loaded via a namespace (and not attached):
[1] compiler_3.6.1 backports_1.1.5 R6_2.4.0 hms_0.5.1
[5] pillar_1.4.2 tibble_2.1.3 Rcpp_1.0.2 crayon_1.3.4
[9] vctrs_0.2.0 zeallot_0.1.0 pkgconfig_2.0.3 rlang_0.4.0
This links to a zip that has the source file. The file is "orgeo2010.sf1". I should have said, the zip is kind of big. Sorry about that.
Does this fix your issue?
widths <- c(6, 2, 3, 2, 3, 2, 7, 1, 1, 2, 3, 2, 2, 5, 2, 2, 5,
2, 2, 6, 1, 4, 2, 5, 2, 2, 4, 5, 2, 1, 3, 5, 2, 6, 1, 5, 2, 5,
2, 5, 3, 5, 2, 5, 3, 1, 1, 5, 2, 1, 1, 2, 3, 3, 6, 1, 3, 5, 5,
2, 5, 5, 5, 14, 14, 90, 1, 1, 9, 9, 11, 12, 2, 1, 6, 5, 8, 8,
8, 8, 8, 8, 8, 8, 8, 2, 2, 2, 3, 3, 3, 3, 3, 3, 2, 2, 2, 1, 1, 5, 18)
vars <- c("fileid", "stusab", "sumlev", "geocomp", "chariter", "cifsn", "logrecno",
"region", "division", "state", "county", "countycc", "countysc", "cousub",
"cousubcc", "cousubsc", "place", "placecc", "placesc", "tract", "blkgrp", "block",
"iuc", "concit", "concitcc", "concitsc", "aianhh", "aianhhfp", "aianhhcc", "aihhtli",
"aitsce", "aits", "aitscc", "ttract", "tblkgrp", "anrc", "anrccc", "cbsa", "cbsac",
"metdiv", "csa", "necta", "nectasc", "nectadiv", "cnecta", "cbsapci", "nectapci",
"ua", "uasc", "uatype", "ur", "cd", "sldu", "sldl", "vtd", "vtdi", "reserve2",
"zcta5", "submcd", "submcdcc", "sdelem", "sdsec", "sduni", "arealand", "areawatr",
"name", "funcstat", "gcuni", "pop100", "hu100", "intptlat", "intptlon", "lsadc",
"partflag", "reserve3", "uga", "statens", "countyns", "cousubns", "placens",
"concitns", "aianhhns", "aitsns", "anrcns", "submcdns", "cd113", "cd114", "cd115",
"sldu2", "sldu3", "sldu4", "sldl2", "sldl3", "sldl4", "aianhhsc", "csasc",
"cnectasc", "memi", "nmemi", "puma", "reserved")
td <- read_fwf("D:/projects_and_data/data/PostgreSQL/data/data/or2010.sf1/orgeo2010.sf1", fwf_widths(widths)
names(td) <- vars
The unexpected string constant is caused by not defining the character vector correctly (you were missing a comma)

How to plot a partition layout/table

I want to plot a partition layout a.k.a. partition table in R. This type of diagram plots hierarchical data horizontally from left to right with nodes as space-filling tiles of proportional height and with tile-adjacency as connections. The x-axis represents hierarchy levels and the y-axis whatever metric used.
Flawed makeshift example cobbled in Excel:
It could also be compared to:
a horizontal uniform-depth icicle plot
a non-nested treemap
a table with variable-height cells
This type of diagram is available in D3.js.
How can I do it in R? Is it possible with ggplot2?
EDIT: Here's my actual data. I need emp_est (not a count) to be the y-axis. emp_est for N-level NAICS code equals the sum of emp_est of daughter N-1-level NAICS codes.
structure(list(naics_level = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3), naics = c("62----", "44----", "54----", "72----", "61----",
"52----", "31----", "56----", "42----", "51----", "81----", "23----",
"55----", "48----", "71----", "53----", "22----", "99----", "11----",
"21----", "541///", "722///", "611///", "622///", "561///", "621///",
"624///", "445///", "551///", "623///", "238///", "423///", "524///",
"522///", "523///", "511///", "813///", "424///", "334///", "448///",
"812///", "531///", "713///", "452///", "441///", "518///", "721///",
"236///", "446///", "444///", "311///", "485///", "811///", "517///",
"332///", "454///", "325///", "453///", "333///", "519///", "484///",
"339///", "237///", "451///", "711///", "221///", "492///", "425///",
"447///", "481///", "442///", "493///", "532///", "336///", "562///",
"488///", "443///", "323///", "326///", "335///", "712///", "515///",
"512///", "327///", "337///", "313///", "322///", "312///", "533///",
"521///", "315///", "331///", "321///", "314///", "316///", "487///",
"525///", "324///", "212///", "115///", "483///", "486///", "114///",
"113///", "213///", "211///", "7225//", "6113//", "6221//", "5511//",
"4451//", "6241//", "5613//", "5415//", "5221//", "5417//", "6211//",
"5416//", "5413//", "5241//", "6231//", "2382//", "5617//", "5239//",
"5112//", "4481//", "7139//", "6111//", "6216//", "6214//", "5411//",
"5182//", "7211//", "5412//", "6244//", "3345//", "6232//", "4234//",
"5242//", "4461//", "8131//", "7223//", "8121//", "4411//", "4441//",
"4521//", "6212//", "5616//", "5313//", "6233//", "6223//", "5419//",
"4244//", "8111//", "4236//", "6116//", "5171//", "5111//", "2383//",
"5191//", "6213//", "5231//", "5614//", "2362//", "2361//", "5311//",
"5418//", "4541//", "4529//", "3344//", "3254//", "4251//", "2381//",
"8129//", "4471//", "4511//", "4921//", "5611//", "6222//", "2389//",
"4931//", "4811//", "2211//", "4431//", "4242//", "4238//", "3391//",
"3231//", "3118//", "8139//", "4841//", "3327//", "8123//", "4854//",
"7224//", "8134//", "5222//", "6215//", "6219//", "4413//", "8133//",
"2371//", "4243//", "4453//", "6243//", "3261//", "4422//", "4532//",
"4543//", "5615//", "4842//", "5619//", "3364//", "6242//", "8132//",
"3399//", "5312//", "4539//", "3332//", "7113//", "3359//", "4231//",
"4851//", "4239//", "7121//", "2373//", "4482//", "4237//", "4452//",
"5179//", "4859//", "3119//", "4853//", "5121//", "5621//", "3339//",
"6112//", "4249//", "4241//", "6117//", "5629//", "4233//", "3329//",
"3323//", "5324//", "5151//", "4248//", "4483//", "5223//", "4885//",
"5321//", "4421//", "4512//", "3328//", "5414//", "7111//", "4881//",
"4232//", "3113//", "4533//", "7112//", "5612//", "6115//", "3333//",
"6239//", "5172//", "3115//", "3116//", "3222//", "3121//", "5322//",
"8122//", "8112//", "3363//", "6114//", "4247//", "2212//", "4884//",
"3342//", "8113//", "4235//", "4246//", "5331//", "3341//", "4442//",
"3372//", "3133//", "3262//", "5211//", "3114//", "3251//", "3353//",
"8114//", "3117//", "4531//", "3252//", "3132//", "4412//", "3152//",
"3334//", "3371//", "3219//", "7131//", "5152//", "3273//", "3255//",
"3335//", "3321//", "3324//", "3259//", "3322//", "3272//", "2379//",
"4922//", "5622//", "7115//", "3315//", "3149//", "4855//", "3279//",
"3162//", "4872//", "2372//", "4812//", "3351//", "4852//", "2213//",
"3336//", "3366//", "3141//", "7212//", "3362//", "5259//", "4542//",
"3241//", "4883//", "3256//", "2123//", "3221//", "4831//", "7114//",
"3369//", "3379//", "3313//", "1152//", "3346//", "5323//", "3343//",
"7132//", "3274//", "3314//", "4862//", "4889//", "7213//", "3271//",
"5122//", "3169//", "3352//", "4871//", "3331//", "3161//", "3326//",
"3312//", "3159//", "4245//", "5174//", "1133//", "3361//", "3253//",
"3325//", "3112//", "1141//", "1153//", "2131//", "1142//", "3211//",
"3111//", "2111//", "5232//", "3365//", "1151//", "1132//", "3131//",
"3212//", "4832//", "1131//", "2122//", "3122//", "3311//", "4879//"
), emp_est = c(444491, 266068, 242031, 217912, 172492, 156366,
150651, 141859, 106269, 103451, 94438, 93862, 78951, 61390, 42552,
38517, 8786, 644, 475, 373, 242031, 193891, 172492, 145566, 134867,
132980, 92554, 79489, 78951, 73391, 63089, 59167, 56680, 53411,
44938, 42428, 41420, 38574, 35544, 35191, 34789, 29626, 29104,
27284, 25031, 24318, 24021, 20156, 19473, 19472, 18503, 18446,
18229, 17982, 16842, 14807, 13685, 12597, 11880, 11531, 11199,
10983, 10617, 10365, 9554, 8786, 8565, 8528, 8025, 7760, 7578,
7390, 7369, 7308, 6992, 6955, 6756, 6511, 6428, 5969, 3894, 3649,
3543, 2642, 2586, 2413, 2154, 1871, 1522, 1256.5, 1145, 1043,
1038, 988, 799, 645, 324, 319, 291, 282, 282, 148, 108, 85, 58,
24, 168952, 123448, 122772, 78951, 70361, 59968, 52588, 47253,
44865, 43270, 40371, 39138, 37926, 36667, 35557, 35527, 34236,
33878, 30390, 28710, 28005, 26446, 26006, 25954, 24481, 24318,
23538, 23091, 22838, 22786, 20565, 20463, 20013, 19473, 18890,
18796, 18617, 18271, 17994, 17856, 17654, 17590, 15429, 15258,
15038, 14706, 14407, 13729, 13324, 12736, 12354, 12038, 11949,
11531, 11385, 11049, 10961, 10105, 10051, 9894, 9756, 9507, 9428,
9329, 9296, 8528, 8059, 8033, 8025, 7895, 7846, 7786, 7756, 7554,
7390, 7258, 6823, 6756, 6702, 6689, 6661, 6511, 6443, 6412, 6351,
6332, 6317, 6272, 6143, 6056, 5918, 5827, 5783, 5689, 5663, 5526,
5381, 5379, 5242, 5138, 5047, 4979, 4977, 4905, 4848, 4742, 4569,
4506, 4399, 4322, 4303, 4287, 4274, 4237, 4212, 4152, 4057, 4045,
3894, 3855, 3810, 3802, 3749, 3611, 3599, 3551, 3493, 3418, 3289,
3182, 3138, 3092, 3063, 3020, 2994, 2859, 2819, 2795, 2739, 2722,
2701, 2671, 2628, 2617, 2607, 2531, 2470, 2467, 2410, 2322, 2316,
2302, 2273, 2220, 2066, 2059, 2054, 2029, 2011, 1939, 1901, 1876,
1870, 1868, 1851, 1822, 1795, 1698, 1650, 1627, 1578, 1567, 1547,
1540, 1531, 1523, 1522, 1515, 1478, 1330, 1322, 1290, 1256.5,
1251, 1174, 1171, 1165, 1112, 1111, 1109, 1084, 1071, 1067, 1035,
1021, 992, 944, 927, 925, 909, 908, 827, 823, 820, 809.5, 801,
719, 719, 709, 672, 661, 645, 630, 618, 596, 547, 517, 502, 482,
395, 385, 358, 355, 343, 342, 340, 324, 323, 319, 311, 304, 288,
286, 274, 257, 238, 235, 209, 206, 197, 172, 170, 155, 154, 148,
148, 144, 141, 133, 125, 111, 104, 96, 94, 92, 90, 80, 78, 78,
78, 77, 77, 73, 73, 68, 66, 66, 58, 41.5, 38, 28, 24, 14.5, 13,
12, 9.5, 9.5, 8, 8, 2.5, 2.5, 2.5, 2.5, 2.5)), .Names = c("naics_level",
"naics", "emp_est"), row.names = c(NA, -390L), class = c("tbl_df",
"tbl", "data.frame"))
Here's one simple example in ggplot2:
d <- data.frame(level3 = c(rep('aaa', 4), 'aab', rep('aba', 2), 'abb', 'aca', 'acb'), stringsAsFactors = FALSE)
d$level2 <- substr(d$level3, 1, 2)
d$level1 <- substr(d$level3, 1, 1)
d$id <- 1:nrow(d)
d2 <- tidyr::gather(d, level, label, -id)
library(ggplot2)
ggplot(d2, aes(level, group = label, fill = level)) +
geom_bar(position = 'stack', col = 1, width = 1) +
geom_text(aes(label = label), position = position_stack(vjust = 0.5), stat = 'count')

Resources