Thursday, December 29, 2011

vim virtualedit

set virtualedit=onemore so that some mapping still works when the cursor is at the end of file. 

Friday, December 9, 2011

jags data format and openbugs data format

From JAGS manual:

There is no need to transpose matrices and arrays when transferring data between R and JAGS,
since JAGS stores the values of an array in "column major" order, like R and FORTRAN (i.e.
filling the left-hand index first).
If you have an S-style data file for WinBUGS and you wish to convert it for JAGS, then
use the command bugs2jags, which is supplied with the coda package.



Data can be in an S-Plus format (see most of the examples ) or, for data in arrays, in rectangular format.

Missing values are represented as NA.

The whole array must be specified in the file - it is not possible just to specify selected components. Any parts of the array you do not want to specify must be filled with NAs.

All variables in a data file must be defined in a model, even if just left unattached to the rest of the model. 

S-Plus format: This allows scalars and arrays to be named and given values in a single structure headed by key-word list. There must be no space after list.

For example, in the Rats example, we need to specify a scalar 
xbar , dimensions and , a vector and a two-dimensional array with 30 rows and 5 columns. This is achieved using the following format:

         list(
               xbar = 22, N = 30, T = 5,
               x = c(8.0, 15.0, 22.0, 29.0, 36.0),
               Y = structure(
                     .Data = c(
                           151, 199, 246, 283, 320,
                           145, 199, 249, 293, 354,
                           ...................
                           ...................
                           137, 180, 219, 258, 291,
                           153, 200, 244, 286, 324),
                     .Dim = c(30, 5)
                     )
         )

OpenBUGS reads data into an array by filling the right-most index first, whereas the S-Plus program fills the left-most index first. Hence OpenBUGS reads the string of numbers c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) into a 2 * 5 dimensional matrix in the order   
   [i, j]th element of matrix      value
      [1, 1]      1
      [1, 2]      2
      [1, 3]      3
      .....      ..
      [1, 5]      5
      [2, 1]      6
      .....      ..
      [2, 5]      10

whereas S-Plus reads the same string of numbers in the order

   [i, j]th element of matrix      value
      [1, 1]      1
      [2, 1]      2
      [1, 2]      3
      .....      ..
      [1, 3]      5
      [2, 3]      6
      .....      ..
      [2, 5]      10

Hence the ordering of the array dimensions must be reversed before using the S-Plus dput function to create a data file for input into 
OpenBUGS .

For example, consider the 2 * 5 dimensional matrix

         1   2   3   4   5
         6   7   8   9   10

This must be stored in S-Plus as a 5 * 2 dimensional matrix:

> M
[,1] [,2]
[1,] 1 6
[2,] 2 7
[3,] 3 8
[4,] 4 9
[5,] 5 10

The S-Plus command

> dput(list(M=M), file="matrix.dat")

will then produce the following data file

list(M = structure(.Data = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), .Dim=c(5,2))

Edit the .Dim statement in this file from .Dim=c(5,2) to .Dim=c(2,5). The file is now in the correct format to input the required 2 * 5 dimensional matrix into 
OpenBUGS .

Now consider a 3 * 2 * 4 dimensional array

         1   2   3   4
         5   6   7   8

         9   10   11   12
         13   14   15   16

         17   18   19   20
         21   22   23   24

This must be stored in S-Plus as the 4 * 2 * 3 dimensional array:

> A
, , 1
[,1] [,2]
[1,] 1 5
[2,] 2 6
[3,] 3 7
[4,] 4 8

, , 2
[,1] [,2]
[1,] 9 13
[2,] 10 14
[3,] 11 15
[4,] 12 16

, , 3
[,1] [,2]
[1,] 17 21
[2,] 18 22
[3,] 19 23
[4,] 20 24

The command

> dput(list(A=A), file="array.dat")

will then produce the following data file

