R – updating a package

8 Jul

So, I have learned through trial and error that the easiest way to update an R package seems to be:

– Download and install R-dev locally.
– Build and check the package –as-cran using this.
– Submit to CRAN if OK.

– update github as follows:
git clone xyz
# move to directory
git add -A
git commit -m “todaysDate”
git checkout
git push

– update R-forge:
# get the command from the ‘SCM’ tab of the project e.g.
svn checkout svn+ssh://cdardis@r-forge.r-project.org/svnroot/logisticdx/
# move to directory containing pkg
svn add /pkg/
svn commit –message “todaysDate”

cv with LaTeX

21 Jun

Had to redo my cv recently.
In case this is helpful for others, I enclose a link to the source files.
It uses XeLaTeX to compile to .pdf (allows for a greater range of fonts).
A link to the output can be found below:

Sample cv

The required res.cls can be found here.

As the code doesn’t display well here on WordPress, I have uploaded the files to a ‘gist’ on github here.

Cox model coefficients in Rcpp::Eigen

20 Aug

Recently decided to see if the original .C Cox fitter used in
package:survival could be improved by re-implementing it using package:RcppEigen , in a similar way to the speedups possible with for e.g. lm() .
Unfortunately, it seems to be just marginally faster.
Still needs some tidying up, but will post here in case someone spots an obvious improvement. The current function implements only the Breslow method for handling ties…

