Converting dataframe to pumas data frame

my data looks like

|id|Time|CONC|PCTESTCD|CMT|AMT|EVID|DVID|bmi|age|wgtno|
|1003|0|10.75157338|CHOLINE|2|0|0|1|29.74639462|53|102.8|
|1003|0|2.396496003|TMAO|8|0|0|2|29.74639462|53|102.8|
|1003|24|13.6314591|CHOLINE|2|0|0|1|29.74639462|53|102.8|
|1003|24|1.328723895|TMAO|8|0|0|2|29.74639462|53|102.8|
|1003|48|10.84756957|CHOLINE|2|0|0|1|29.74639462|53|102.8|
|1003|48|1.397956002|TMAO|8|0|0|2|29.74639462|53|102.8|

running this code

Data1_df = Data1
Data1_df[!, :CONC] .= ifelse.(Data1_df.EVID .==1, missing,Data1_df.CONC )

pk_data = read_pumas(Data1_df,
                    id           = :id,
                    time         = :Time,
                    amt          = :AMT,
                    observations = [:CONC],
                    covariates   = [:wgtno],
                    evid         = :EVID,
                    cmt          = :CMT)

i get this error

PumasDataError: (id, Time) pair should be unique, if your data has multiple observations in one column, please convert it to wide format.

my guess is it has something to do with having four compartments that have obervations. cmt = 2, 7, 8, and 9
the dose is in compartment 1

Do you have observed data that is representing reach compartment that your modeling together?

Yes

4 compartments have data

image001.png

hi Robert,
We don’t require that data is in long form and in fact suggest that the data should be in wide form for Pumas modeling. So, if you have a NM dataset with multiple DV’s stacked on top of each other, you could unstack them and work so that you derived block can specify a distribution for each variable. A reproducible example for you to adapt the code from is below.

Hope that helps



julia> df = DataFrame(id =  [1,1,1,2,2,2], 
                      amt = [1, 0, 0, 1, 0,0],
                     time = [0,1,1,0,1,1], 
                     dv = [0,1.2,8.2,0,1.1,9.1], 
                     cmt = [:Depot,:Parent,:Metab,:Depot,:Parent,:Metab])
6×5 DataFrame
│ Row │ id    │ amt   │ time  │ dv      │ cmt    │
│     │ Int64 │ Int64 │ Int64 │ Float64 │ Symbol │
├─────┼───────┼───────┼───────┼─────────┼────────┤
│ 1   │ 1     │ 1     │ 0     │ 0.0     │ Depot  │
│ 2   │ 1     │ 0     │ 1     │ 1.2     │ Parent │
│ 3   │ 1     │ 0     │ 1     │ 8.2     │ Metab  │
│ 4   │ 2     │ 1     │ 0     │ 0.0     │ Depot  │
│ 5   │ 2     │ 0     │ 1     │ 1.1     │ Parent │
│ 6   │ 2     │ 0     │ 1     │ 9.1     │ Metab  │

julia> df_wide = unstack(df, [:id, :time, :amt], :cmt, :dv)
4×6 DataFrame
│ Row │ id    │ time  │ amt   │ Depot    │ Metab    │ Parent   │
│     │ Int64 │ Int64 │ Int64 │ Float64? │ Float64? │ Float64? │
├─────┼───────┼───────┼───────┼──────────┼──────────┼──────────┤
│ 1   │ 1     │ 0     │ 1     │ 0.0      │ missing  │ missing  │
│ 2   │ 1     │ 1     │ 0     │ missing  │ 8.2      │ 1.2      │
│ 3   │ 2     │ 0     │ 1     │ 0.0      │ missing  │ missing  │
│ 4   │ 2     │ 1     │ 0     │ missing  │ 9.1      │ 1.1      │

julia> df_wide[!, :cmt] = ifelse.(df_wide.time .== 0, 1, missing)
4-element Array{Union{Missing, Int64},1}:
 1
  missing
 1
  missing

julia> df_wide[!, :evid] = ifelse.(df_wide.time .== 0, 1, 0)
4-element Array{Int64,1}:
 1
 0
 1
 0

julia> select!(df_wide, Not(:Depot))
4×7 DataFrame
│ Row │ id    │ time  │ amt   │ Metab    │ Parent   │ cmt     │ evid  │
│     │ Int64 │ Int64 │ Int64 │ Float64? │ Float64? │ Int64?  │ Int64 │
├─────┼───────┼───────┼───────┼──────────┼──────────┼─────────┼───────┤
│ 1   │ 1     │ 0     │ 1     │ missing  │ missing  │ 1       │ 1     │
│ 2   │ 1     │ 1     │ 0     │ 8.2      │ 1.2      │ missing │ 0     │
│ 3   │ 2     │ 0     │ 1     │ missing  │ missing  │ 1       │ 1     │
│ 4   │ 2     │ 1     │ 0     │ 9.1      │ 1.1      │ missing │ 0     │

julia> df_wide
4×7 DataFrame
│ Row │ id    │ time  │ amt   │ Metab    │ Parent   │ cmt     │ evid  │
│     │ Int64 │ Int64 │ Int64 │ Float64? │ Float64? │ Int64?  │ Int64 │
├─────┼───────┼───────┼───────┼──────────┼──────────┼─────────┼───────┤
│ 1   │ 1     │ 0     │ 1     │ missing  │ missing  │ 1       │ 1     │
│ 2   │ 1     │ 1     │ 0     │ 8.2      │ 1.2      │ missing │ 0     │
│ 3   │ 2     │ 0     │ 1     │ missing  │ missing  │ 1       │ 1     │
│ 4   │ 2     │ 1     │ 0     │ 9.1      │ 1.1      │ missing │ 0     │