Representing Parametric Survival Model in ‘Counting Process’ form in JAGS

You can use rstanarm package, which is a wrapper around STAN. It allows to use standard R formula notation to describe survival models. stan_surv function accepts arguments in a “counting process” form. Different base hazard functions including Weibull can be used to fit the model. The survival part of rstanarm – stan_surv function is still … Read more

How to set limits for axes in ggplot2 R plots?

Basically you have two options scale_x_continuous(limits = c(-5000, 5000)) or coord_cartesian(xlim = c(-5000, 5000)) Where the first removes all data points outside the given range and the second only adjusts the visible area. In most cases you would not see the difference, but if you fit anything to the data it would probably change the … Read more

Sample random rows in dataframe

First make some data: > df = data.frame(matrix(rnorm(20), nrow=10)) > df X1 X2 1 0.7091409 -1.4061361 2 -1.1334614 -0.1973846 3 2.3343391 -0.4385071 4 -0.9040278 -0.6593677 5 0.4180331 -1.2592415 6 0.7572246 -0.5463655 7 -0.8996483 0.4231117 8 -1.0356774 -0.1640883 9 -0.3983045 0.7157506 10 -0.9060305 2.3234110 Then select some rows at random: > df[sample(nrow(df), 3), ] X1 X2 … Read more

Side-by-side plots with ggplot2

Any ggplots side-by-side (or n plots on a grid) The function grid.arrange() in the gridExtra package will combine multiple plots; this is how you put two side by side. require(gridExtra) plot1 <- qplot(1) plot2 <- qplot(1) grid.arrange(plot1, plot2, ncol=2) This is useful when the two plots are not based on the same data, for example … Read more

Elegant way to check for missing packages and install them?

Yes. If you have your list of packages, compare it to the output from installed.packages()[,”Package”] and install the missing packages. Something like this: list.of.packages <- c(“ggplot2”, “Rcpp”) new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,”Package”])] if(length(new.packages)) install.packages(new.packages) Otherwise: If you put your code in a package and make them dependencies, then they will automatically be installed when you … Read more

Extracting specific columns from a data frame

You can subset using a vector of column names. I strongly prefer this approach over those that treat column names as if they are object names (e.g. subset()), especially when programming in functions, packages, or applications. # data for reproducible example # (and to avoid confusion from trying to subset `stats::df`) df <- setNames(data.frame(as.list(1:5)), LETTERS[1:5]) … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)