// int main(int argc, char** argv) {
//
// RcppExport SEXP test_e1() {
//
// [[Rcpp::depends(RcppEigen)]]
#include <Eigen/Dense>
#include 
#include 
#include 
using namespace std;
using namespace Eigen;
using namespace Rcpp;
typedef Eigen::Map MapMatd;
// [[Rcpp::export]]
Rcpp::List e2(const Eigen::ArrayXi & stat1,
	      const Eigen::ArrayXd & t1,
	      const MapMatd & x2,
	      int maxiter=10
	      ) {
  //
  // iteration
  int iter=1; 
  int halving=0; 
  double newlk=0;
  double sct=0;
  ArrayXXd x1=x2.array();
  // ArrayXXd x1(6, 2);
  // x1 << 1 , 1 , 1 , 0 , 1 ;
  //      0, 0, 0, 1, 0, 0;
  cout << "x1" << x1 <<endl;
  int nvar1=x1.cols();
  int nused1=x1.rows();
  cout << "cols " << nvar1 << " rows " << nused1 <<endl;
  // ArrayXi stat1(nused1);
  // stat1 << 1, 0, 1, 1, 0, 1;
  // ArrayXd t1(nused1);
  // t1 << 1, 1, 6, 6, 8, 9;
  // total deaths
  int td1=stat1.sum();
  // cout << "tdeaths" << td1 << endl;
  // offset
  // ArrayXd off1=ArrayXd::Constant(nused1, 0);
  // weights
  // ArrayXd wei1=ArrayXd::Constant(nused1, 1);
  // strata 
  ArrayXi str1=ArrayXi::Constant(nused1, 0);
  // 
  ArrayXd a1=ArrayXd::Zero(nvar1);
  ArrayXd ones=ArrayXd::Constant(nvar1, 1);
  ArrayXd a2=ArrayXd::Zero(nvar1);
  ArrayXd cov=ArrayXd::Zero(nvar1);
  ArrayXd temp1=ArrayXd::Zero(nvar1);
  ArrayXd temp2=ArrayXd::Zero(nvar1);
  ArrayXd u1=ArrayXd::Zero(nvar1);
  ArrayXd u2=ArrayXd::Zero(nvar1);
  ArrayXd scale1=ArrayXd::Zero(nvar1);
  ArrayXd beta=ArrayXd::Zero(nvar1);
  ArrayXd newB=ArrayXd::Zero(nvar1);
  ArrayXd maxB=ArrayXd::Zero(nvar1);
  ArrayXXd cmat1=ArrayXXd::Zero(nvar1, nvar1);
  ArrayXXd cmat2=ArrayXXd::Zero(nvar1, nvar1);
  ArrayXXd imat1=ArrayXXd::Zero(nvar1, nvar1);
  ArrayXXd imat2=ArrayXXd::Zero(nvar1, nvar1);
  //
  double c1=0; 
  //
  // cout << nvar1 << " columns" << endl;
  // cout << nused1 << " rows" << endl;
  // cout << "x1\n" << x1.transpose() << endl;
  // iterators
 int i=0, j=0, k=0;
 // VectorXd means1(1, nvar1);
 // means1=x1.matrix().colwise().mean();
 // cout << "means1\n" << means1 << endl;
 // means1=means1 / nused1;
 // x1.matrix().rowwise() -= means1.transpose();
 // cout << "scaledx1\n" << x1.transpose() << endl;
 //
 // last stratum is one
 str1(nused1-1)=1;
 //cout << "strata" << str1.transpose() << endl;
 // log likelihood
 Array2d loglik=Array2d::Zero(2);
 cout << "\nINITIAL ITERATION";  //  double denom;  double risk;  double zB=0;  // observation  int obs1=nused1-1;  double dt1;  int nrisk=0;  int ndead=0;  double deadwt=0;  for (obs1=(nused1 - 1); obs1 >= -1; ) {
 // cout <<  "obs1for=" << obs1 << " "; 
   if (obs1==-1) break;
   if (str1(obs1) == 1) {
     nrisk=0;  
     denom=0;
     a1=ArrayXd::Zero(nvar1);
     cmat1=ArrayXXd::Zero(nvar1, nvar1);
   };
   dt1=t1(obs1);
   cout <<  "\n\ndt1=" << dt1 << endl;     nrisk=0;    ndead=0;    deadwt=0;    while(t1(obs1) == dt1 && obs1 >= -1) {
     cout <<  "obs=" << obs1 << " "; 
     nrisk++;
     cov=x1.row(obs1);
     cout << "beta=" << beta.transpose() << " ";
     cout << "cov=" << cov.transpose() << " ";
     a2=beta * cov;
     //zB += a2.sum();
     zB += (beta * cov).sum();
     cout <<  "zB=" << zB << " "; 
     risk=exp(zB);
     cout <<  "risk=" << risk << " "; 
     denom += risk;
     cout <<  "denom=" << denom << " "; 
     a1 += cov * risk;
     cout <<  "a1=" << a1.transpose() << " "; 
     cout <<  "cmat1=" << cmat1 << " "; 
     for (i=0; i<nvar1; i++) {
       for (j=0; j<=i; j++){ 
	 cmat1(j, i) += (risk * x1(obs1, i) * x1(obs1, j));
       }}
     cout <<  "cmat1=" << cmat1 << endl;
     // if death/event occurs
     if (stat1(obs1)==1) {
       ndead++;
       deadwt++;
       loglik(1) = loglik(1) + zB;
       cout <<  "loglik1=" << loglik(1) << " "; 
       //cout << " x1.row(obs1).array()= " << x1.row(obs1).array() << " ";
       u1 += x1.row(obs1);
       cout <<  "u1=" << u1 << " "; 
     }
  //cout << "obs1whilebef=" << obs1 << " "; 
     obs1--;
     if (obs1==-1) break;
 //cout << "o1whileaft=" << obs1 << " ";       if (str1(obs1)==1) break;    }    if(ndead>0){
   cout << "ndead=" << ndead << " "; 
   loglik(1) -= deadwt * log(denom);
   cout <<  "loglik= " << loglik(1) << " "; 
   temp2=a1 / denom;  
   cout <<  "temp2= " << temp2.transpose() << " "; 
   u1=u1 - temp2 * deadwt;
   cout<<"u1= " << u1.transpose() << endl;
   for (i=0; i<nvar1; i++) {
     for (j=0; j<=i; j++){
       imat1(i, j) += deadwt * (cmat1(j, i) - temp2(j) * a1(i))/denom;
     }}
   cout << "imat=" << imat1 << endl;
   }
 }
 cout << "\n\nEND accumulation loop \n";
 loglik(0) = loglik(1);
 cout <<  "ll=" << loglik << endl; 
 maxB = 20 * ( (imat1.matrix().diagonal().array()/td1).sqrt() );
 cout << "imat=" << imat1 << " ";
 cout << "maxB=" << maxB.transpose() << endl;
 a1=u1;
 cout << "a1=" << a1 << " CHOLESKY SOLVE 2 ";
 a1.matrix() = imat1.matrix().ldlt().solve(a1.matrix());
 cout << "a1=" << a1 << " ";
 cout << "u1=" << u1 << " ";
 sct = (a1 * u1).sum();
 cout << "st/temp=" << sct << " ";
 newB=beta + a1;
 cout << "newB=" << newB << endl << endl;
 //
 if (maxiter==0) {
   cout << "imat=" << imat1;
   imat1.matrix()=imat1.matrix().inverse();
   goto finish;
 }
 //
 //
 // MAIN LOOP
 //
 //
 halving=0;
 temp2=ArrayXd::Zero(nvar1);
 a1=ArrayXd::Zero(nvar1);
 a2=ArrayXd::Zero(nvar1);
 cout << "***MAIN LOOP***" << endl;
 for (iter=1; iter <= maxiter; iter++) {
   cout << "\n \n ITER= " << iter << " \n \n";
   newlk=0;
   cout << "newlk= " << newlk << " ";
   u1=ArrayXd::Zero(nvar1);
   cout << " u1 = " << u1.transpose() << " ";
   imat1=ArrayXXd::Zero(nvar1, nvar1);
   cout << " imat1= " << imat1 << " ";    for (obs1=(nused1 - 1); obs1 >= -1; ) {
     cout << " obs1= " << obs1 << " ";
     if (obs1==-1) break;
     if (str1(obs1)==1) {
       nrisk=0;  
       denom=0;
       a1=ArrayXd::Zero(nvar1);
       cout << " a1= " << a1.transpose() << " ";
       cmat1=ArrayXXd::Zero(nvar1, nvar1);
       cout <<  "cmat1=" << cmat1 << " "; 
     };
 dt1=t1(obs1);
 cout <<  "\n\ndt1=" << dt1 << endl;     nrisk=0;    ndead=0;    deadwt=0;    while(t1(obs1)==dt1 && obs1 >= -1) {
     cout <<  "\n obs1=" << obs1 << " "; 
     nrisk++;
     cov=x1.row(obs1).array();
     cout << "newB=" << newB.transpose() << " ";
     cout << "cov=" << cov.transpose() << " ";
     a2=newB * cov;
     cout << "a2=" << a2.transpose() << " ";
     zB = 0;
     zB += a2.sum();
     cout <<  "zB=" << zB << " "; 
     risk=exp(zB);
     cout <<  "risk=" << risk << " "; 
     denom += risk;
     cout <<  "denom=" << denom << " "; 
     cout <<  "(cov*risk)=" << (cov * risk) << " "; 
     a1 += (cov * risk);
     cout <<  "a1=" << a1.transpose() << " "; 
     for (i=0; i<nvar1; i++) {
       for (j=0; j<=i; j++){ 
	 cmat1(j, i) += (risk*x1(obs1, i) * x1(obs1, j));
       }}
     cout <<  " cmat1=" << cmat1 << endl; 
     if (stat1(obs1)==1) {
       ndead++;
       deadwt++;
       newlk += zB;
       cout <<  "newlk=" << newlk << " "; 
       cout <<  "u1=" << u1.transpose() << " "; 
       cout <<  " x1.row(obs1)=" << x1.row(obs1).transpose() << " "; 
       u1 += x1.row(obs1);
       cout <<  " u1=" << u1.transpose() << " "; 
     }
//cout << "obs1whilebef=" << obs1 << " "; 
     obs1--;
     if (obs1==-1) break;
//cout << "o1whileaft=" << obs1 << " "; 
     if (str1(obs1)==1) break;
   }
   cout << "ndead=" << ndead << " ";  if(ndead>0){
   newlk = newlk - deadwt*log(denom);
   cout <<  "\nnewlk=" << newlk << " "; 
   temp2=a1 / denom;  
   cout <<  "temp2=" << temp2.transpose() << " deadwt " << deadwt << " "; 
   cout<< " u1=" << u1.transpose() << endl;
   u1 -= temp2 * deadwt;
   cout<< "u1=" << u1.transpose() << endl;
   for (i=0; i<nvar1; i++) {
     for (j=0; j<=i; j++){
        imat1(i, j) += deadwt*(cmat1(j, i) - temp2(j)*a1(i))/denom;
     }}
   cout << "imat1=" << imat1 << endl;
   }
   } //cout << "\n\nEND ITER\n";


 cout << "loglik=" << loglik << "\nnewlk=" << newlk << "\n";
 if (fabs(1-(loglik(1)/newlk)) <=  0.00001
     && halving==0){
   loglik(1) = newlk;
   cout << "imat=" << imat1 << " CHOLESKY INVERT 2 ";
   imat1.matrix()= imat1.matrix().ldlt().solve(ones.matrix());
   cout << "imat=" << imat1 << " ";
   goto finish;
 }
if (iter==maxiter) break;
//
 if (newlk < loglik(1)) { 
   /*it is not converging ! */
   cout << "\n\nNOT CONVERGING" << endl;
   halving =1;
   newB=(newB+beta)/2;
 } else { 
   halving=0;
   loglik(1) = newlk;
   cout << " u1=" << u1.transpose();
   u1.matrix()= imat1.matrix().ldlt().solve(u1.matrix());
   cout << " u1=" << u1.transpose();
   j=0;
   cout << " beta=" << beta.transpose() << " newB=" << newB.transpose() << " ";
   beta=newB;
   newB += u1;
   for (i=0; i<nvar1; i++) {      if (newB(i) > maxB(i)) newB(i) = maxB(i);
     else if (newB(i) < -maxB(i)) newB(i) = -maxB(i);
   }
 }
 // another iteration
 }
 //
 cout << "\n\nRAN OUT OF ITERATIONS";
 loglik(1)=newlk;
 imat1.matrix() = imat1.matrix().inverse();
 // 
 finish:
 cout<< "\n\nFINISH \n newB=" << newB << endl;
 cout<< "beta=" << beta << endl;
 cout << "imat=" << imat1 << " ";
 cout << "imat2=" << imat2 << " ";
 cout << "sct=" << sct << " ";
 cout << "u1=" << u1 << endl;
 cout << "loglik=" << loglik << endl;
 cout << "iter=" << iter << endl;
 // 
