Wednesday, November 12, 2014

Sigmoids

Link to the Rmd file on GitHub

Sigmoid Function

\(\frac{p_1}{(1+e^{-p_2 \times (x - p_3)})}\)

p1 is the full scale from 0 to p1. For logistic regression it is 1. In the later examples it will be hardcoded as 1.

p2 is the sharpness of change.

p3 x position of the 50% height.

sigmoid = function(x, p1, p2, p3) {p1 / (1 + exp(-p2 * (x - p3)))}
curve(sigmoid(x, p1=1, p2=2, p3=-3), from=-10, to=+10)
abline(v=-3, lty=2)

plot of chunk chunk1


Sigmoid that starts at zero.

Sometimes we want to convert a parameter that varies only as \((0,+\infty)\) instead of \((-\infty,+\infty)\). First, to convert x form \((0,+\infty)\) to \((-\infty,+\infty)\) we’ll take the log. The formular for the sigmoid/logistic curve will be transformed as…

\(\frac{1}{(1+e^{-p_2 \times (log(\frac{x}{p_3}))})}\)

sigmoid2 = function(x, p2, p3) {1 / (1 + exp(-p2 * (log(x/p3))))}
curve(sigmoid2(x, p2=20, p3=3), from=0.01, to=+10)
abline(v=3, lty=2)

plot of chunk chunk1a


Transformed Positive Sigmoid

If we move p2 inside the log followed by logarithm exponentiation we’ll get

\(\frac{1}{1+(\frac{p_3}{x})^{p_2}}\)

sigmoid3 = function(x, p2, p3) {1 / (1 + (p3/x)^p2)}
curve(sigmoid3(x, p2=20, p3=3), from=0.01, to=+10)
abline(v=3, lty=2)
# smoother
curve(sigmoid3(x, p2=4, p3=3), from=0.01, to=+10, add=T, col='navyblue')

plot of chunk chunk1b

In case we need to model an opposite dependency the trend has to be reversed. For example we want to formalize a probablity of chemical compound being missed in analysis as a function of intensity.

\(\frac{1}{1+(\frac{x}{p_3})^{p_2}}\)

sigmoid3 = function(x, p2, p3) {1 / (1 + (x/p3)^p2)}
curve(sigmoid3(x, p2=20, p3=3), from=0.01, to=+10)
abline(v=3, lty=2)
# smoother
curve(sigmoid3(x, p2=4, p3=3), from=0.01, to=+10, add=T, col='navyblue')

plot of chunk chunk1c


Arctangent

Another way of modeling sigmoid dependency.

\(\frac{2}{\pi} \times arctan((x-p3) \times p2)\)

The notation of parameters as above:

p2 - shaprness

p3 - position of 50% change

sigmoid2 = function(x, sharpness, threshold) (2/pi)*atan((x-threshold)*sharpness)
curve(sigmoid2(x, sharpness=1, threshold=2), from=-10, to=+10)
curve(sigmoid2(x, sharpness=3, threshold=2), from=-10, to=+10, add=TRUE, col='blue')
abline(v=2)

plot of chunk chunk2

Friday, June 13, 2014

GitHub Notes to ... myself

Downloading github repo into a local folder
git clone https://github.com/vladpetyuk/MSnID.git
Assuming I am using RStudio, start a new project from existing directory. In this example that would be MSnID.

How to update your fork, so it catches up with original?
It is discussed here Basically issue a pull request where is the base is your own repository and head is the original that your forked from. Note that the default is the opposite: base - the original, head - you. You have to switch it using the drop-down menu.

An example of handling gist from GitHub
  • create directory where the gists are going to be stored
  • git clone https://gist.github.com/8607045.git
  • the new directory 8607045 will be created
  • cd 8607045
  • git add .
  • edit the file
  • to check what is going on: git status
  • git commit -m "your commit message"
  • git push