list(A = structure(.Data = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1 
6, 17, 18, 19, 20, 21, 22, 23, 24), .Dim=c(4,2,3)) 

Edit the .Dim 
statement in this file from .Dim=c(4,2,3) to .Dim=c(3,2,4). The file is now in the correct format to input the required 3 * 2 * 4 dimensional array into OpenBUGS in the order 

Friday, November 4, 2011

vim syntax support for JAGS

I modified the syntax file for R to support jags (http://mcmc-jags.sourceforge.net/).  The problem is that I do not know how to write this type of file.  When I picked up how the syntax file should be defined, I would polished it. 

Download the file (jags.vim) from http://goo.gl/FSmD2

Tuesday, October 25, 2011

Wednesday, October 5, 2011

sqlite3 concatenate a column by group

there is function:
group_concat(X)

group_concat(X,Y)

So we can easily from 
The group_concat() function returns a string which is the concatenation of all non-NULL values of X. If parameter Y is present then it is used as the separator between instances of X. A comma (",") is used as the separator if Y is omitted. The order of the concatenated elements is arbitrary.
So we can easily from say, we have table tname as 

id|attr
1|black
1|female
2|male
2|white
3|other

to by ``select id, group_concat(attr) from tname group by id'':

1|black,female
2|white,male
3|other


Friday, September 30, 2011

sqlite3 attach a database

using attach, we can deal multiple databases.
an example

attach database 'a.db' as a;


vim search ignore case

I like to keep it case sensitive most of the time.  However, sometimes, if I do not like to, see

In a nutshell, we can use 
/\cfootbar 

Wednesday, September 28, 2011

sqlite3 describe table

it is not as in SQL by using describe table, it is the following instead.

.schema ?TABLE?        Show the CREATE statements
                                    If TABLE specified, only show tables matching
                                    LIKE pattern TABLE.

====
all the information obtained by .help in sqlite3:

.backup ?DB? FILE      Backup DB (default "main") to FILE
.bail ON|OFF           Stop after hitting an error.  Default OFF
.databases             List names and files of attached databases
.dump ?TABLE? ...      Dump the database in an SQL text format
                         If TABLE specified, only dump tables matching
                         LIKE pattern TABLE.
.echo ON|OFF           Turn command echo on or off
.exit                  Exit this program
.explain ?ON|OFF?      Turn output mode suitable for EXPLAIN on or off.
                         With no args, it turns EXPLAIN on.
.header(s) ON|OFF      Turn display of headers on or off
.help                  Show this message
.import FILE TABLE     Import data from FILE into TABLE
.indices ?TABLE?       Show names of all indices
                         If TABLE specified, only show indices for tables
                         matching LIKE pattern TABLE.
.load FILE ?ENTRY?     Load an extension library
.log FILE|off          Turn logging on or off.  FILE can be stderr/stdout
.mode MODE ?TABLE?     Set output mode where MODE is one of:
                         csv      Comma-separated values
                         column   Left-aligned columns.  (See .width)
                         html     HTML <table> code
                         insert   SQL insert statements for TABLE
                         line     One value per line
                         list     Values delimited by .separator string
                         tabs     Tab-separated values
                         tcl      TCL list elements
.nullvalue STRING      Print STRING in place of NULL values
.output FILENAME       Send output to FILENAME
.output stdout         Send output to the screen
.prompt MAIN CONTINUE  Replace the standard prompts
.quit                  Exit this program
.read FILENAME         Execute SQL in FILENAME
.restore ?DB? FILE     Restore content of DB (default "main") from FILE
.schema ?TABLE?        Show the CREATE statements
                         If TABLE specified, only show tables matching
                         LIKE pattern TABLE.
.separator STRING      Change separator used by output mode and .import
.show                  Show the current values for various settings
.stats ON|OFF          Turn stats on or off
.tables ?TABLE?        List names of tables
                         If TABLE specified, only list tables matching
                         LIKE pattern TABLE.
.timeout MS            Try opening locked tables for MS milliseconds
.width NUM1 NUM2 ...   Set column widths for "column" mode
.timer ON|OFF          Turn the CPU timer measurement on or off

Friday, September 16, 2011

rjags on ubuntu -- nonzero exit status

on ubuntu 10.10 and rjags 3.x, I could still have this problem.

On 05/27/2010 05:23 AM, Scott Brogan wrote:
> I've not been able to install rjags successfully in Ubuntu 10.04 :
>
> ** testing if installed package can be loaded
> Error : .onLoad failed in loadNamespace() for 'rjags', details:
> call: dyn.load(file, DLLpath = DLLpath, ...)
> error: unable to load shared library
> '/home/scott/R/i486-pc-linux-gnu-library/2.11/rjags/libs/rjags.so':
> libjags.so.2: cannot open shared object file: No such file or directory
> ERROR: loading failed
>
> I've tried with different versions of both JAGS& rjags; lastly with R
> 2.11.0, JAGS 2.1.0,& rjags_2.1.0-4. Could anyone help ?
>

Scott,

I also had trouble installing rjags in 10.04. I got the same error you
did, even though I could see the .so files. The fix (I think, I tried
lots of things) was to run 'ldconfig', then launch R and install the
package. Apparently, my jags install did not update the libraries.

Hope this helps,
Michael

cd/dvd driver

I have this problem as described in 

in this forum, this is the final solution.  

OK now is the truth. This is the solution. I assume all of you is using USB drive to install. When Windows is asking for driver, just click Cancel. You will be brought back to the welcome screen. At the welcome screen, remove your USB drive, insert it back to DIFFERENT USB PORT. Click Install Now again. The installation process will be like usual.....
Thanks,the problem fixed whit your methods...nice job :)