return Rcpp::List::create(Rcpp::Named("outer")=1);
}
// return 0;
//}

setup Ubuntu with R, Texlive, emacs etc. using shell script

13 Jun

I have had to configure Ubuntu from scratch a couple of times so want to record the steps necessary to get things set up. I’m trying to keep most of it in one .sh file so that all commands can be executed sequentially from there…

First, with username = “c”

sudo adduser c sudo

Logout + in.

sudo apt-get install emacs

sudo emacs

(Manually) put this in ~/.emacs.d/init.el to allow execution of shell scripts line-by-line
(modified from work by @Koshke):

###
### run shell code line by line
### 
(defun sh-send-line-or-region (&optional step)
  (interactive ())
  (let ((proc (get-process "shell"))
        pbuf min max command)
    (unless proc
      (let ((currbuff (current-buffer)))
        (shell)
        (switch-to-buffer currbuff)
        (setq proc (get-process "shell"))
        ))
    (setq pbuff (process-buffer proc))
    (if (use-region-p)
        (setq min (region-beginning)
              max (region-end))
      (setq min (point-at-bol)
            max (point-at-eol)))
    (setq command (concat (buffer-substring min max) "\n"))
    (with-current-buffer pbuff
      (goto-char (process-mark proc))
      (insert command)
      (move-marker (process-mark proc) (point))
      ) ;;pop-to-buffer does not work with save-current-buffer -- bug?
    (process-send-string  proc command)
    (display-buffer (process-buffer proc) t)
    (when step 
      (goto-char max)
      (next-line))
    ))
