Fit values for a CFA with a single factor can only be computed with models with more than 3 observed variables. With 3 obeserved variables and one factor you have to compute 6 free parameters (with one fixed). But your covariance matrix has also 6 nonredundant elements. Hence you get df=0 degress of freedom. A chi-squared distribution with df=0 is constant 0, hence we can expect the test statistic to be 0.

# Little letters are also important

In the PISA 2012 Technical Report on page 312 (chapter 16), the formula (16.3) for the Partial Credit Model is printed for computing scale parameters. Looking at it twice, one can see that something must be wrong with the subscribs used for the sumations.

By consulting the chapter “Polytomous Rasch Models and their Estimation” in the book “Rasch Models” (Fischer, 1995), and combining the formulas (15.3) with (15.8) there, we get

Now by comparing the meaning of all the symbols and applying it on the formula from PISA Technical Report – by respecting as much as possible of the notations used by them – we get

We see that we have to change in the numerator of the very first formula above to , and to .

That makes sense!

# A Simple Method of Sample Size Calculation for Logistic Regression

This posting is based on the paper “A simple method of sample size calculation for linear and logistic regression” by F. Y. Hsieh et al., which can be found under http://dx.doi.org/10.1002/(SICI)1097-0258(19980730)17:14<1623::AID-SIM871>3.0.CO;2-S .

We consider the case where we want to calculate the sample size for a multiple logistic regression with continous response variable and with continous covariates.

Fomula (1) in the paper computes the required sample size for a simple logistic regression, given the effect size to be tested, the event rate at the mean of the (single) covariate, level of significance, and required power for the test. This formula is implemented in the function SSizeLogisticCon() from R package “powerMediation” and can easily be applied.

For the multiple case, Hsieh et al. introduce the variance inflation factor (VIF), with which the sample size for the simple case can be inflated to get the sample size for the multiple case. I have implemented it as R function:

1 2 3 4 5 6 7 8 9 10 |
## p1: the event rate at the mean of the predictor X ## OR: expected odds ratio. log(OR) is the change in log odds ## for an increase of one unit in X. ## beta*=log(OR) is the effect size to be tested ## r2: r2 = rho^2 = R^2, for X_1 ~ X_2 + ... + X_p ssize.multi <- function(p1, OR, r2, alpha=0.05, power=0.8) { n1 <- SSizeLogisticCon(p1, OR, alpha, power) np <- n1 / (1-r2) return(np) } |

Another approximation for the simple case is given in Formula (4), and is based on formulae given by A. Whittemore in “Sample size for logistic regression with small response probability”. I have implemente the simple case,

1 2 3 4 5 6 7 8 9 10 11 12 13 |
## p1: as above ## p2: event rate at one SD above the mean of X ssize.whittemore <- function (p1, p2, alpha = 0.05, power = 0.8) { beta.star <- log(p2*(1-p1)/(p1*(1-p2))) za <- qnorm(1 - alpha/2) zb <- qnorm(power) V0 <- 1 Vb <- exp(-beta.star^2 / 2) delta <- (1+(1+beta.star^2)*exp(5*beta.star^2 / 4)) * (1+exp(-beta.star^2 / 4))^(-1) n <- (V0^(1/2)*za + Vb^(1/2)*zb)^2 * (1+2*p1*delta) / (p1*beta.star^2) n.int <- ceiling(n) return(n.int) } |

and the multiple case.

1 2 3 4 5 6 |
## all parameters as above ssize.whittemore.multi <- function(p1, p2, r2, alpha=0.05, power=0.8) { n1 <- ssize.whittemore(p1, p2, alpha, power) np <- n1 / (1-r2) return(np) } |

The complete R script, the examples from the paper included, can be found under http://rpubs.com/candrea/ssizelogreg .

# New R Package “betas”

In social science it is often required to compute standardized regression coefficients – called beta coefficients, or simply betas. These betas can be interpreted as effects, and thus are independent of the original scale.

There are two approaches how you can obtain betas. You Z-standardize the data before fitting or you compute the betas after fitting the model.

The first approach has flaws if you have non-numeric variables in your data or if you intent to incorporate interaction terms in your model.

The second approach is a way more convenient, but until now there was no R package helping you compute betas for as many kinds of models as you needed. For example with lm.beta() from “QuantPsyc” Package you cannot handle models with factors with more than two levels.

The features of the “betas” R package are (so far for v0.1.1):

Compute standardized beta coefficients and corresponding standard errors for the following models:

- linear regression models with numerical covariates only
- linear regression models with numerical and factorial covariates
- weighted linear regression models
- all these linear regression models with interaction terms
- robust linear regression models with numerical covariates only

