Published
Edited
Sep 9, 2021
1 star
Insert cell
Insert cell
sample1 = await FileAttachment('sample1.csv').csv({ typed: true})
Insert cell
Insert cell
Insert cell
sample1Table = Inputs.table(sample1)
Insert cell
// funcLRT_obs = py`import numpy as np
// import pandas as pd
// def flogLRv(x, fnidot, fndotj, fndotdot):
// logLRvr = x * (np.log(x) - np.log(fnidot))
// logLRvr = logLRvr + (fndotj - x) * (np.log(fndotj - x) - np.log(fndotdot - fnidot))
// logLRvr = logLRvr - fndotj * (np.log(fndotj) - np.log(fndotdot));
// logLRvr.fillna(0)
// pj = x / fnidot
// qj = (fndotj - x) / (fndotdot - fnidot)
// logLRvr[ pj < qj ] = 0
// return logLRvr

// def fLRT(qnum, alpha = 0.05, alphaSpending = 1):
// mylist = qnum
// mylist["ntrt"] = pd.to_numeric(mylist["ntrt"])
// mylist["nidot"] = pd.to_numeric(mylist["nidot"])
// mylist["ndotj"] = pd.to_numeric(mylist["ndotj"])
// mylist["ndotdot"] = pd.to_numeric(mylist["ndotdot"])
// mylist["logLR"] = flogLRv(mylist["ntrt"], mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0])
// mylist = mylist.sort_values("logLR", ascending=False)
// return mylist

// fLRT`
Insert cell
// LRTResultObs = py`import pandas as pd
// ${funcLRT_obs}(pd.read_json(${JSON.stringify(sample1)})).to_json(orient="records")`
Insert cell
// JSON.parse(LRTResultObs)
Insert cell
LRTResultObs = await FileAttachment('sample1TableObs.csv').csv({ typed: true})
Insert cell
sample1TableObs = Inputs.table(LRTResultObs)
Insert cell
// funcLRT_sim = py`import numpy as np
// import pandas as pd
// def flogLRv(x, fnidot, fndotj, fndotdot):
// logLRvr = x * (np.log(x) - np.log(fnidot))
// logLRvr = logLRvr + (fndotj - x) * (np.log(fndotj - x) - np.log(fndotdot - fnidot))
// logLRvr = logLRvr - fndotj * (np.log(fndotj) - np.log(fndotdot));
// logLRvr.fillna(0)
// pj = x / fnidot
// qj = (fndotj - x) / (fndotdot - fnidot)
// logLRvr[ pj < qj ] = 0
// return logLRvr

// def fLRT(qnum, number, alpha = 0.05, alphaSpending = 1):
// mylist = qnum
// mylist["ntrt"] = pd.to_numeric(mylist["ntrt"])
// mylist["nidot"] = pd.to_numeric(mylist["nidot"])
// mylist["ndotj"] = pd.to_numeric(mylist["ndotj"])
// mylist["ndotdot"] = pd.to_numeric(mylist["ndotdot"])
// mylist["logLR"] = flogLRv(mylist["ntrt"], mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0])
// mylist = mylist.sort_values("logLR", ascending=False)
// cnstR = number
// np.random.seed(123)
// mylist["Pvector"] = mylist["nidot"] / mylist["ndotdot"]
// mylist["Pobs"] = mylist["ntrt"] / mylist["ndotj"]
// sim_cntdata = np.random.multinomial(mylist["ndotj"][0], mylist["Pvector"], size = cnstR)
// return sim_cntdata