### 
(defun sh-send-line-or-region-and-step ()
  (interactive)
  (sh-send-line-or-region t))
(defun sh-switch-to-process-buffer ()
  (interactive)
  (pop-to-buffer (process-buffer (get-process "shell")) t))
### 
(add-hook 'sh-mode-hook (lambda nil
 (local-set-key [C-tab] 'sh-switch-to-process-buffer)))
(add-hook 'sh-mode-hook (lambda nil
 (local-set-key [(shift return)] 'sh-send-line-or-region-and-step)))
### 
###----------------------------------------
### 
### check version and
### if 64-bit OS
lsb_release -a
/bin/uname -m
### check display 
spci | grep -E "VGA"
xrandr
### 
### 
### modify .bashrc (non-login shell options)
### 
### remap CAPSLOCK to CTRL 
setxkbmap -option ctrl:nocaps
### sed adds newline if not starting on one
sed -i -e '$a\' ~/.bashrc
### echo >> adds to end of file
echo "setxkbmap -option ctrl:nocaps" >> ~/.bashrc
### change prompt for ease of display
sed -i -e '$a\' .bashrc
echo 'PS1="\u@\w>"' >> .bashrc
export PS1="\u@\w>"
### change permission recursively 
chmod -R a+rwX .emacs.d
### 
### make sure all hard drives mounted
### 
gnome-disks
### check UUID (unique universal IDs + labels)
blkid
### mount additional disks on startup by default
### use gnome disks to check default 
### name/ location/ filesystem
### maybe easier to do the following using GUI 
### from gnome-disks
cat <> /etc/fstab
/dev/sdb1 /media/RAID/ ext4 defaults 0 2
EOF
### 
### modify EMACS startup
### 
### cat > ~/emacs.d/init.el <<EOF 
cat <>  ~/.emacs.d/init.el
;;; start in fullscreen
;;;
(custom-set-variables
 '(initial-frame-alist (quote ((fullscreen . maximized)))))
;;;
;;; F11 toggle fullscreen
;;;
(defun toggle-fullscreen ()
  "Toggle full screen on X11"
  (interactive)
  (when (eq window-system 'x)
    (set-frame-parameter
     nil 'fullscreen
     (when (not (frame-parameter nil 'fullscreen)) 'fullboth))))
(global-set-key [f11] 'toggle-fullscreen)
;;;
;;; inhibit startup messages
(setq inhibit-startup-screen  '(1))
;;;
;;; open at home directory 
(setq default-directory "~")
(setq initial-buffer-choice "~")
;;;
;;; y/n for yes/no
(defalias 'yes-or-no-p 'y-or-n-p)
;;;
;;; C-tab to switch windows
;;;
(global-set-key [C-S-tab] 
    (lambda ()
      (interactive)
      (other-window -1)))
(global-set-key [C-tab] (lambda ()
      (interactive)
      (other-window)))
;;;
;;; kbd shortcuts
;;;
;;; F6 for ;;;
(fset 'sc ";;; ")
(add-hook ''emacs-lisp-mode-hook
              '(lambda ()
                 (local-set-key (kbd "") 'sc )))
;;;
;;; F7 for hyphens in Latex
(fset 'pHyphens "%%%----------------------------------------\r")
(add-hook 'LaTeX-mode-hook
              '(lambda ()
                 (local-set-key (kbd "") 'pHyphens )))
;;;
;;; F8 for ###
(fset '3hash "### ")
(global-set-key (kbd "") '3hash)
;;;
;;; F9 for ###----------------------------------------
(fset 'hyphens "###----------------------------------------\r")
(global-set-key (kbd "") 'hyphens)
;;;
EOF
### 
###----------------------------------------
### restart sudo emacs
###----------------------------------------
### 
### icicles
### 
sed -i -e '$a\' /etc/apt/sources.list
echo "deb http://us.archive.ubuntu.com/ubuntu trusty main universe" >> /etc/apt/sources.list
sed -i -e '$a\' /etc/apt/sources.list
apt-get update
apt-get install -y icicles
cat <>  ~/.emacs.d/init.el
;;; icicles
;;;
 (add-to-list 'load-path "/my/path/to/icicles/")
 (require 'icicles)
 (icy-mode 1)
EOF
### 
### curl
### 
apt-get -y install curl
apt-get -y update
### 
### git
apt-get -y install git
git config --global user.name "c"
git config --global user.email "christopherdardis@gmail.com"
git config --list
### 
### pymacs
### 
cd ~/.emacs.d/ 
git clone https://github.com/gabrielelanaro/emacs-for-python
cat <>  ~/emacs.d/init.el
;;;
;;; emacs for python
;;; 
(load-file "~/.emacs.d/emacs-for-python/epy-init.el")
EOF
### 
### aspell
### 
apt-get -y install aspell aspell-en
### 
### texlive (vanilla)
### 
apt-get -y install wget
mkdir ~/texinst
cd ~/texinst
wget http://mirror.ctan.org/systems/texlive/tlnet/install-tl-unx.tar.gz
tar -xzf install-tl-unx.tar.gz
cd inst*
sudo ./install-tl
### this can take > 1 hour !
### make note of year no. (current=2013)
### make symlink
sudo ln -s /usr/local/texlive/2013/bin/* /opt/texbin
### edit path
### change to e.g.
### PATH="/opt/texbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
p1=$( overwrites contents
echo $p2 > "/etc/environment"
### 
###----------------------------------------
### logout + in (reload environment)
###----------------------------------------
### 
### run this as local user (not sudo)
### to check install has worked
# which tex
### 
### fake packages
sudo apt-get -y install equivs --no-install-recommends
mkdir /tmp/tl-equivs 
cd /tmp/tl-equivs
equivs-control texlive-local
### edit this for year and person details
cat <>  ~/texlive-local
Section: misc 
Priority: optional
Standards-Version: 3.9.4 
Package: texlive-local
Version: 2013-1
Maintainer: c 
Provides: chktex, biblatex, biblatex-dw, cm-super, cm-super-minimal, context, 
 dvidvi, dvipng, feynmf, fragmaster, jadetex, lacheck, latex-beamer, 
 latex-cjk-all, latex-cjk-chinese, latex-cjk-chinese-arphic-bkai00mp, 
 latex-cjk-chinese-arphic-bsmi00lp, latex-cjk-chinese-arphic-gbsn00lp, 
 latex-cjk-chinese-arphic-gkai00mp, latex-cjk-common, latex-cjk-japanese, 
 latex-cjk-japanese-wadalab, latex-cjk-korean, latex-cjk-thai, latexdiff, 
 latexmk, latex-sanskrit, latex-xcolor, lcdf-typetools, lmodern, luatex, 
 musixtex, passivetex, pgf, preview-latex-style, prosper, ps2eps, psutils, 
 purifyeps, t1utils, tex4ht, tex4ht-common, tex-gyre, texlive, texlive-base, 
 texlive-bibtex-extra, texlive-binaries, texlive-common, texlive-extra-utils,
 texlive-fonts-extra, texlive-fonts-extra-doc, texlive-fonts-recommended,
 texlive-fonts-recommended-doc, texlive-font-utils, texlive-formats-extra,
 texlive-games, texlive-generic-extra, texlive-generic-recommended,
 texlive-humanities, texlive-humanities-doc, texlive-lang-african,
 texlive-lang-all, texlive-lang-arabic, texlive-lang-cjk, texlive-lang-cyrillic,
 texlive-lang-czechslovak, texlive-lang-english, texlive-lang-european,
 texlive-lang-french, texlive-lang-german, texlive-lang-greek, 
 texlive-lang-indic, texlive-lang-italian, texlive-lang-other, 
 texlive-lang-polish, texlive-lang-portuguese, texlive-lang-spanish,
 texlive-latex-base, texlive-latex-base-doc, texlive-latex-extra, 
 texlive-latex-extra-doc, texlive-latex-recommended, 
 texlive-latex-recommended-doc, texlive-luatex, texlive-math-extra, 
 texlive-metapost, texlive-metapost-doc, texlive-music,
 texlive-omega, texlive-pictures, texlive-pictures-doc, texlive-plain-extra,
 texlive-pstricks, texlive-pstricks-doc, texlive-publishers,
 texlive-publishers-doc, texlive-science, texlive-science-doc, texlive-xetex,
 thailatex, tipa, tipa-doc, xindy, xindy-rules, xmltex 
Architecture: all
Description: My local installation of TeX Live 2013.
 A full 'vanilla' TeX Live 2013
 http://tug.org/texlive/debian#vanilla
EOF
### 
equivs-build texlive-local
dpkg -i texlive-local_2013-1_all.deb
### PERL
apt-get -y install perl-tk --no-install-recommends
### setup
/opt/texbin/tlmgr --gui
### modify EMACS 
cat <>  ~/.emacs.d/init.el
;;;
;;; Tex commands
;;; 
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(add-hook 'LaTeX-mode-hook 'visual-line-mode)
(add-hook 'LaTeX-mode-hook 'flyspell-mode)
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
(add-hook 'LaTeX-mode-hook 'turn-on-reftex)
(setq reftex-plug-into-AUCTeX t)
(setq TeX-PDF-mode t)
(load "auctex.el" nil t t)
(load "preview-latex.el" nil t t)
EOF
### AUCTEX
aptitude install auctex
### 
### 
### R
### 
### add newline
sed -i -e '$a\' /etc/apt/sources.list
### change name of version after ubuntu below
echo "deb http://cran.revolutionanalytics.com/bin/linux/ubuntu trusty/" >> /etc/apt/sources.list 
### add key
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
### install
apt-get update
apt-get build-dep -y r-base-core
apt-get update
###----------------------------------------
### logout + in (reload sources)
###----------------------------------------
apt-get install -y r-base
apt-get install -y r-base-dev
### ESS
apt-get install ess
### dependencies (not exhaustive):
### for rgl
apt-get install -y libglu1-mesa-dev
### for RCurl
apt-get install -y libcurl4-openssl-dev
### for rjags
apt-get install jags
### multiple packages need XML
apt-get install -y libxml2-dev
### some packages may not be available for most recent 
### verson of R and should be installed manually first
### e.g. gam for R 3.0.2
### first ensure all users have read/write to 
### default R library
chmod -R a+rwX /usr/local/lib/R/site-library
cd /usr/local/lib/R/site-library
### -x is eXtract; -z is decompress (if using gZip)
curl http://cran.r-project.org/src/contrib/gam_1.09.1.tar.gz | tar -xz
cd ~
### GDAL http://www.gdal.org/
add-apt-repository -y ppa:ubuntugis/ppa 
apt-get update
apt-get install -y gdal-bin
### confirm working
ogrinfo
### 
apt-get install -y gsl
### install all packages
cat <> ~/all.R
local({r <- getOption("repos")
       r["CRAN"] <- "http://cran.r-project.org"
       options(repos=r)
})
pack <- available.packages()
packnames <- paste(pack[, 1], sep=", ")
for (i in 1:length(packnames)){
    install.packages(packnames[i])
}
EOF
###  modify emacs
cat <>  ~/.emacs.d/init.el
;;;
;;; shift-enter for sending .R to *R
;;;
(setq ess-ask-for-ess-directory nil)
(setq ess-local-process-name "R")
(setq ansi-color-for-comint-mode 'filter)
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)
(defun my-ess-start-R ()
  (interactive)
  (if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
      (progn
        (delete-other-windows)
        (setq w1 (selected-window))
        (setq w1name (buffer-name))
        (setq w2 (split-window w1 nil t))
        (R)
        (set-window-buffer w2 "*R*")
        (set-window-buffer w1 w1name))))
(defun my-ess-eval ()
  (interactive)
  (my-ess-start-R)
  (if (and transient-mark-mode mark-active)
      (call-interactively 'ess-eval-region)
    (call-interactively 'ess-eval-line-and-step)))
(add-hook 'ess-mode-hook
          '(lambda()
             (local-set-key [(shift return)] 'my-ess-eval)))
(add-hook 'inferior-ess-mode-hook
          '(lambda()
             (local-set-key [C-up] 'comint-previous-input)
             (local-set-key [C-down] 'comint-next-input)))
(add-hook 'Rnw-mode-hook
          '(lambda()
             (local-set-key [(shift return)] 'my-ess-eval)))
(require 'ess-site)
EOF
### for default library
mkdir /media/RAID/rp
###  modify .Renviron
touch /~ .Renviron
cat <>  ~/.Renviron
R_LIBS = /media/RAID/rp
EOF
###  modify .Rprofile
touch /~ .Rprofile
cat <>  ~/.Rprofile
### hard code the US repo for CRAN
r <- getOption("repos")             
r["CRAN"] <- "http://cran.us.r-project.org"
options(repos = r)
rm(r)
### 
.First <- function(){
   cat("\n Rrrr! The statistics program for Pirates !\n\n")
   .libPaths("/media/RAID/rp")
   if(interactive()) {
        require(survival)
        require(data.table)
        require(ggplot2)
        require(devtools)
        require(utils)
        options(error=recover)
    }
}
### 
### based on postings by Petr Pikal and David Hinds to the r-help list in 2004
### modified by: Dirk Eddelbuettel 
### (http://stackoverflow.com/questions/1358003/tricks-to-manage-the-available-memory-in-an-r-session) 
### I then gave it a few tweaks 
### (show size as megabytes and use defaults that I like)
### returns a data frame of the objects and their associated storage needs.
### 
.ls.objects <- function (pos=1, pattern, order.by="Size", decreasing=TRUE, head=TRUE, n=10) {
  napply <- function(names, fn) sapply(names, function(x)
          fn(get(x, pos = pos)))
  names <- ls(pos = pos, pattern = pattern)
  obj.class <- napply(names, function(x) as.character(class(x))[1])
  obj.mode <- napply(names, mode)
  obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
  obj.size <- napply(names, object.size) / 10^6 # megabytes
  obj.dim <- t(napply(names, function(x)
            as.numeric(dim(x))[1:2]))
  vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
  obj.dim[vec, 1] <- napply(names, length)[vec]
  out <- data.frame(obj.type, obj.size, obj.dim)
  names(out) <- c("Type", "Size", "Rows", "Columns")
  out <- out[order(out[[order.by]], decreasing=decreasing), ]
  if (head)
    out <- head(out, n)
  out
}
### 
EOF
### best to let this run overnight..
Rscript all.R
### R manuals
apt-get update -y
apt-get install -Gy evince
apt-get install -y xpdf
mkdir /media/RAID/lib
mkdir /media/RAID/lib/rman
touch ~/packPdf.R
cat <>  ~/packPdf.R
p1 <- available.packages()
p1 <- paste(p1[ ,1], sep=", ")
for (i in seq_along(p1)){
    u1 <- paste0("http://cran.r-project.org/web/packages/",
                 p1[i], "/", p1[i], ".pdf")
    f1 <- paste0("/media/RAID/lib/rman/", p1[i], ".pdf")
    download.file(u1, f1, mode="wb")
}
EOF
### takes around 2-3h
Rscript packPdf.R
### cleanup
mkdir ~/setup
mv  ~/all.R ~/setup/all.R
mv  ~/packPdf.R ~/setup/packPdf.R
mv ~/texlive-local*.deb ~/setup/
rm  ~/*.*~
### 
### Mendeley
### 
### get this from:
### http://www.mendeley.com/repositories/ubuntu/stable/amd64/mendeleydesktop-latest
### easiest to install with Ubuntu Software Center
### 
### armadillo
### 
### first two should already be installed with R
apt-get install -y liblapack-dev
apt-get install -y libblas-dev
apt-get install -y libboost-dev
apt-get install -y libarmadillo-dev
### 
### copy home files at shutdown
mkdir /media/RAID/c
touch /etc/rc6.d/K99copyHome.sh
cat <>  /etc/rc6.d/K99copyHome.sh
cp /home/c /media/RAID -R
EOF
chmod +x /etc/rc6.d/K99copyHome.sh
### copy files on reboot
touch /etc/rc0.d/K99copyHome.sh
cat <>  /etc/rc0.d/K99copyHome.sh
cp /home/c /media/RAID -R
EOF
chmod +x /etc/rc0.d/K99copyHome.sh
### handy image editor
### like MS Paint
apt-get install -y pinta
### synergy (control additional desktop)
apt-get install -y synergy
### add video card at this point
### msi Radeon HD6450
### for dual display
### backup configuration file
cp /etc/X11/xorg.conf /etc/X11/xorg.conf.BAK
### in case already installled
apt-get remove --purge fglrx fglrx-amdcccle fglrx-updates fglrx-amdcccle-updates
apt-get install fglrx fglrx-amdcccle
aticonfig --initial
reboot
### now check it's working
### connect video out to new video card
fglrxinfo
### video acceleration
apt-get install -y xvba-va-driver libva-glx1 libva-egl1 vainfo

Update R version

22 Aug

Was putting off upgrading version of R (2.15 to 3.01, on Win 7) and currently installed packages for some time, but finally got around to it and was all rather painless.

Many thanks to  Tal Galili for the code for this.

Get (unmodified) variable names from formula

31 Jul

This question came up on the R-help list, but my answer was rejected there (I think because the question was so old…)

So briefly:

Given a formula, how do we get just the variable names?

Of course it can be converted to character and manipulated as such, but there’s also this approach:

 f1 <- formula( y~cos(x)+log(z) )
 identical( all.vars(delete.response(terms(f1))), c("x","z") )

Upload package to R-forge from Windows

23 Apr

Don’t need to do this v. often and it’s a bit of a chore, so I’m making a note for later reference.

Thankfully I have already set up Tortiseshell, putty, pageant and the rest as per instructions here. This took around 4h for a first timer, including reading manuals. (This appears to be easier on Linux although I haven’t tried it).

I already have a local SSH key on this device, and am choosing to keep the same key for all packages for now.

 

For an existing package on R-forge:

My Page –> mypackage –> SCM –> [Browse Subversion Repository]

 

The easiest way I’ve found is to copy and paste the Developer Subversion Access url into the browser address bar, something like:

svn+ssh://cdardis@r-forge.r-project.org/svnroot/mypackage/

In Chrome, this will launch Tortoise and open it to this directory. Right-click and checkout locally e.g. to C:\pkg

Modify the files locally. For files which have the same name, copy and paste and agree overwrite. For files to be deleted, this needs to be done via right-click –> TortoiseSVN –> delete.

New files to be added need to have their status changed with right-click –> TortoiseSVN –> add.

Once all changes made, go back to root of this checked-out directory, C:\pkg. Then right-click –> SVN Commit.

This can take repeated efforts as connection can be lost. Typically asks for password 5-6 times per checkout/commit, so pick something short!

Can take 2-3 days to ensure passes build checks. Then is ready for to commit to CRAN.

 

This also works, for a new package:

So, I have a directory parent within which is directory mypackage.

First thing is to submit a request to R-forge to host a new package. Typically takes 1-2 days for approval.

Then navigate there in a browser to make sure it’s set up correctly (not essential). Login, then click:

My Page –> mypackage –> SCM –> [Browse Subversion Repository]

This shows the directory structure; the one to upload to is pkg/

First, ‘check this out’ to the local device. Create a new directory in parent called pkg. Right click it and click SVN Checkout. The address to ‘check out’ from is shown on the SCM tab under ‘Developer Subversion Access’; copy and paste the text into Tortoise, something like:

svn+ssh://myname@r-forge.r-project.org/svnroot/mypackage/

into the Checkout directory which is something like:

C:\parent\pkg

You’ll be prompted for a password at this point and a download occurs.

Now, working locally, copy and paste mypackage into pkg. Then right click again and hit SVN Commit. Head back to R-forge and make sure it’s there.

 

N.B. the directory structure on the R-forge side. Your /R/man and DESCRIPTION files should be uploaded to the directory pkg. Otherwise the package will not be built/checked as R-Forge won’t find the expected structure.

(First time around I put them into /pkg/mypackage/pkg/mypackage/R. This doesn’t work.)