My comments:
What is microsoft doing?  make sense? does not make sense?  


Wednesday, September 14, 2011

ubuntu maverick lib64

On Ubuntu maverick: 
there is not /usr/local/lib64 link to /usr/local/lib 

so 

sudo ln -s /usr/local/lin /usr/local/lib64 

ubuntu sudo

it seems that the path environment is not influenced by changes in /etc/profile when using sudo 

old ubuntu (maverick), new R

add the cran apt source repository to /etc/apt/sources.list
see
http://cran.r-project.org/bin/linux/ubuntu/README.html

in which there is a part about security. 



Friday, September 9, 2011

grep --color=always

It would work (with color) when there is a more after grep. 
grep foo --color=always | more 

Monday, July 4, 2011

clang another good c compiler

It took me several minutes and googling to remember it is `clang', another c compiler. 

Sunday, July 3, 2011

obtain a figure file list from a tex file using grep and sed: new version

#!/bin/bash 

# to get a list of pdf files included in a tex file 
# echo "Usage: $0 {file.name}" 
# echo "Processing file $1"


EXPECTED_ARGS=1
E_BADARGS=65

if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` {tex.file}"
  exit $E_BADARGS
fi

tmpfile=`mktemp -p . exttmp.XXXX`

grep includegraphics $1 > $tmpfile
# sed -e 's/includegraphics/  /' -e 's/[\\{}]//g' -e 's/\[.*\]//g' -e 's/[^ ]\+//' -e 's/^ \+//' < $tmpfile 

# this one is much better than the above 
sed -e 's/.*{\(.*\.pdf\)}.*/\1/' < $tmpfile

if [ -f "$tmpfile" ]
then
    rm "$tmpfile"
fi

tmpfile=`mktemp -p . exttmp.XXXX`

grep includegraphics $1 > $tmpfile
sed -e 's/includegraphics/  /' -e 's/[\\{}]//g' -e 's/\[.*\]//g' -e 's/[^ ]\+//' -e 's/^ \+//' < $tmpfile

if [ -f "$tmpfile" ]
then
    rm "$tmpfile"
fi

