# Fix data error,

**URL:** <https://discourse.pumas.ai/t/fix-data-error/59>\
**Category:** Uncategorized\
**Created:** [November 11, 2019, 6:13pm UTC](https://discourse.pumas.ai/t/fix-data-error/59 "2019-11-11T18:13:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai\_matcha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/sai_matcha/32/297_2.png) [@sai\_matcha](https://discourse.pumas.ai/u/sai_matcha)\
**Post date:** [November 11, 2019, 6:13pm UTC](https://discourse.pumas.ai/t/fix-data-error/59/1 "2019-11-11T18:13:52Z")

</div>

hi all  
i tried the below codes and not able to fit the data at end

```auto
DR1 = DosageRegimen(50,time=0,cmt=2)

#DR2 = DosageRegimen(100,time=0,cmt=2)

#DR3 = DosageRegimen(200,time=0,cmt=2)

#s1 = Subject(id=1,evs = DR1, cvs = (wt=70,))

#s2 = Subject(id=2, evs = DR2, cvs= (wt=60,))

#s3 = Subject(id=3,evs=DR3, cvs = (wt=65,))

choose_covariates() = (WT = rand(55:80),)

#pop = Population([s1,s2,s3])

pop = Population(map(i -> Subject(id = i,evs = DR1, cvs = choose_covariates()),1:24))

pop

##############################

mymodel = @model begin
  @param begin
    tvcl ∈ RealDomain(lower=0, init = 1.0)
    tvv ∈ RealDomain(lower=0, init = 5)
    tvQ ∈ RealDomain(lower = 0, init= 1)
    tvv2 ∈ RealDomain(lower = 0, init = 10)
    Ω ∈ PDiagDomain(init=[0.09])
    σ_prop ∈ RealDomain(lower=0,init=0.04)
  end
#end
  @random begin
    η ~ MvNormal(Ω)
  end
#end
  @pre begin
    CL = tvcl * exp(η[1])
    V = tvv 
    V2 = tvv2
    Q = tvQ
  end
#end
 #@covariates WT
#end
  #@dynamics ImmediateAbsorptionModel
    @dynamics begin
        # Depot' = -Ka*Depot
            Central' = Q*(Peripheral-Central) - (CL/V)*Central
        
         Peripheral' = Q*(Central-Peripheral)
    end
#end

  @derived begin
      cp = @. 1000*(Central / V)
      DV ~ @. Normal(cp,sqrt(cp^2*σ_prop))
    end
end
#####

  
param = init_param(mymodel)

obs = simobs(mymodel, pop, param, obstimes=0:1:72)
plot(obs)

simdf = DataFrame(obs)
first(simdf, 6)

simdf.route = "IV"

timeu = u"hr"
concu = u"mg/L"
amtu = u"mg"

ncadf = read_nca(simdf, id=:id, time=:time, conc=:cp, amt=:amt,
    route=:route,timeu=timeu, concu=concu, amtu=amtu, lloq=0.4concu)

plot(ncadf)

simdf.cmt = ifelse.(ismissing.(simdf.cmt), 2, simdf.cmt)
est_df = simdf[.!((simdf.DV .== 0.0) .& (simdf.cmt .==2)),:]
first(est_df,6)

data = read_pumas(est_df ,cvs = [:WT], dvs=[:DV])

res = fit(mymodel,data,param,Pumas.FOCEI())

```

error is  
Typeerror: in type assert, expected forward diff.dual … and many things

 ![fit%20error](https://canada1.discourse-cdn.com/flex030/uploads/pumas/original/1X/1ee054ada19f169ade6112993d98c38cb8a2094d.png)

---

<div class="post-metadata">

**Author:** ![vijay](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/vijay/32/96_2.png) [@vijay](https://discourse.pumas.ai/u/vijay)\
**Post date:** [November 11, 2019, 8:29pm UTC](https://discourse.pumas.ai/t/fix-data-error/59/2 "2019-11-11T20:29:24Z")

</div>

@sai_matcha I see two immediate reasons why this did not run.

1. `DR1` doses the drug into the peripheral compartment in a 2 compartment IV bolus model that is written up. This is incorrect. it should be `DR1 = DosageRegimen(50,time=0,cmt=1)`
2. It is preferred to use all lower case variable names in Pumas (dv as opposed to DV)

```auto
  @derived begin
      cp = @. 1000*(Central / V)
      dv ~ @. Normal(cp,sqrt(cp^2*σ_prop))
    end

```

Fixing these two, gives me a successful run. Please try it out and let us know.

Best,  
Vijay

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/sai_matcha/32/297_2.png) [@sai\_matcha](https://discourse.pumas.ai/u/sai_matcha)\
**Post date:** [November 12, 2019, 2:33pm UTC](https://discourse.pumas.ai/t/fix-data-error/59/3 "2019-11-12T14:33:06Z")

</div>

Hi.  
i tried running the same codes after changing to cmt=1, DV to dv…  
but this time im stuck at nca itself…

```auto
DR1 = DosageRegimen(50,time=0,cmt=1)

#DR2 = DosageRegimen(100,time=0,cmt=2)

#DR3 = DosageRegimen(200,time=0,cmt=2)

#s1 = Subject(id=1,evs = DR1, cvs = (wt=70,))

#s2 = Subject(id=2, evs = DR2, cvs= (wt=60,))

#s3 = Subject(id=3,evs=DR3, cvs = (wt=65,))

choose_covariates() = (wt = rand(55:80),)

#pop = Population([s1,s2,s3])

pop = Population(map(i -> Subject(id = i,evs = DR1, cvs = choose_covariates()),1:24))

pop

##############################

mymodel = @model begin
  @param begin
    tvcl ∈ RealDomain(lower=0, init = 1.0)
    tvv ∈ RealDomain(lower=0, init = 5)
    tvQ ∈ RealDomain(lower = 0, init= 1)
    tvv2 ∈ RealDomain(lower = 0, init = 10)
    Ω ∈ PDiagDomain(init=[0.09])
    σ_prop ∈ RealDomain(lower=0,init=0.04)
  end
#end
  @random begin
    η ~ MvNormal(Ω)
  end
#end
  @pre begin
    CL = tvcl * exp(η[1])
    V = tvv 
    V2 = tvv2
    Q = tvQ
  end
#end
 #@covariates WT
#end
  #@dynamics ImmediateAbsorptionModel
    @dynamics begin
        # Depot' = -Ka*Depot
            Central' = Q*(Peripheral-Central) - (CL/V)*Central
        
         Peripheral' = Q*(Central-Peripheral)
    end
#end

  @derived begin
      cp = @. 1000*(Central / V)
      dv ~ @. Normal(cp,sqrt(cp^2*σ_prop))
    end
end
#####

  
param = init_param(mymodel)

obs = simobs(mymodel, pop, param, obstimes=0:1:72)
plot(obs)

simdf = DataFrame(obs)
first(simdf, 6)

simdf.route = "IV"

timeu = u"hr"
concu = u"mg/L"
amtu = u"mg"

ncadf = read_nca(simdf, id=:id, time=:time, conc=:cp, amt=:amt,
    route=:route,timeu=timeu, concu=concu, amtu=amtu, lloq=0.4concu) 

```

 ![nca%20error](https://canada1.discourse-cdn.com/flex030/uploads/pumas/original/1X/0a338f535f30e16edda8f81a3bf668a7432ffe96.png)

---

<div class="post-metadata">

**Author:** ![sai\_matcha](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.pumas.ai/sai_matcha/32/297_2.png) [@sai\_matcha](https://discourse.pumas.ai/u/sai_matcha)\
**Post date:** [November 12, 2019, 2:53pm UTC](https://discourse.pumas.ai/t/fix-data-error/59/4 "2019-11-12T14:53:11Z")

</div>

![nca%20error](https://canada1.discourse-cdn.com/flex030/uploads/pumas/original/1X/0a338f535f30e16edda8f81a3bf668a7432ffe96.png)