You can install the package from CRAN (http://cran.r-project.org/web/packages/betas/):

1 2 |
install.packages("betas") library(betas) |

The package is maintained on GitHub: https://github.com/andreaphsz/betas .

Feel free to report issues: https://github.com/andreaphsz/betas/issues .

Enjoy hassle-free computations of betas in R!

# Robust Standardized Beta Coefficients

Standardized beta coefficients are definded as

beta = b * sd_x/sd_y

where b are the coefficients from OLS linear regression, and sd_x and sd_y are standard deviations of each x variable and of y.

In the case where you performe a robust linear regression, sd_x and sd_y seems not be very meanigfull anymore, because variances and hence standard deviations are not robust. The R package “robust” provides the function covRob() to compute a robust covariance estimator.

I have written the following function to compute standardized beta coefficients for a robust linear regression. Setting the parameter classic=TRUE gives you the classic estimation of the beta coefficients. For very bad data, the covRob() function cannot compute the covariance due singularities. In this case the classical estimator is returned.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
my.lmr.beta <- function (object, classic = FALSE) { if(class(object) != "lmRob") stop("Object must be of class 'lmRob'") model <- object$model num <- sapply(model, is.numeric) # numeric vars only b <- object$coefficients[num][-1] # final coefficients w/o intercept ## compute robust covariance covr <- NULL try(covr <- diag(covRob(model[num])$cov), silent = TRUE) if(is.null(covr) & classic == FALSE) warning("covRob() coud not be computed, instead covClassic() was applied.") ## compute classic covariance if robust failed if(is.null(covr) | classic == TRUE) covr <- diag(covClassic(model[num])$cov) sx <- sqrt(covr[-1]) # standard deviation of x's sy <- sqrt(covr[1]) # standard deviation of y beta <- b * sx/sy return(beta) } |

** UPDATE — 2014-07-23**

Computing standard deviations for factors makes sense, because variance is definded for the binomial distribution. So I have removed the num variable.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
my.lmr.beta <- function (object, classic = FALSE) { if(class(object) != "lmRob") stop("Object must be of class 'lmRob'") model <- object$model #num <- sapply(model, is.numeric) # numeric vars only b <- object$coefficients[-1] # final coefficients w/o intercept ## compute robust covariance covr <- NULL try(covr <- diag(covRob(model)$cov), silent = TRUE) if(is.null(covr) & classic == FALSE) warning("covRob() coud not be computed, instead covClassic() was applied.") ## compute classic covariance if robust failed if(is.null(covr) | classic == TRUE) covr <- diag(covClassic(sapply(model, as.numeric))$cov) sx <- sqrt(covr[-1]) # standard deviation of x's sy <- sqrt(covr[1]) # standard deviation of y beta <- b * sx/sy return(beta) } |

# Select Pupils by Number of Pupils per Group

Selecting rows conditioned on values in columns is easy, as for example selecting people aged over 33. What is about selecting rows conditioned on statistics computed on multiple rows of the data frame, as for example selecting pupils in groups by the number of pupils per group?

That is where the very nice dplyr package comes in.

We build and print the data frame:

1 |
df <- data.frame(id=1:9, classid=c(1,1,1,2,2,3,3,3,3), math=round(runif(9,1,6),1)) |

1 2 3 4 5 6 7 8 9 10 11 |
> print(df) id classid math 1 1 1 5.4 2 2 1 4.0 3 3 1 1.1 4 4 2 2.2 5 5 2 3.9 6 6 3 2.7 7 7 3 6.0 8 8 3 2.0 9 9 3 1.6 |

Now, we want to select – i.e. “filter” in terms of the dplyr package – pupils that are part of groups/classes with more than two pupils per class. In dplyr there are three different syntaxes to achieve this.

1 2 3 4 5 6 7 8 9 10 11 |
# step-by-step df.g <- group_by(df, classid) df.n <- filter(df.g, n()>2) # or nested syntax df.n <- filter(group_by(df, classid), n()>2) # or with %.% operator df.n <- df %.% group_by(classid) %.% filter(n()>2) |

The result is the same for all:

1 2 3 4 5 6 7 8 9 10 11 12 |
> print(df.n) Source: local data frame [7 x 3] Groups: classid id classid math 1 1 1 5.4 2 2 1 4.0 3 3 1 1.1 4 6 3 2.7 5 7 3 6.0 6 8 3 2.0 7 9 3 1.6 |

Of course, you can do this the pure-R-way,

1 2 3 4 5 6 7 8 9 10 |
> df.c <- df[df$classid %in% which(xtabs(~classid, df)>2), ] > print(df.c) id classid math 1 1 1 5.4 2 2 1 4.0 3 3 1 1.1 6 6 3 2.7 7 7 3 6.0 8 8 3 2.0 9 9 3 1.6 |

but I think with dplyr it looks quite a bit nicer.

Happy dpylr!

# Installing JAGS 3.4.0 under OS X 10.9 Maverick

First of all see the excellent installation manual by Martyn Plummer and Bill Northcott (JAGS Version 3.4.0 installation manual) at section 2 “Mac OS X”.

For the rest of us (me included) thinking ./configure without any options will do the job are getting the following error:

1 |
configure: error: "You need to install the LAPACK library" |

(It will not work even with the --with-lapack='-framework vecLib' option.)

Do not follow this instruction! You do not have to install the LAPACK library because on OS X 10.9 the optimized (accelerated by Apple engineers) version of LAPACK is allready installed, see the LAPACK(7) Mac OS X Manual Page.

From now on just type the slightly modified commands in your bash-shell (terminal):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
export CC=/usr/bin/clang export CXX=/usr/bin/clang++ export CFLAGS="-g -Os -mmacosx-version-min=10.6 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -arch i386 -arch x86_64" export CXXFLAGS="-g -Os -mmacosx-version-min=10.6 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -arch i386 -arch x86_64" export FFLAGS="-g -Os -mmacosx-version-min=10.6 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -arch i386 -arch x86_64" export LDFLAGS="-mmacosx-version-min=10.6 -arch i386 -arch x86_64" ./configure --disable-dependency-tracking --with-included-ltdl make -j 8 sudo make install |

Of course, make sure you have installed all the required tools before executing the commands.

Happy ./configuring!

# Raspberry Pi Camera sends Pictures to ownCloud

Of course, there are many variants to take pictures with your raspberry pi camera and save them somewhere.

I use my raspberry pi camera to observe my flat when I am away. I want to do this in a as secure way as possible, i.e., I do not want to open any special ports on my router nor do I want to send unencrypted images over the web.

Inspired by http://blog.davidsingleton.org/raspberry-pi-webcam-a-gentle-intro-to-crontab/ my idea was to take a photo every 5 or 10 minutes and save them to my ownCloud server via WebDAV with SSL encryption.

Step by step:

1.) Make sure you have a raspberry pi with a camera module and an owncloud installation reachable over https somewhere out in the web (with picture app activated).

2.) Mount your owncloud drive on your raspberry pi: Create the directory /home/pi/owncloud and add the following line to fstab:

1 |
https://[your.owncloud.domain.name]/remote.php/webdav/ /home/pi/owncloud davfs rw,noexec,noauto,user,async,_netdev,uid=pi,gid=pi 0 0 |

then mount the drive by calling mount owncloud/ or by calling echo -e "y" | mount owncloud/ if your server has a self-signed certificate as mine has.

3.) My idea is to take a picture, say, every 10 minutes, and take them for 24 hours with any need to remove them manually. So, write a script like the following, name it “take_photo.sh” and make it executable:

