Running R code in SPSS for paired t (equal groups)

The R code below may be run in a syntax window in SPSS 16 after using the R integrator plug-in. You need to enter the inputs (Type I error, difference in means, sd and required power) in the BEGIN DATA sandwich. Sample size (in both groups) is outputted in the SPSS output window.

DATA LIST free
/alpha (f10.5) mdiff (f10.5) sd (f10.5) power (f10.5). 
BEGIN DATA. 
0.05 6.0 10.0 0.90
END DATA. 

begin program r.
casedata <- spssdata.GetDataFromSPSS()

beta <- casedata$alpha
mdiff <- casedata$mdiff
sd <- casedata$sd
pow <-casedata$power

fn <- function(n) {
(pow - (1 - pt(abs(qt(beta/2,(2*n)-2)),(2*n)-2,mdiff/sqrt(2*sd*sd/n))) )^2
 }

nout<- nlm(fn,2)
nout<- trunc(nout$estimate+1)
outp <- data.frame(A=c(nout,beta,mdiff,sd,pow),row.names=c("N","alpha","Mean Diff","sd","Power"))

spsspivottable.Display(outp,
                       title="Sample Size needed",                   rowdim=" ",                                                     format=formatSpec.Correlation)
end program.