-
Notifications
You must be signed in to change notification settings - Fork 9
Tutorial from Amidlige and MBuynak #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #Import Packages | ||
| import pandas | ||
| import numpy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
| import re | ||
| import os | ||
| from plotnine import * | ||
|
|
||
| os.chdir("/Users/madelinebuynak/Desktop") | ||
|
|
||
| #load data | ||
| data = pandas.read_csv("ponzr1.csv",header=0,sep='\t') | ||
|
|
||
| subset=data.loc[data.mutation.isin(['WT','I231N']),:] | ||
|
|
||
| #two likelihood functions | ||
| def nllike(p,obs): | ||
| B0=p(0) | ||
| B1=p(1) | ||
| sigma=p(2) | ||
|
|
||
| def null (p,obs): | ||
| B0=p[0] | ||
| sigma=p[1] | ||
|
|
||
| expected=B0 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| def alter (p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[2] | ||
|
|
||
| expected=B0+B1*obs.x | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| initialGuess=numpy.array([1,1,1]) | ||
| fitNull=minimize(nllike,initialGuess,method="Nelder-Mead",options={'disp':True},args=subset) | ||
| fitAlter=minimize(nllike,initialGuess,method="Nelder-Mead",options={'disp':True},args=subset) | ||
|
|
||
|
|
||
| subset=data.loc[data.mutation.sin(['WT','V456D']),:] | ||
|
|
||
|
|
||
| def null (p,obs): | ||
| B0=p[0] | ||
| sigma=p[1] | ||
|
|
||
| expected=B0 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| def alter (p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[2] | ||
|
|
||
| expected=B0+B1*obs.x | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| initialGuess=numpy.array([1,1,1]) | ||
| fitNull=minimize(null,initialGuess,method="Nelder-Mead",options={'disp':True},args=subset) | ||
| fitAlter=minimize(alter,initialGuess,method="Nelder-Mead",options={'disp':True},args=subset) | ||
|
|
||
| D=2*(fitNull.fun-fitAlter.fun) | ||
| 1-ch2.cdf(x=D,df=1) | ||
|
|
||
| #M124K: p-value ~ 0.72 (no effect of treatment) | ||
|
|
||
| #V456D: p-value ~ 5.6e-6 (effect of treatment) | ||
|
|
||
| #I213N: p-value ~ 0.88 (no effect of treatment) | ||
|
|
||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The structure looks good. Try to make the syntax work better |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #Import Packages | ||
| import pandas | ||
| import numpy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
| import re | ||
| import os | ||
| from plotnine import * | ||
|
|
||
| os.chdir("/Users/madelinebuynak/Desktop") | ||
|
|
||
| #load data | ||
| data=pandas.read_csv("leafDecomp.csv",header=0,sep='\t') | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. data=pandas.read_csv("leafDecomp.csv") |
||
|
|
||
| subset=data.loc[data.mutation.isin(['WT','I231N']),:] | ||
|
|
||
|
|
||
| #plot data | ||
| plot = ggplot(data, aes(y = 'Ms', x = 'decomp')) | ||
| plot + geom_jitter(colour='black') | ||
|
|
||
| #quadratic | ||
|
|
||
| #define 3 custom likelihood functions | ||
| def nllike(p,obs): | ||
| B0=p(0) | ||
| B1=p(1) | ||
| sigma=p(2) | ||
|
|
||
| def null (p,obs): | ||
| B0=p[0] | ||
| sigma=p[1] | ||
|
|
||
| expected=B0 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| def alter (p,obs): | ||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[2] | ||
|
|
||
| expected=B0+B1*obs.x | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| def alter (p,obs): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You want to name the function different than the previous one |
||
| B0=p[0] | ||
| B1=p[1] | ||
| sigma=p[3] | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. B2=p[2] |
||
|
|
||
| expected=B0+B1*obs.x+B2*obs.x^2 | ||
| nll=-1*norm(expected,sigma).logpdf(obs.y).sum() | ||
| return nll | ||
|
|
||
| #Estimate the Parameters | ||
| #Initial Guess for Model 1 (mean~590) | ||
| #Initial Guess for Model 2 (intercept~200 slope~6.33) | ||
|
|
||
| initialGuess=numpy.array([1,1,1]) | ||
| fitNull=minimize(nllike,initialGuess,method="Nelder-Mead",options={'disp':True},args=subset) | ||
| fitAlter=minimize(nllike,initialGuess,method="Nelder-Mead",options={'disp':True},args=subset) | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. initialGuess=numpy.array([1,1,1,1]) |
||
| #Compare Models | ||
| #likelihood test | ||
| D=2*(fitNull.fun-fitAlter.fun) | ||
| 1-ch2.cdf(x=D,df=1) | ||
|
|
||
|
|
||
| #quadratic model is the best fit, but linear model is better than constant model | ||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The structure is correct. Try to improve the syntax -0.5 pts |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #Import Packages | ||
| import pandas | ||
| import numpy | ||
| from scipy.optimize import minimize | ||
| from scipy.stats import norm | ||
| import re | ||
| import os | ||
| from plotnine import * | ||
|
|
||
| data = pandas.read_csv("mMarinumGrowth.csv",header=0,sep='\t') | ||
|
|
||
| def Monod(p,obs): | ||
| uMax=p[0] | ||
| K=p[1] | ||
| sigma=p[2] | ||
|
|
||
| expected=uMax* (x/(x+K)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected=uMax* (obs.S/(obs.S+K)) |
||
| nll=-1+norm(expected,sigma).logpdf(obs.y).sum | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nll=-1+norm(expected,sigma).logpdf(obs.u).sum |
||
| return nll | ||
|
|
||
| initialGuess=numpy.array([1,1,1]) | ||
| fit-minimize(Monod,initialGuess,method="Monod Equation",options={'disp':True},args=data) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fit=minimize(Monod,initialGuess,method="Nelder-Mead",options={'disp':True},args=data) |
||
|
|
||
| print(fit.x) | ||
|
|
||
|
|
||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. -0.5 pts |
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
subset=ponzr.loc[data.mutation.isin(['WT','M124K']),:]
input=pandas.DataFrame({'y':subset.ponzr1Counts,'design':0})
input.loc[subset.mutation=='M124K','design']=1
M124Knull=minimize(null, initialGuess,method="Nelder-Mead",args=subset)
M124Ktrt=minimize(alter, initialGuess,method="Nelder-Mead",args=subset)
1-chi2.cdf(x=2*(M124Knull.fun-M124Ktrt.fun),df=1)
Same for the other two mutations