// fLRT`
Insert cell
// LRTResultSim = py`import pandas as pd
// ${funcLRT_sim}(pd.read_json(${JSON.stringify(sample1)}),1000).transpose()`
Insert cell
// LRTResultSimCSV = aq.from(LRTResultSim).objects()
Insert cell
LRTResultSimData = await FileAttachment('LRTResultSimCSV@1.csv').csv({ typed: true})
Insert cell
sample1TableSim = Inputs.table(LRTResultSimData)
Insert cell
// funcLRT_simLR = py`import numpy as np
// import pandas as pd
// def flogLRv(x, fnidot, fndotj, fndotdot):
// logLRvr = x * (np.log(x) - np.log(fnidot))
// logLRvr = logLRvr + (fndotj - x) * (np.log(fndotj - x) - np.log(fndotdot - fnidot))
// logLRvr = logLRvr - fndotj * (np.log(fndotj) - np.log(fndotdot));
// logLRvr.fillna(0)
// pj = x / fnidot
// qj = (fndotj - x) / (fndotdot - fnidot)
// logLRvr[ pj < qj ] = 0
// return logLRvr

// def fLRT(qnum, number, alpha = 0.05, alphaSpending = 1):
// mylist = qnum
// mylist["ntrt"] = pd.to_numeric(mylist["ntrt"])
// mylist["nidot"] = pd.to_numeric(mylist["nidot"])
// mylist["ndotj"] = pd.to_numeric(mylist["ndotj"])
// mylist["ndotdot"] = pd.to_numeric(mylist["ndotdot"])
// mylist["RR"] = (mylist["ntrt"])*((mylist["ndotdot"]-mylist["nidot"])/(mylist["nidot"]*(mylist["ndotj"]-mylist["ntrt"])))
// mylist["stdRR"] = 1/(mylist["ntrt"]) - 1/mylist["nidot"] + 1/(mylist["ndotj"]-mylist["ntrt"]) - 1/(mylist["ndotdot"]-mylist["nidot"])
// mylist["stdRR"] = np.sqrt(mylist["stdRR"])
// mylist["lowRR"] = np.log(mylist["RR"])- 1.96*(mylist["stdRR"])
// mylist["lowRR"] = np.exp(mylist["lowRR"])
// mylist["uppRR"] = np.log(mylist["RR"])+ 1.96*(mylist["stdRR"])
// mylist["uppRR"] = np.exp(mylist["uppRR"])
// mylist["logLR"] = flogLRv(mylist["ntrt"], mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0])
// mylist = mylist.sort_values("logLR", ascending=False)
// cnstR = number
// np.random.seed(123)
// mylist["Pvector"] = mylist["nidot"] / mylist["ndotdot"]
// mylist["Pobs"] = mylist["ntrt"] / mylist["ndotj"]
// sim_cntdata = np.random.multinomial(mylist["ndotj"][0], mylist["Pvector"], size = cnstR)
// logLRsim = np.array([flogLRv(i, mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0]) for i in sim_cntdata])
// return logLRsim

// fLRT`
Insert cell
// LRTResultSimLR = py`import pandas as pd
// ${funcLRT_simLR}(pd.read_json(${JSON.stringify(sample1)}),1000)`
Insert cell
// LRTResultSimLRCSV = aq.from(LRTResultSimLR).objects()
Insert cell
LRTResultSimLRData = await FileAttachment('LRTResultSimLRCSV.csv').csv({ typed: true})
Insert cell
LRTResultSimMaxLRData = d3.transpose(LRTResultSimLRData.map(n => Object.values(n)))
Insert cell
sample1TableSimLR= Inputs.table(LRTResultSimMaxLRData)
Insert cell
// maxlogLRsim = py`import numpy as np
// import pandas as pd
// np.array([np.max(i) for i in pd.read_json(${LRTResultSimLRData})])`
Insert cell
maxlogLRsim = LRTResultSimLRData.map(n => Math.max(...Object.values(n)))
Insert cell
// T_alpha = py`import numpy as np
// np.quantile(${maxlogLRsim}, 1 - 0.05)
// `
Insert cell
T_alpha = _math.quantileSeq(maxlogLRsim, 1 - 0.05)
Insert cell
LRTResultPlot = Plot.plot({
height: 200,
y: {
grid: true
},
x: {
label: "Likelihood Ratio →"
},
marks: [
Plot.rectY(aq.table({maxLogLR:maxlogLRsim}).objects(),
Plot.binX({y: "count"},
{x: "maxLogLR",
thresholds: 30,
fill: "steelblue"
})),
Plot.ruleY([0]),
Plot.ruleX([T_alpha], {stroke: "red"}),
Plot.text([[T_alpha, 50]], {text: ['Rejection Region, LLR>'+T_alpha.toFixed(3)], dx: 18, fill:'red'}),
]
})
Insert cell
// funcLRT = py`import numpy as np
// import pandas as pd
// def flogLRv(x, fnidot, fndotj, fndotdot):
// logLRvr = x * (np.log(x) - np.log(fnidot))
// logLRvr = logLRvr + (fndotj - x) * (np.log(fndotj - x) - np.log(fndotdot - fnidot))
// logLRvr = logLRvr - fndotj * (np.log(fndotj) - np.log(fndotdot));
// logLRvr.fillna(0)
// pj = x / fnidot
// qj = (fndotj - x) / (fndotdot - fnidot)
// logLRvr[ pj < qj ] = 0
// return logLRvr