#
# the includegraphics must be at the same line with the pdf or other figure files included. 

R mathplot plotmath

it is ?plotmath to get the help in R. 

You cannot imagine that at some time point, you would forget this. 

Friday, July 1, 2011

pwdx - current working directory of a process

  pwdx - report current working directory of a process

Vim file type detection: only R for .r and .R

copied from 
http://biostat.mc.vanderbilt.edu/wiki/Main/RVim

Filetype detection

Vim includes syntax highlighting for two other languages that use the .r and .R file extensions. Vim searches up to the first 50 lines of a file and checks the comment syntax to determine whether it is R or Rexx. If no comments are found, it defaults to Rexx. This can be annoying for starting a new script. If you want .r and .R to ALWAYS be associated with R syntax, put au BufNewFile,BufRead .r,.R  setf r in your ~/.vim/filetype.vim (alternatively, you can use the same line in~/.vim/ftdetect/r.vim).

=====

Under ubuntu maverick, I would just change file /usr/share/vim/vim72/filetype.vim 


Thursday, June 30, 2011

obtain a figure file list from a tex file using grep and sed

tmpfile=`mktemp -p . exttmp.XXXX`

grep includegraphics $1 > $tmpfile
sed -e 's/includegraphics/  /' -e 's/[\\{}]//g' -e 's/\[.*\]//g' -e 's/[^ ]\+//' -e 's/^ \+//' < $tmpfile

if [ -f "$tmpfile" ]
then
    rm "$tmpfile"
fi

#
# the includegraphics must be at the same line with the pdf or other figure files included. 

Sunday, May 29, 2011

imapsync

https://fedorahosted.org/released/imapsync/

sync mails from two mail addresses

Saturday, March 12, 2011

R environment

assign("7", "Mountain West", envir = confnameenv);


grep files from results by find

 find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 644 {} \;
 This command changes the permissions of all files with a name ending in .mp3 in the directory /var/ftp/mp3. The action is carried out by specifying the option -exec chmod 644 {} \; in the command. For every file whose name ends in .mp3, the command chmod 644 {} is executed replacing {} with the name of the file. The semicolon (backslashed to avoid the shell interpreting it as a command separator) indicates the end of the command. Permission 644, usually shown as rw-r--r--, gives the file owner full permission to read and write the file, while other users have read-only access. In some shells, the {} must be quoted.
 copied from http://en.wikipedia.org/wiki/Find

Sunday, February 6, 2011

sqlite3 scripts comments notation

--

-- ********************************************************************
-- Creating a trigger for timeEnter
-- Run as follows:
-- $ sqlite3 test.db < trigger1
-- ********************************************************************
CREATE TRIGGER insert_t1_timeEnter AFTER INSERT ON t1
BEGIN
UPDATE t1 SET timeEnter = DATETIME('NOW') WHERE rowid = new.rowid;
END;
-- ********************************************************************

Sunday, January 30, 2011

sqlite dump

copied from http://stackoverflow.com/questions/75675/how-do-i-dump-the-data-of-some-sqlite3-tables

You don't say what you wish to do with the dumped file.

I would use the following to get a CSV file, which I can import into almost everything

.mode csv 
.header on 
.out file.dmp 
select * from emp;

If you want to reinsert into a different SQLite database then:

.mode insert 
.out file.sql 
select * from em

compile sqlite3 to be a shared library


gcc -c -fPIC sqlite3.c
# gcc -shared -o libsqlite3.so -fPIC sqlite3.o 
gcc -shared -o libsqlite3.so -fPIC sqlite3.o -ldl -lpthread

Wednesday, January 12, 2011

cd 2 iso on mac

mac, cd to iso 

hdiutil makehybrid -iso -joliet -o Master.is XXXX

XXXX is the folder that might show on the desktop

pdfcrop2 absolute mode

using pdfcrop2 under absolute mode,
you can crop from any rectangular area from a pdf file: paper size - margins