1 2 3 4 |
#!/bin/bash filename="/home/pi/owncloud/myflat_$(date +%H%M).jpg" raspistill -o /home/pi/image.jpg mv /home/pi/image.jpg $filename |

4.) Set up the crontab by calling crontab -e and add the following line:

1 |
*/10 * * * * /home/pi/take_photo.sh |

5.) You are finished. Raspberry pi will take every 10 minutes a photo and save them to your ownCloud where you have a beautiful picture viewer that you can access wherever you are on whatever for a device.

# Compute SPSS like mean index variables

Suppose the problem discribed under theanalysisfactor.com.

The point is to compute meaningfull mean index variables while missing values are present. In R you have the switch na.rm to tell a function – here the mean() function – what to do with missing values. Setting this to true – mean(..., na.rm=TRUE) – forces the function to use all non-missing values, even if there is only one. In the other case – mean(..., na.rm=FALSE) – the function will return NA even if there is ony one missing value.

To handel this situation I have written a very handy function that works like the MEAN.{X}() function in SPSS, where {X} denotes the minimal number of variables that should be non-missing to be incorporated in computing the mean value.

My single line R function looks like

1 2 3 |
spss.row.means <- function(vars, not.na=0) { apply(vars, 1, function(x) ifelse(sum(!is.na(x)) >= not.na, mean(x, na.rm=TRUE), NA)) } |

As the first argument you have to pass the variables (in columns) and the second argument is the minimal number of variables that should be non-missing.

Have fun(ction)!

# The Beauty of Unique Alphanumeric Random Codes

My challenge was to generate random codes of variable length and size. The codes should be all unique and build of alphanumeric symbols. I wrote the following function using the build-in function sample():

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
alpha.rnd <- function(length, size) { if(size > 36^length) stop("\n size cannot be greater than 36^length") b <- NULL repeat { a <- NULL for (i in 1:length) { a <- paste0(sample(c(letters, 0:9), size, replace = TRUE), a) } b <- c(a,b) if(sum(duplicated(b)) == 0) break size = sum(duplicated(b)) b <- b[!duplicated(b)] } return(b) } |

Example:

1 2 3 |
> set.seed(77) > alpha.rnd(3,5) [1] "5qk" "94z" "f45" "au8" "qq0" |

Enjoy!