// def fLRT(qnum, number, alpha = 0.05, alphaSpending = 1):
// mylist = qnum
// mylist["ntrt"] = pd.to_numeric(mylist["ntrt"])
// mylist["nidot"] = pd.to_numeric(mylist["nidot"])
// mylist["ndotj"] = pd.to_numeric(mylist["ndotj"])
// mylist["ndotdot"] = pd.to_numeric(mylist["ndotdot"])
// mylist["logLR"] = flogLRv(mylist["ntrt"], mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0])
// mylist = mylist.sort_values("logLR", ascending=False)
// cnstR = number
// np.random.seed(123)
// mylist["Pvector"] = mylist["nidot"] / mylist["ndotdot"]
// sim_cntdata = np.random.multinomial(mylist["ndotj"][0], mylist["Pvector"], size = cnstR)
// logLRsim = np.array([flogLRv(i, mylist["nidot"], mylist["ndotj"][0], mylist["ndotdot"][0]) for i in sim_cntdata])
// maxlogLRsim = np.array([np.max(i) for i in logLRsim])
// mylist["T_alpha"] = np.quantile(maxlogLRsim, 1 - alpha)
// mylist["pvalue"] = np.array([ np.mean(maxlogLRsim > i) for i in mylist["logLR"]])
// if alphaSpending >= 1:
// mylist["is_signal"] = (mylist["logLR"] > mylist["T_alpha"])
// else:
// mylist["alpha"] = alphaSpending
// mylist["is_signal"] = (mylist["pvalue"] < alphaSpending)
// mylist = mylist.sort_values("logLR", ascending=False)
// return mylist

// fLRT`
Insert cell
// LRTResult = py`import pandas as pd
// ${funcLRT}(pd.read_json(${JSON.stringify(sample1)}),1000).to_json(orient="records")`
Insert cell
// LRTResultFinalCSV = aq.from(JSON.parse(LRTResult)).objects()
Insert cell
LRTResultFinalData = await FileAttachment('LRTResultFinalCSV.csv').csv({ typed: true})
Insert cell
sample1TableFinal = Inputs.table(LRTResultFinalData,
{columns: ["AEID", "ntrt", "nidot", "ndotj", "ndotdot", "logLR", "T_alpha", "pvalue", "is_signal"],
width: {AE: 250},
// height: 550,
align: {
AEID: "center",
ntrt: "center",
nidot: "center",
ndotj: "center",
ndotdot: "center",
logLR: "center",
T_alpha: "center",
pvalue: "center",
is_signal: "center"
}
})
Insert cell
products = await FileAttachment('AZproducts.csv').csv({ typed: true})
Insert cell
Array.from(new Set(products.map(n=>n.area)))
Insert cell
viewof areaSelector = select({
title: "Therapy Area",
// description: "Please pick your favorite stooge.",
options: Array.from(new Set(products.map(n=>n.area))),
value: Array.from(new Set(products.map(n=>n.area)))[1]
})
Insert cell
productsTable = Inputs.table(products.filter(n=>n.area === areaSelector), {
columns:["name"]
})
Insert cell
Insert cell
Insert cell
// import { py, pyodide } from '@gnestor/pyodide'
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more