Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Tutorial 9
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)

@lyy005 lyy005 Nov 10, 2017

Copy link
Copy Markdown
Owner

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


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)


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure looks good. Try to make the syntax work better
-0.5 pts



72 changes: 72 additions & 0 deletions Tutorial 9 Question 3
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')

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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):

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

initialGuess=numpy.array([1,1,1,1])
fit3a=minimize(null,initialGuess,method="Nelder-Mead",options={'disp':True},args=data)
fit3b=minimize(alter1,initialGuess,method="Nelder-Mead",options={'disp':True},args=data)
fit3c=minimize(alter2,initialGuess,method="Nelder-Mead",options={'disp':True},args=data)

#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

@lyy005 lyy005 Nov 10, 2017

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The structure is correct. Try to improve the syntax

-0.5 pts


26 changes: 26 additions & 0 deletions tutorial9question2.py
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))

@lyy005 lyy005 Nov 10, 2017

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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)


Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

-0.5 pts