}
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (phi in seq(0,1, 1/20)) {
sig1 <- matrix( c(
  1, phi, l^2+l^2*phi, l^2+l^2*phi,
  phi, 1, l^2+l^2*phi, l^2+l^2*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
mu <- c(0, 0, 0, 0)
s1 <- mvrnorm(1000, mu, sig1)
colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
print(phi)
#print(eigen(cov(s1))
print(cronbach.alpha(cov(s1)))
}
eigen(cov(s1))
eigen(cov(s1))$values
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (phi in seq(0,1, 1/20)) {
sig1 <- matrix( c(
  1, phi, l^2+l^2*phi, l^2+l^2*phi,
  phi, 1, l^2+l^2*phi, l^2+l^2*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
mu <- c(0, 0, 0, 0)
s1 <- mvrnorm(1000, mu, sig1)
colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
print(phi)
print(eigen(cov(s1))$values)
print(cronbach.alpha(cov(s1)))
}
lpq
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
cronbach.alpha2 <- function(x) {
  a <- sum(diag(x))/sum(x) * (ncol(x)/(ncol(x)-1))
  a
}
for (phi in seq(0,1, 1/20)) {
sig1 <- matrix( c(
  1, phi, l^2+l^2*phi, l^2+l^2*phi,
  phi, 1, l^2+l^2*phi, l^2+l^2*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
mu <- c(0, 0, 0, 0)
s1 <- mvrnorm(1000, mu, sig1)
colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
print(phi)
print(eigen(cov(s1))$values)
print(cronbach.alpha(cov(s1)))
print(cronbach.alpha2(cov(s1)))
}
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (phi in seq(0,1, 1/20)) {
sig1 <- matrix( c(
  1, phi, l^2+l^2*phi, l^2+l^2*phi,
  phi, 1, l^2+l^2*phi, l^2+l^2*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
  l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
mu <- c(0, 0, 0, 0)
s1 <- mvrnorm(1000, mu, sig1)
colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
print(cov(s1))
print(phi)
print(eigen(cov(s1))$values)
print(cronbach.alpha(cov(s1)))
}
?write.csv
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (l in seq(0, 1, 1/20)) {
  for (vare1 in seq(0, 0.5, 0.1)) {
    vare2 <- vare1
    for (phi in seq(0,1, 1/20)) {
      sig1 <- matrix( c(
        1, phi, l^2+l^2*phi, l^2+l^2*phi,
        phi, 1, l^2+l^2*phi, l^2+l^2*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
      mu <- c(0, 0, 0, 0) 
      s1 <- mvrnorm(1000, mu, sig1)
      colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
      print(phi)
      print(eigen(cov(s1))$values)
      print(cronbach.alpha(cov(s1)))
      r <- cbind(l, vare1, phi, cronbach.alpha(cov(s1)))
      results <- rbind(results, r)
    }
  }
}
?data.frame
d
?ls()
ls()
k
k <- data.frame()
k
rm(k)
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
results <- data.frame()
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (l in seq(0, 1, 1/20)) {
  for (vare1 in seq(0, 0.5, 0.1)) {
    vare2 <- vare1
    for (phi in seq(0,1, 1/20)) {
      sig1 <- matrix( c(
        1, phi, l^2+l^2*phi, l^2+l^2*phi,
        phi, 1, l^2+l^2*phi, l^2+l^2*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
      mu <- c(0, 0, 0, 0) 
      s1 <- mvrnorm(1000, mu, sig1)
      colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
      print(phi)
      print(eigen(cov(s1))$values)
      print(cronbach.alpha(cov(s1)))
      r <- cbind(l, vare1, phi, cronbach.alpha(cov(s1)))
      results <- rbind(results, r)
    }
  }
}
write.csv(results, 'results.csv')
results
summary(results)
?write.csv
write.csv(results, 'results.csv', quote=FALSE, sep=",", row.names=FALSE)
?write.csv
write.csv(results, 'results.csv', quote=FALSE, sep=",", row.names=FALSE, col.names=c('lambda', 'error.var', 'phi', 'alpha'))
colnames(results)=c('lambda', 'error.var', 'phi', 'alpha')
write.csv(results, 'results.csv', quote=FALSE, row.names=FALSE)
quit()
ls()
princomp(s1)$loadings
prcomp(s1)$rotation
prcomp(s1)$rotation[,1]
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
results <- data.frame()
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (l in seq(0, 1, 1/20)) {
  for (vare1 in seq(0, 0.5, 0.1)) {
    vare2 <- vare1
    for (phi in seq(0,1, 1/20)) {
      sig1 <- matrix( c(
        1, phi, l^2+l^2*phi, l^2+l^2*phi,
        phi, 1, l^2+l^2*phi, l^2+l^2*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
      mu <- c(0, 0, 0, 0) 
      s1 <- mvrnorm(1000, mu, sig1)
      colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
      print(phi)
      print(eigen(cov(s1))$values)
      print(cronbach.alpha(cov(s1)))
      r <- cbind(l, vare1, phi, cronbach.alpha(cov(s1)), eigen(cov(s1))$values, prcomp(s1)$rotation[,1] )
      results <- rbind(results, r)
    }
  }
}
colnames(results)=c('lambda', 'error.var', 'phi', 'alpha')
write.csv(results, 'results.csv', quote=FALSE, row.names=FALSE)
results
eigen(s1)
eigen(cov(s1))
eigen(cov(s1))$vectors
eigen(cov(s1))$vectors[,1]
eigen(cov(s1))$values
cbind(eigen(cov(s1)), eigen(cov(s1)))
?delist
?unlist
cbind(unlist(eigen(cov(s1))), unlist(eigen(cov(s1))))
cbind(eigen(cov(s1))$values, eigen(cov(s1))$values)
rbind(eigen(cov(s1))$values, eigen(cov(s1))$values)
c(eigen(cov(s1))$values, eigen(cov(s1))$values)
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
results <- data.frame()
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (l in seq(0, 1, 1/20)) {
  for (vare1 in seq(0, 0.5, 0.1)) {
    vare2 <- vare1
    for (phi in seq(0,1, 1/20)) {
      sig1 <- matrix( c(
        1, phi, l^2+l^2*phi, l^2+l^2*phi,
        phi, 1, l^2+l^2*phi, l^2+l^2*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
      mu <- c(0, 0, 0, 0) 
      s1 <- mvrnorm(1000, mu, sig1)
      colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
      print(phi)
      print(eigen(cov(s1))$values)
      print(cronbach.alpha(cov(s1)))
      r <- c(l, vare1, phi, cronbach.alpha(cov(s1)), eigen(cov(s1))$values, prcomp(s1)$rotation[,1] )
      results <- rbind(results, r)
    }
  }
}
colnames(results)=c('lambda', 'error.var', 'phi', 'alpha')
write.csv(results, 'results.csv', quote=FALSE, row.names=FALSE)
library(MASS)
library(semplus)
library(lavaan)
library(Matrix)
library(sfsmisc)
l <- 1
vare1 <- 0.0
vare2 <- 0.0
results <- data.frame()
cronbach.alpha <- function(x) {
  a <- sum(diag(x))
  x2 <- x
  x2[lower.tri(x2)==FALSE] <- 0
  a <- 1 - a / (a + 2*sum(x2))
  a <- a*(ncol(x)/(ncol(x)-1))
  a
}
for (l in seq(0, 1, 1/20)) {
  for (vare1 in seq(0, 0.5, 0.1)) {
    vare2 <- vare1
    for (phi in seq(0,1, 1/20)) {
      sig1 <- matrix( c(
        1, phi, l^2+l^2*phi, l^2+l^2*phi,
        phi, 1, l^2+l^2*phi, l^2+l^2*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi+vare1, 2*l^4+2*l^4*phi,
        l^2+l^2*phi, l^2+l^2*phi, 2*l^4+2*l^4*phi, 2*l^4+2*l^4*phi+vare2 ), nrow=4, ncol=4, byrow=T)
      mu <- c(0, 0, 0, 0) 
      s1 <- mvrnorm(1000, mu, sig1)
      colnames(s1) <- c('a', 'b', 'c', 'd')
#s2 <- mvrnorm(1000, c(0, 0), matrix(c(1, phi, phi, 1), nrow=2, ncol=2))
#x <- s2[,1]*l + s2[,2]*l
#c <- l*x + rnorm(1000, 0, sqrt(vare1))
#d <- l*x + rnorm(1000, 0, sqrt(vare2))
#s2 <- cbind(s2, c, d)
#model <- 'X =~ NA*a + b + c + d
#X ~~ 1*X
#d ~~ equal("c~~c")*d'
#sem1 <- sem(model, sample.cov=as.matrix(nearPD(cov(s1))$mat), sample.nobs=nrow(s1))
      print(phi)
      print(eigen(cov(s1))$values)
      print(cronbach.alpha(cov(s1)))
      r <- c(l, vare1, phi, cronbach.alpha(cov(s1)), eigen(cov(s1))$values, prcomp(s1)$rotation[,1] )
      results <- rbind(results, r)
    }
  }
}
colnames(results)=c('lambda', 'error.var', 'phi', 'alpha', 'eigen1', 'eigen2', 'eigen3', 'eigen4', 'load1', 'load2', 'load3', 'load4')
write.csv(results, 'results.csv', quote=FALSE, row.names=FALSE)
library(lattice)
?levelplot
summary(results)
levelplot(alpha ~ phi*lambda | error.var, results)
?pdf
tiff('factor.loadings.abs%03d.tiff', compression="none", onefile=FALSE)
tiff('alpharesults.tiff', compression="none")
levelplot(alpha ~ phi*lambda | error.var, results)
dev.off()
levelplot(alpha ~ phi*lambda | error.var, results)
?pdf
pdf('alpharesults.pdf', compression="none")
pdf('alpharesults.pdf')
levelplot(alpha ~ phi*lambda | error.var, results)
dev.off()
quit()
??vif
library(car)
??car
?vif
quit()
setwd('/home/joerg/Teaching/stats/semtutorial_lavaan/CD')
library(lavaan)
library(matrixcalc)
install.packages('matrixcalc')
library(matrixcalc)
library(mvnormtest)
library(norm)
install.packages('norm')
library(norm)
library(Amelia)
model1 <- readLines('model1.txt')
model1
?readLines
model1
ls()
model2 <- readLines('model2.txt')
model2
model3 <- readLines('model3.txt')
model3
model4 <- readLines('model4.txt')
model4
model1
model1 <- readLines('model1.txt')
model1
data1 <- read.csv('dataset1.csv')
summary(data1)
sem1 <- sem(model1, data1)
summary(sem1)
model1a <- readLines('model1a.txt')
model1a
sem1a <- sem(model1a, data1)
summary(sem1a)
delta.chisq <- 7962.058 - 0
delta.df <- 2 - 0
delta.p.value <- pchisq(delta.chisq, delta.df, lower.tail=F)
delta.p.value
model2 <- readLines('model2')
model2 <- readLines('model2.txt')
data2 <- read.csv('dataset2.csv')
sem2 <- sem(model2, sample.cov=cov(data2), sample.nobs=nrow(data2))
summary(sem2, standardized=T)
summary(sem2, fit.measures=T)
source('sem.GFI.R')
sem.GFI(sem2)
sem.AGFI(sem2)
source('sem.r2.R')
sem.r2(sem2)
quit()
ls()
rm(*)
rm()
ls()
?rm
rm(ls())
?rm
rm(list=ls())
ls()
quit()