Pushing local repo to GitHub
Described here. Basically in my case, when git already exists. I need to

  • Create a new repository on GitHub. Do not initialize the new repository with anything yet! (in example below - simulant.LCMS)
  • git remote add origin https://github.com/vladpetyuk/simulant.LCMS.git
  • git remote -v
  • git push -u origin master


  • Greyed out push and pull in Rstudio
    fix explained here

    Friday, January 24, 2014

    Mimicking Object-Oriented Behavior with R Functions (that is Closures)

    Actually, there is already a number of ways and packages object-oriented semantics implemented in R. It is nicely summarize in the Table 1 of this article. Let's assume for a moment this diversity of object-oriented approaches in R is not enough. There is an unbearable urge to implement yet another one. Well... let's exploit the fact that functions in R are not functions in a C++ or even Python sense. They are closures. Put in a simple terms it is a function that packaged up with some state. That state (environment in case of R) may contain variables and functions. A couple of other posts on a somewhat relevant topic that I may suggest are here and here.

    Monday, December 16, 2013

    Updating values in hierarchical lists

    How to parse and update a hierarchical list using an elegant single command? A lot of things can be done with a single command in R. This (after a few considerations) turned out not an exception either.

    Friday, December 6, 2013

    Fixing the blurry R fonts caused by DPI scaling on Windows 7

    R does not play well with high DPI scaling on Windows 7.  Fonts look blurry on my 27 inch WHDQ monitor with 150% DPI scaling.  Fixing that for 32-bit R was easy.  Go to the properties of C:\Program Files\R\R-3.0.2\bin\i386\Rgui.exe. Note, you may need to replace the R version number in that path. Then open the "Compatibility" tab check the box "Disable display scaling on high DPI settings" and you are done. Once you try to do the same for 64-bit counterpart C:\Program Files\R\R-3.0.2\bin\x64\Rgui.exe you'll see the grayed out check box that can not be altered. The solutions requires editing the registry and is described here
    http://superuser.com/questions/355915/vista-dpi-scaling-cannot-disable-dpi-scaling-for-wireshark/355953#355953?s=97656833-4e08-4a55-8773-1fe68b5fca77
    Brief instructons.
    Go to
    HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers
    Most likely you'll already see a setting for 32-bit Rgui.exe
    Make a similar one by right clicking on the "Layer" and add new string value.
    Name: C:\Program Files\R\R-3.0.2\bin\x64\Rgui.exe
    Type: REG_SZ
    Data: HIGHDPIAWARE
    

    Saturday, November 16, 2013

    Exploring S4 Objects in R


    Looking what is inside of a particular method.
    Method - spectra
    Class - MSnExp
    If method is exported (check NAMESPACE) then this will work.
    getMethod("spectra","MSnExp")
    
    Method Definition:
    function (object) 
    {
        sl <- as.list(assayData(object))
        fnames <- featureNames(object)
        return(sl[fnames])
    }
    
    Signatures:
            object  
    target  "MSnExp"
    defined "MSnExp"
    

    Showing slots for a given class
    showClass("MSnExp")
    

    showMethods(classes="MSnExp", where="package:MSnbase")
    
    The link to check http://missingreadme.wordpress.com/2011/01/04/how-to-see-the-code-for-s4-methods-in-r/
    Debugging
    trace(show, tracer=browser, signature=c(x="eSet"), where=getNamespace("Biobase"))
    

    Thursday, November 7, 2013

    Where are the R libraries?

    on Mac
    /Library/Frameworks/R.framework/Versions/3.0/Resources/library
    on Win
    c:/Users/vlad/R/win-library/3.0

    Thursday, November 22, 2012

    Passing-by-reference in R

    collection of links to debates/tricks on passing-by-reference in R. http://stackoverflow.com/questions/2603184/r-pass-by-reference
    http://www.stat.berkeley.edu/~paciorek/computingTips/Pointers_passing_reference_.html
    https://stat.ethz.ch/pipermail/r-devel/2009-January/051899.html
    http://homepage.stat.uiowa.edu/~luke/R/references.html
    http://r.789695.n4.nabble.com/Pass-By-Value-Questions-td2331565.html

    My take on that is to use new Reference Classes if there is a need for complex mutable objects.
    Use of environments to pass-by-reference is considered as a hack.
    E.g. (from Robert Gentleman's book)
    e1 = new.env()
    f = function(x) {x+z}
    enironment(f) = e1
    e1$z = 10
    f(1)
    Output is 11
    Note, however, after deleting e1, f does not go away.
    rm(e1)
    f is still around

    Second point is to take a look at data.frame by pass-by-reference using "ref" and "plyr" packages.

    Checking font availability on Mac   fc-list : family | grep "Fira Code"   or   system_profiler -json SPFontsDataType | grep \...