content
stringlengths
0
14.9M
filename
stringlengths
44
136
## File Name: IRT.marginal_posterior.R ## File Version: 0.13 IRT.marginal_posterior <- function(object, dim, remove_zeroprobs=TRUE, ...) { UseMethod("IRT.marginal_posterior") } IRT_marginal_posterior_compute <- function(object, dim, remove_zeroprobs=TRUE, ... ) { post <- IRT.posterior(object=object) theta <- attr(post, "theta") skill_names <- colnames(theta) if ( ! is.numeric(dim) ){ dim <- match(dim, skill_names) } ND <- length(dim) if (remove_zeroprobs){ ind <- which( colSums(post) > 0 ) post <- post[, ind, drop=FALSE] theta <- theta[ ind,, drop=FALSE] } N <- nrow(post) LD <- length(dim) sp <- "" for (ll in 1:LD){ sp <- paste0(sp,theta[,ll]) } spu <- sort(unique(sp)) NS <- length(spu) index_sp <- match(sp, spu) marg_post <- matrix(NA, nrow=N, ncol=NS) colnames(marg_post) <- spu for (ss in 1:NS){ marg_post[,ss] <- rowSums( post[, index_sp==ss] ) } # MAP estimate map <- spu[ max.col(marg_post) ] # skill design theta <- strsplit(spu, split="") theta <- matrix( as.numeric(unlist(theta)), ncol=ND, byrow=TRUE) colnames(theta) <- skill_names[dim] rownames(theta) <- spu #-- output res <- list(marg_post=marg_post, map=map, theta=theta) return(res) } IRT.marginal_posterior.gdina <- IRT_marginal_posterior_compute IRT.marginal_posterior.din <- IRT_marginal_posterior_compute IRT.marginal_posterior.mcdina <- IRT_marginal_posterior_compute
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.marginal_posterior.R
## File Name: IRT.modelfit.R ## File Version: 0.21 ########################################################### IRT.modelfit <- function (object, ...) { UseMethod("IRT.modelfit") } ########################################################### # general model fit function for CDM objects IRT.modelfit.CDM <- function( object, mod ) { res <- modelfit.cor.din( dinobj=object) res$IRT.IC <- IRT.IC(object) res$objname <- mod class(res) <- paste0("IRT.modelfit.", class(object) ) return(res) } ########################################################### ########################################################### # IRT.modelfit for objects of class din, gdina IRT.modelfit.din <- function( object, ... ) { cl <- paste(match.call())[2] res <- IRT.modelfit.CDM( object, mod=cl ) return(res) } ##################################################### # IRT.modelfit for gdina objects IRT.modelfit.gdina <- function( object, ... ) { cl <- paste(match.call())[2] res <- IRT.modelfit.CDM( object, mod=cl ) return(res) } ############################################################# ##################################################### # IRT.modelfit for gdm objects IRT.modelfit.gdm <- function( object, ... ) { cl <- paste(match.call())[2] res <- IRT.modelfit.CDM( object, mod=cl ) return(res) } ############################################################# ############################################################ # summary summary.IRT.modelfit.helper <- function( object, ... ) { cat("Test of Global Model Fit\n") obji <- object$modelfit.test cdm_print_summary_data_frame(obji=obji, from=2, digits=3, rownames_null=FALSE) #------ cat("\nFit Statistics\n") obji <- object$modelfit.stat cdm_print_summary_data_frame(obji=obji, from=1, digits=3, rownames_null=FALSE) } ################################################################# # summary.modelfit.cor.din summary.IRT.modelfit.din <- summary.IRT.modelfit.helper summary.IRT.modelfit.gdina <- summary.IRT.modelfit.helper summary.IRT.modelfit.gdm <- summary.IRT.modelfit.helper # summary.IRT.modelfit.gdm <- summary.modelfit.cor.gdm
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.modelfit.R
## File Name: IRT.parameterTable.R ## File Version: 0.06 ########################################################### # extracts used dataset IRT.parameterTable <- function(object, ...) { UseMethod("IRT.parameterTable") } ########################################################### # IRT.data.din <- function( object, ... ){ # dat <- object$dat # attr(dat,"weights") <- object$control$weights # attr(dat,"group") <- object$control$group # return(dat) # } ############################################################ # IRT.data.gdina <- IRT.data.din # IRT.data.gdm <- IRT.data.din # IRT.data.mcdina <- IRT.data.din # IRT.data.slca <- IRT.data.din #############################################################
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.parameterTable.R
## File Name: IRT.posterior.R ## File Version: 0.15 ########################################################### # extracts the individual posterior IRT.posterior <- function (object, ...) { UseMethod("IRT.posterior") } ########################################################### ########################################################### # object of class din IRT.posterior.din <- function( object, ... ) { ll <- object$posterior attr(ll,"theta") <- object$attribute.patt.splitted attr(ll,"prob.theta") <- object$attribute.patt$class.prob attr(ll,"G") <- 1 return(ll) } ########################################################### ########################################################### # object of class gdina IRT.posterior.gdina <- function( object, ... ) { ll <- object$posterior attr(ll,"theta") <- object$attribute.patt.splitted attr(ll,"prob.theta") <- object$attribute.patt[, 1:object$G ] attr(ll,"G") <- object$G return(ll) } ############################################################ ########################################################### # object of class mcdina IRT.posterior.mcdina <- function( object, ... ) { ll <- object$posterior attr(ll,"theta") <- object$attribute.patt.splitted attr(ll,"prob.theta") <- object$attribute.patt attr(ll,"G") <- object$G return(ll) } ############################################################ ########################################################### # object of class gdm IRT.posterior.gdm <- function( object, ... ) { ll <- object$posterior attr(ll,"theta") <- object$theta.k attr(ll,"prob.theta") <- object$pi.k attr(ll,"G") <- object$G return(ll) } ############################################################ ########################################################### # object of class slca IRT.posterior.slca <- function( object, ... ) { ll <- object$posterior attr(ll,"theta") <- NA res <- list( "delta"=object$delta, "delta.designmatrix"=object$delta.designmatrix ) attr(ll,"skillspace") <- res attr(ll,"prob.theta") <- object$pi.k attr(ll,"G") <- object$G return(ll) } ############################################################ ########################################################### # object of class reglca IRT.posterior.reglca <- function( object, ... ) { ll <- object$p.aj.xi attr(ll,"theta") <- NA attr(ll,"prob.theta") <- object$class_probs attr(ll,"G") <- object$G return(ll) } ############################################################
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.posterior.R
## File Name: IRT.predict.R ## File Version: 1.29 ######################################################################## # function for predicting item responses based on posterior IRT.predict <- function( object, dat, group=1 ) { resp <- as.matrix(dat) # post1 <- IRT.posterior( object ) irf1 <- IRT.irfprob( object ) irf1[ is.na(irf1) ] <- 0 N <- nrow(resp) I <- ncol(resp) TP <- dim(irf1)[3] K <- dim(irf1)[2] if ( length( dim(irf1) )==4 ){ # handle case with group-wise item response functions irf1 <- irf1[,,,group] } irf1_ <- as.numeric(irf1) # call Rcpp function res0 <- cdm_rcpp_irt_predict( resp=resp, irf1=irf1_, K=K, TP=TP ) probs.categ <- array( res0$probs_categ, dim=c(N,K,TP,I) ) pred <- res0$pred var1 <- res0$var1 resid1 <- res0$resid1 sresid1 <- res0$sresid1 # output res <- list( "expected"=pred, "probs.categ"=probs.categ, "variance"=var1, "residuals"=resid1, "stand.resid"=sresid1 ) return(res) } ####################################################################### # cat("output\n") ; a1 <- Sys.time() ; print( a1- a0 ) ; a0 <- a1
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.predict.R
## File Name: IRT.predict.R.R ## File Version: 0.09 ######################################################################## # function for predicting item responses based on posterior IRT.predict.R <- function( object, dat, group=1 ) { resp <- dat irf1 <- IRT.irfprob( object ) irf1[ is.na(irf1) ] <- 0 N <- nrow(resp) I <- ncol(resp) TP <- dim(irf1)[3] K <- dim(irf1)[2] if ( length( dim(irf1) )==4 ){ # handle case with group-wise item response functions irf1 <- irf1[,,,group] } pred <- array( 0, dim=c(N,TP,I) ) dimnames(pred)[[3]] <- colnames(resp) var1 <- pred # category-wise predictions pred.categ <- array( 0, dim=c(N,K,TP,I) ) dimnames(pred.categ)[[4]] <- colnames(resp) #------------------------- for (ii in 1:I){ # ii <- 32 v1 <- rep(0,N) kk <- 1 irf.ii <- matrix( irf1[ii,kk,], nrow=N, ncol=TP, byrow=TRUE ) pred.categ[,kk,,ii] <- irf.ii for (kk in 2:K){ irf.ii <- matrix( irf1[ii,kk,], nrow=N, ncol=TP, byrow=TRUE ) p1 <- irf.ii pred.categ[,kk,,ii] <- p1 v1 <- (kk-1) * p1 + v1 } pred[,,ii] <- v1 ind.ii <- which( is.na(resp[,ii]) ) if ( length(ind.ii) > 0 ){ pred[ ind.ii,,ii ] <- NA pred.categ[ ind.ii, 1:K,,ii ] <- NA } for (kk in 1:K){ var1[,,ii ] <- var1[,,ii] + pred.categ[, kk,, ii] * ( ( kk-1 ) - pred[,,ii] )^2 } } #---------------------- # compute residuals resp1 <- array( 0, dim=c(N,TP,I) ) for (tt in 1:TP){ resp1[,tt,] <- resp } resid1 <- resp1 - pred sresid1 <- resid1 / sqrt( var1 ) # output res <- list( "expected"=pred, "probs.categ"=pred.categ, "variance"=var1, "residuals"=resid1, "stand.resid"=sresid1 ) return(res) } #######################################################################
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.predict.R.R
## File Name: IRT.repDesign.R ## File Version: 0.18 ######################################################## IRT.repDesign <- function( data, wgt=NULL, jktype="JK_TIMSS", jkzone=NULL, jkrep=NULL, jkfac=NULL, fayfac=1, wgtrep="W_FSTR", ngr=100, Nboot=200, seed=.Random.seed ) { # BIFIEsurvey must be loaded CDM_require_namespace("BIFIEsurvey") data <- as.data.frame(data) #************************* # apply Jackknife (Bootstrap) routine if ( jktype !="BOOT"){ bdat2 <- BIFIEsurvey::BIFIE.data.jack(data, wgt=wgt, jktype=jktype, pv_vars=NULL, jkzone=jkzone, jkrep=jkrep, jkfac=jkfac, fayfac=1, wgtrep=wgtrep, pvpre=NULL, ngr=ngr, seed=seed, cdata=FALSE) } else { bdat2 <- BIFIEsurvey::BIFIE.data.boot( data, wgt=wgt, pv_vars=NULL, Nboot=Nboot, seed=seed, cdata=FALSE) } cat("+++ Generated IRT.repDesign object\n") # output res <- list( "wgt"=bdat2$wgt, "wgtrep"=bdat2$wgtrep, "fayfac"=bdat2$fayfac, "RR"=ncol( bdat2$wgtrep) ) class(res) <- "IRT.repDesign" return(res) } ########################################################################
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.repDesign.R
## File Name: IRT.se.R ## File Version: 0.03 ########################################################### IRT.se <- function (object, ...) { UseMethod("IRT.se") } ###########################################################
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.se.R
## File Name: IRT.se.din.R ## File Version: 0.12 ######################################################################### IRT.se.din <- function( object, extended=FALSE, parm=NULL, level=.95, infomat=FALSE, ind.item.skillprobs=TRUE, ind.item=FALSE, diagcov=FALSE, h=.001, ... ) { if ( ! missing( parm) ){ partable <- object$partable g1 <- parm %in% partable$parnames if ( mean(g1) < 1 ){ stop("Not all requested parameters in parameter table!\n") } } # variance covariance matrix v1 <- vcov( object, extended=extended, infomat=FALSE,ind.item.skillprobs=ind.item.skillprobs, ind.item=ind.item, diagcov=diagcov, h=h,...) # confidence intervals res <- data.frame( "parameter"=names(attr( v1, "coef")) ) res$est <- attr(v1,"coef") res$se <- sqrt( diag(v1) ) level1 <- round( 100*( 1 - level ) / 2, 1 ) quant1 <- abs( stats::qnorm( ( 1-level) / 2) ) res[, paste0( level1, " %" ) ] <- res$est - quant1*res$se res[, paste0( 100-level1, " %" ) ] <- res$est + quant1*res$se partable <- object$partable ind1 <- match( res$parameter, partable$parnames ) vars1 <- c("partype", "parindex" ) res <- cbind( partable[ ind1, vars1 ], res ) vars2 <- c( "item", "item.name", "skillclass", "fixed","free", "rule", "totindex" ) res <- cbind( res, partable[ ind1, vars2 ]) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT.se.din.R
## File Name: IRT_RMSD_calc_distributions.R ## File Version: 0.07 IRT_RMSD_calc_distributions <- function( n.ik, pi.k, eps=1E-30 ) { # probs ... [ classes, items, categories ] # n.ik ... [ classes, items, categories, groups ] # N.ik ... [ classes, items, categories] N.ik <- n.ik[,,,1] G <- dim(n.ik)[4] pitot <- pi.k[,1] eps <- 1E-10 if (G>1){ for (gg in 2:G ){ N.ik <- N.ik + n.ik[,,,gg] pitot <- pitot + pi.k[,gg] } } #*** extract maximum number of categories maxK <- apply( N.ik, c(2,3), sum, na.rm=TRUE ) maxK <- rowSums( maxK > eps ) # calculate summed counts N.ik_tot <- array( 0, dim=dim(N.ik) ) N.ik_tot[,,1] <- N.ik[,,1,drop=FALSE] K <- dim(N.ik)[3] for (kk in 2:K){ N.ik_tot[,,1] <- N.ik_tot[,,1,drop=FALSE] + N.ik[,,kk,drop=FALSE] } for (kk in 2:K){ N.ik_tot[,,kk] <- N.ik_tot[,,1] } # calculate itemwise statistics p.ik_observed <- N.ik / ( N.ik_tot + eps ) p.ik_observed <- replace_NA( p.ik_observed, value=0 ) # define class weights pi.k_tot <- array( 0, dim=dim(p.ik_observed) ) for (kk in 1:K){ pi.k_tot[,,kk] <- matrix( pitot, nrow=dim(pi.k_tot)[1], ncol=dim(pi.k_tot)[2], byrow=FALSE ) } #--- output res <- list( N.ik=N.ik, N.ik_tot=N.ik_tot, p.ik_observed=p.ik_observed, pi.k_tot=pi.k_tot, maxK=maxK, K=K ) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_RMSD_calc_distributions.R
## File Name: IRT_RMSD_calc_md.R ## File Version: 2.17 #-- MD statistic IRT_RMSD_calc_md <- function( n.ik, pi.k, probs, eps=1e-30 ) { # probs ... [ classes, items, categories ] # n.ik ... [ classes, items, categories, groups ] # N.ik ... [ classes, items, categories] N.ik <- n.ik[,,,1] G <- dim(n.ik)[4] pitot <- pi.k[,1] eps <- 1E-10 if (G>1){ for (gg in 2:G){ N.ik <- N.ik + n.ik[,,,gg] pitot <- pitot + pi.k[,gg] } } #*** extract maximum number of categories maxK <- apply( N.ik, c(2,3), sum, na.rm=TRUE ) maxK <- rowSums( maxK > eps ) # calculate summed counts N.ik_tot <- array( 0, dim=dim(N.ik) ) N.ik_tot[,,1] <- N.ik[,,1,drop=FALSE] K <- dim(N.ik)[3] for (kk in 2:K){ N.ik_tot[,,1] <- N.ik_tot[,,1,drop=FALSE] + N.ik[,,kk,drop=FALSE] } for (kk in 2:K){ N.ik_tot[,,kk] <- N.ik_tot[,,1] } # calculate itemwise statistics p.ik_observed <- N.ik / ( N.ik_tot + eps ) p.ik_observed[ is.na( p.ik_observed ) ] <- 0 # define class weights pi.k_tot <- array( 0, dim=dim(p.ik_observed) ) for (kk in 1:K){ pi.k_tot[,,kk] <- matrix( pitot, nrow=dim(pi.k_tot)[1], ncol=dim(pi.k_tot)[2], byrow=FALSE ) } # calculate statistics dist.item <- pi.k_tot * ( p.ik_observed - probs ) h1 <- 0 * dist.item[,,1] for (kk in 2:K){ h1 <- h1 + (kk-1) * dist.item[,,kk] } itemfit.md <- colSums( h1 / ( maxK - 1) ) v1 <- apply(p.ik_observed, 2, sum) itemfit.md[ v1==0 ] <- NA return(itemfit.md) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_RMSD_calc_md.R
## File Name: IRT_RMSD_calc_rmsd.R ## File Version: 3.34 #--- auxiliary function RMSD calculation IRT_RMSD_calc_rmsd <- function( n.ik, pi.k, probs, eps=1E-30 ) { #*** compute distributions based on expected counts res <- IRT_RMSD_calc_distributions( n.ik=n.ik, pi.k=pi.k, eps=eps ) pi.k_tot <- res$pi.k_tot p.ik_observed <- res$p.ik_observed maxK <- res$maxK K <- res$K N.ik <- res$N.ik eps1 <- 1 eps0 <- 1E-20 #------------------------------------------------ #*** RMSD fit statistic dist.item <- pi.k_tot * ( p.ik_observed - probs )^2 h1 <- IRT_RMSD_proc_dist_item(dist.item=dist.item) RMSD <- sqrt( colSums( h1 ) / maxK ) #*** bias corrected RMSD statistic N.it <- array(0, dim=dim(N.ik) ) K <- dim(N.ik)[3] for (kk in 1:K){ N.it[,, 1] <- N.it[,,1] + N.ik[,,kk] } for (kk in 2:K){ N.it[,, kk] <- N.it[,,1] } N.it <- N.it # + 1 p.ik_obs_var <- probs * ( 1 - probs ) / ( N.it + eps0 ) p.ik_samp_var <- pi.k_tot^2 * p.ik_obs_var h2 <- IRT_RMSD_proc_dist_item(dist.item=p.ik_samp_var) h2 <- colSums( h2 ) / maxK RMSD_bc <- RMSD^2 - h2 RMSD_bc <- sign(RMSD_bc)*sqrt( abs(RMSD_bc) ) #------------------------------------------------ #*** MD fit statistic dist.item <- pi.k_tot * ( p.ik_observed - probs ) h1 <- IRT_RMSD_proc_dist_item(dist.item=dist.item) MD <- colSums( h1 ) / ( maxK - 1) #------------------------------------------------ #*** MAD fit statistic dist.item <- pi.k_tot * abs( p.ik_observed - probs ) h1 <- IRT_RMSD_proc_dist_item(dist.item=dist.item) MAD <- colSums(h1) / maxK RMSD <- cdm_replace_inf(x=RMSD) RMSD_bc <- cdm_replace_inf(x=RMSD_bc) MD <- cdm_replace_inf(x=MD) MAD <- cdm_replace_inf(x=MAD) #--- output res <- list( RMSD=RMSD, RMSD_bc=RMSD_bc, MD=MD, MAD=MAD ) return(res) } rmsea_aux <- function( n.ik, pi.k, probs, eps=1E-30 ) { res <- IRT_RMSD_calc_rmsd( n.ik=n.ik, pi.k=pi.k, probs=probs, eps=eps ) return( res$RMSD ) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_RMSD_calc_rmsd.R
## File Name: IRT_RMSD_proc_dist_item.R ## File Version: 0.05 IRT_RMSD_proc_dist_item <- function(dist.item) { #--- processing dist.item <- replace_NA( dist.item, value=0 ) h1 <- array3_sum( dist.item ) return(h1) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_RMSD_proc_dist_item.R
## File Name: IRT_RMSD_summary_print_statistics.R ## File Version: 0.07 IRT_RMSD_summary_print_statistics <- function( stat_summary, stat, digits) { obji <- stat_summary NC <- ncol(obji) for (gg in 2:NC ){ obji[,gg] <- round( obji[,gg], digits=digits) } print( obji ) cat("\n") obji <- stat NC <- ncol(obji) for (gg in 2:NC ){ obji[,gg] <- round( obji[,gg], digits=digits) } print(obji) } summary.IRT.RMSD_print_statistics <- IRT_RMSD_summary_print_statistics
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_RMSD_summary_print_statistics.R
## File Name: IRT_frequencies_default.R ## File Version: 0.09 IRT_frequencies_default <- function(data, post, probs, weights=NULL) { dim_probs <- dim(probs) K <- dim_probs[2] I <- dim_probs[1] TP <- dim_probs[3] N <- nrow(data) #--- preparation item response data dat <- data dat.ind <- 1 - is.na(dat) dat[ is.na(dat) ] <- 0 if (is.null(weights)){ weights <- rep(1,N) } #--- univariate observed frequencies uni_obs <- matrix( 0, nrow=I, ncol=K) items <- colnames(dat) categories <- paste0("Cat", 1:K - 1 ) rownames(uni_obs) <- items colnames(uni_obs) <- categories for (kk in 1:K){ uni_obs[,kk] <- colSums( ( dat==kk - 1) * dat.ind * weights ) } #--- univariate expected frequencies uni_exp <- 0*uni_obs for (ii in 1:I){ for (kk in 1:K){ counts_ii_kk <- dat.ind[,ii] * weights * post uni_exp[ii,kk] <- sum( t(counts_ii_kk) * probs[ii,kk,] ) } } #--- univariate marginals descriptive statistics M_obs <- rep(NA, I) names(M_obs) <- items SD_obs <- SD_exp <- M_exp <- M_obs for (ii in 1:I){ res <- univar_table_statistics( freq=uni_obs[ii,] ) M_obs[ii] <- res$M SD_obs[ii] <- res$SD res <- univar_table_statistics( freq=uni_exp[ii,] ) M_exp[ii] <- res$M SD_exp[ii] <- res$SD } #--- bivariate frequency tables biv_obs <- array( 0, dim=c(I,I,K,K) ) dimnames(biv_obs) <- list( items, items, categories, categories ) biv_exp <- biv_obs biv_N <- matrix(0,nrow=I,ncol=I) dimnames(biv_N) <- list(items, items) chisq <- cov_obs <- cov_exp <- cor_exp <- cor_obs <- biv_N attr( chisq, "df") <- chisq for (ii in 1:I){ for (jj in ii:I){ for (kk in 1:K){ for (hh in 1:K){ dat_temp <- ( dat[,ii]==kk-1 ) * ( dat[,jj]==hh - 1 ) * dat.ind[,ii] * dat.ind[,jj] * weights biv_obs[ii,jj,kk,hh] <- sum( dat_temp ) counts_temp <- dat.ind[,ii] * dat.ind[,jj] * weights * post biv_exp[ii,jj,kk,hh] <- sum( t(counts_temp) * probs[ii,kk,] * probs[jj,hh,] ) } } biv_N[ii,jj] <- biv_N[jj,ii] <- sum(biv_obs[ii,jj,,]) biv_obs[jj,ii,,] <- biv_obs[ii,jj,,] biv_exp[jj,ii,,] <- biv_exp[ii,jj,,] } } for (ii in 1:I){ for (jj in ii:I){ res <- bivariate_table_statistics(freqtable=biv_obs[ii,jj,,]) cov_obs[ii,jj] <- cov_obs[jj,ii] <- res$cov cor_obs[ii,jj] <- cor_obs[jj,ii] <- res$cor res <- bivariate_table_statistics(freqtable=biv_exp[ii,jj,,]) K1 <- res$K1 K2 <- res$K2 cov_exp[ii,jj] <- cov_exp[jj,ii] <- res$cov cor_exp[ii,jj] <- cor_exp[jj,ii] <- res$cor chisq[ii,jj] <- chisq[jj,ii] <- chisq_compute(obs=biv_obs[ii,jj,,], exp=biv_exp[ii,jj,,]) attr(chisq, "df")[ii,jj] <- attr(chisq, "df")[jj,ii] <- (K1-1)*(K2-1) } } diag(chisq) <- NA attr(chisq, "p") <- 1 - stats::pchisq(chisq, df=attr(chisq,"df") ) #--- output res <- list( uni_obs=uni_obs, uni_exp=uni_exp, M_obs=M_obs, M_exp=M_exp, SD_obs=SD_obs, SD_exp=SD_exp, biv_obs=biv_obs, biv_exp=biv_exp, biv_N=biv_N, cov_obs=cov_obs, cov_exp=cov_exp, cor_obs=cor_obs, cor_exp=cor_exp, chisq=chisq ) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_frequencies_default.R
## File Name: IRT_se_gdina.R ## File Version: 0.21 IRT_se_gdina <- function(object, h=1E-4) { dat <- object$dat resp.ind.list <- object$resp.ind.list delta <- object$delta attr.prob <- object$attr.prob aggr.attr.patt <- object$control$aggr.attr.patt Aj <- object$Aj Mj <- object$Mj p.xi.aj <- object$like J <- ncol(dat) linkfct <- object$control$linkfct item.patt.split <- object$control$item.patt.split item.patt.freq <- object$item.patt.freq IP <- nrow(item.patt.split) zeroprob.skillclasses <- object$zeroprob.skillclasses resp.ind.list <- object$resp.ind.list G <- object$G L <- object$Nskillclasses group <- object$G reduced.skillspace <- object$reduced.skillspace beta <- object$beta Z.skillspace <- object$Z.skillspace if (G==1){ item.patt.freq <- as.vector(item.patt.freq) } progress <- FALSE iter <- 1E2 #-- handle reduced skill space if (reduced.skillspace){ if (G==1){ attr.prob <- reduced_skillspace_beta_2_probs( Z=Z.skillspace, beta=beta ) } if (G>1){ attr.prob <- matrix(NA, nrow=L, ncol=G) for (gg in 1:G){ attr.prob[,gg] <- reduced_skillspace_beta_2_probs( Z=Z.skillspace, beta=beta[,gg] ) } } } # no reduced skillspace if (! reduced.skillspace){ eps <- 2*h beta <- matrix( 0, nrow=L-1, ncol=G) bounds <- c(eps, 1E3) if (G==1){ beta[,1] <- cdm_sumnorm_squeeze( vec=attr.prob, bounds=bounds )[-L] } else { for (gg in 1:G){ beta[,gg] <- cdm_sumnorm_squeeze( vec=attr.prob[,gg], bounds=bounds )[-L] } } } arglist <- list( J=J, L=L, aggr.attr.patt=aggr.attr.patt, Mj=Mj, delta=delta, linkfct=linkfct, IP=IP, item.patt.split=item.patt.split, resp.ind.list=resp.ind.list, zeroprob.skillclasses=zeroprob.skillclasses, G=G, item.patt.freq=item.patt.freq, beta=beta, Z.skillspace=Z.skillspace, reduced.skillspace=reduced.skillspace, return_all=TRUE) res <- do.call( what=IRT_se_gdina_calc_individual_likelihood, args=arglist ) loglike <- res$loglike p.xi.aj <- res$p.xi.aj p.aj.xi <- res$p.aj.xi like_ind <- res$like_ind #--- derivatives based with respect to delta delta1 <- delta jj <- 5 # item pp <- 1 # parameter delta1[[jj]][[pp]] <- delta[[jj]][[pp]] + h arglist1 <- arglist arglist1$return_all <- FALSE arglist1$delta <- delta1 loglike1 <- do.call( what=IRT_se_gdina_calc_individual_likelihood, args=arglist1 ) d1 <- ( loglike1 - loglike ) / h # Revalpr("d1") eps <- 1E-20 prob_args <- list( J=J, jj=jj, L=L, aggr.attr.patt=aggr.attr.patt, Mj=Mj, delta=delta, linkfct=linkfct) pj0 <- do.call( gdina_calc_prob_one_item, args=prob_args ) prob_args1 <- prob_args delta1 <- delta delta1[[jj]][[pp]] <- delta[[jj]][[pp]] + h prob_args1$delta <- delta1 pj1 <- do.call( gdina_calc_prob_one_item, args=prob_args1 ) delta1 <- delta delta1[[jj]][[pp]] <- delta[[jj]][[pp]] - h prob_args1$delta <- delta1 pj2 <- do.call( gdina_calc_prob_one_item, args=prob_args1 ) like_der <- matrix( 0, nrow=IP, ncol=L) resp_jj <- resp.ind.list[[jj]] data_jj <- item.patt.split[ resp_jj, jj ] + 1 like_der[ resp_jj, ] <- ( pj1[ data_jj, ] - pj2[ data_jj, ] ) / (2*h) / pj0[ data_jj, ] if (G==1){ d1a <- sum( rowSums( p.aj.xi * like_der ) * item.patt.freq ) } else { d1a <- 0 for (gg in 1:G){ d1a <- d1a + sum( rowSums( p.aj.xi[,,gg] * like_der ) * item.patt.freq[,gg] ) } } #---- derivatives with respect to beta bb <- 4 gg <- 1 beta1 <- beta beta1[bb,gg] <- beta[bb,gg] + h arglist1 <- arglist arglist1$return_all <- FALSE arglist1$beta <- beta1 loglike1 <- do.call( what=IRT_se_gdina_calc_individual_likelihood, args=arglist1 ) d1 <- ( loglike1 - loglike ) / h beta1 <- beta beta1[bb,gg] <- beta[bb,gg] - h arglist1 <- arglist arglist1$return_all <- FALSE arglist1$beta <- beta1 loglike2 <- do.call( what=IRT_se_gdina_calc_individual_likelihood, args=arglist1 ) d1b <- ( loglike1 - loglike2 ) / (2*h) # Revalpr("d1b") beta1[bb,gg] <- beta[bb,gg] + h skill_args <- list( beta=beta1, Z.skillspace=Z.skillspace, reduced.skillspace=reduced.skillspace, G=G) attr.prob1 <- do.call( what=IRT_se_gdina_calc_skill_distribution, args=skill_args ) beta1[bb,gg] <- beta[bb,gg] - h skill_args$beta <- beta1 attr.prob2 <- do.call( what=IRT_se_gdina_calc_skill_distribution, args=skill_args ) if (G==1){ attr_prob1 <- attr.prob1 attr_prob2 <- attr.prob2 attr_prob <- attr.prob post <- p.aj.xi freq <- item.patt.freq } else { attr_prob1 <- attr.prob1[,gg] attr_prob2 <- attr.prob2[,gg] attr_prob <- attr.prob[,gg] post <- p.aj.xi[,,gg] freq <- item.patt.freq[,gg] } M1 <- cdm_matrix2( ( attr_prob1 - attr_prob2 ) / (2*h) / ( attr_prob + eps ), nrow=IP) d1a <- sum( rowSums( post * M1 ) * freq ) ### This function is yet incomplete. But the remaining derivatives can be simply calculated. }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_se_gdina.R
## File Name: IRT_se_gdina_calc_individual_likelihood.R ## File Version: 0.19 IRT_se_gdina_calc_individual_likelihood <- function(J, L, aggr.attr.patt, Mj, delta, linkfct, IP, item.patt.split, resp.ind.list, zeroprob.skillclasses, G, item.patt.freq, Z.skillspace, beta, reduced.skillspace, return_all=FALSE ) { pjM <- gdina_calc_prob( progress=FALSE, iter=100, disp="", J=J, L=L, aggr.attr.patt=aggr.attr.patt, Mj=Mj, delta=delta, linkfct=linkfct ) p.xi.aj <- gdina_calc_individual_likelihood( IP=IP, L=L, pjM=pjM, item.patt.split=item.patt.split, J=J, resp.ind.list=resp.ind.list, zeroprob.skillclasses=zeroprob.skillclasses ) #--- compute attribute probabilities attr.prob <- IRT_se_gdina_calc_skill_distribution( beta=beta, Z.skillspace=Z.skillspace, reduced.skillspace=reduced.skillspace, G=G, eps=1E-5 ) eps <- 1E-30 #--- calculate the updated likelihood p.xi.aj[ p.xi.aj > 1 ] <- 1 - eps p.xi.aj[ p.xi.aj < 0 ] <- eps if (G==1){ l1 <- rowSums( p.xi.aj * cdm_matrix2( attr.prob, nrow=IP ) ) + eps l1[ l1 < 0 ] <- eps like_ind <- l1 loglike <- sum( log(l1) * item.patt.freq ) } if (G>1){ like_ind <- array( 0, dim=c(IP, L, G) ) loglike <- 0 for (gg in 1:G){ l1 <- rowSums( p.xi.aj * cdm_matrix2( attr.prob[,gg], nrow=IP ) ) + eps l1[ l1 < 0 ] <- eps like_ind[,,gg] <- l1 loglike <- loglike + sum( log(l1) * item.patt.freq[,gg] ) } } #--- update posterior if requested if (return_all){ res <- gdina_calc_individual_posterior(G=G, IP=IP, attr.prob=attr.prob, p.xi.aj=p.xi.aj, L=L, I=0, zeroprob.skillclasses=zeroprob.skillclasses, reduced.skillspace=reduced.skillspace, item.patt.freq=item.patt.freq) p.aj.xi <- res$p.aj.xi } #--- output res <- loglike if (return_all){ res <- list( loglike=loglike, p.xi.aj=p.xi.aj, p.aj.xi=p.aj.xi, like_ind=like_ind ) } return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_se_gdina_calc_individual_likelihood.R
## File Name: IRT_se_gdina_calc_skill_distribution.R ## File Version: 0.09 IRT_se_gdina_calc_skill_distribution <- function(beta, Z.skillspace, reduced.skillspace, G, eps=1E-5 ) { #--- adapt to multiple group case #--- reduced skill space if (reduced.skillspace){ L <- nrow(Z.skillspace) if (G==1){ attr.prob <- reduced_skillspace_beta_2_probs( Z=Z.skillspace, beta=beta ) } else { attr.prob <- matrix( NA, nrow=L, ncol=G) for (gg in 1:G){ attr.prob[,gg] <- reduced_skillspace_beta_2_probs( Z=Z.skillspace, beta=beta[,gg] ) } } } #--- no reduced skill space if ( ! reduced.skillspace ){ bounds <- c(eps, 1E2) if (G==1){ attr.prob <- c( beta, 1 - sum(beta) ) attr.prob <- cdm_sumnorm_squeeze(vec=attr.prob, bounds=bounds) } else { L <- nrow(beta) + 1 attr.prob <- matrix( NA, nrow=L, ncol=G) for (gg in 1:G){ b1 <- beta[,gg] b1 <- c( b1, 1 - sum(b1) ) attr.prob[,gg] <- cdm_sumnorm_squeeze(vec=b1, bounds=bounds) } } } return(attr.prob) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/IRT_se_gdina_calc_skill_distribution.R
## File Name: RcppExports.R ## File Version: 8.002006 # Generated by using Rcpp::compileAttributes() -> do not edit by hand # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 cdm_rcpp_din_deterministic_devcrit <- function(DAT, DATRESP, LATRESP, GUESS, SLIP) { .Call('_CDM_cdm_rcpp_din_deterministic_devcrit', PACKAGE='CDM', DAT, DATRESP, LATRESP, GUESS, SLIP) } cdm_rcpp_din_jml_devcrit <- function(DAT, DATRESP, LATRESP, GUESS, SLIP) { .Call('_CDM_cdm_rcpp_din_jml_devcrit', PACKAGE='CDM', DAT, DATRESP, LATRESP, GUESS, SLIP) } cdm_rcpp_din_calc_prob <- function(latresp1, guess, slip, J, L) { .Call('_CDM_cdm_rcpp_din_calc_prob', PACKAGE='CDM', latresp1, guess, slip, J, L) } cdm_rcpp_din_calc_counts <- function(p_aj_xi, item_patt_freq, item_patt_split1, resp_patt_bool, J, L) { .Call('_CDM_cdm_rcpp_din_calc_counts', PACKAGE='CDM', p_aj_xi, item_patt_freq, item_patt_split1, resp_patt_bool, J, L) } cdm_rcpp_din_validate_aggregate_max <- function(IDI, itemindex, I) { .Call('_CDM_cdm_rcpp_din_validate_aggregate_max', PACKAGE='CDM', IDI, itemindex, I) } cdm_rcpp_din_validate_update_qmatrix <- function(qmatrix_poss, attr_patt, Ilj, Rlj, I, L, K, rule) { .Call('_CDM_cdm_rcpp_din_validate_update_qmatrix', PACKAGE='CDM', qmatrix_poss, attr_patt, Ilj, Rlj, I, L, K, rule) } cdm_rcpp_discrimination_index_attribute_patterns <- function(attr_patt) { .Call('_CDM_cdm_rcpp_discrimination_index_attribute_patterns', PACKAGE='CDM', attr_patt) } cdm_rcpp_discrimination_index_calc <- function(comp_matrix, probs, dim_probs, K) { .Call('_CDM_cdm_rcpp_discrimination_index_calc', PACKAGE='CDM', comp_matrix, probs, dim_probs, K) } cdm_rcpp_discrimination_index_test_level <- function(discrim_item_attribute) { .Call('_CDM_cdm_rcpp_discrimination_index_test_level', PACKAGE='CDM', discrim_item_attribute) } cdm_rcpp_discrimination_index_idi <- function(probs, dim_probs, K) { .Call('_CDM_cdm_rcpp_discrimination_index_idi', PACKAGE='CDM', probs, dim_probs, K) } cdm_rcpp_est_calc_accuracy_version2_consistency_helper <- function(post, est, max_est_index, N, prob_theta, eps) { .Call('_CDM_cdm_rcpp_est_calc_accuracy_version2_consistency_helper', PACKAGE='CDM', post, est, max_est_index, N, prob_theta, eps) } cdm_rcpp_data_prep_long_format <- function(data) { .Call('_CDM_cdm_rcpp_data_prep_long_format', PACKAGE='CDM', data) } cdm_rcpp_normalize_matrix_row <- function(x) { .Call('_CDM_cdm_rcpp_normalize_matrix_row', PACKAGE='CDM', x) } cdm_rcpp_eval_likelihood_calc_wide_format <- function(data, irfprob, dim_irfprob, like0) { .Call('_CDM_cdm_rcpp_eval_likelihood_calc_wide_format', PACKAGE='CDM', data, irfprob, dim_irfprob, like0) } cdm_rcpp_eval_likelihood_calc_long_format <- function(data_long, irfprob, dim_irfprob, like0) { .Call('_CDM_cdm_rcpp_eval_likelihood_calc_long_format', PACKAGE='CDM', data_long, irfprob, dim_irfprob, like0) } cdm_rcpp_eval_likelihood <- function(data, irfprob, dim_irfprob, prior, normalization, long_format, N) { .Call('_CDM_cdm_rcpp_eval_likelihood', PACKAGE='CDM', data, irfprob, dim_irfprob, prior, normalization, long_format, N) } cdm_rcpp_generalized_distance_method <- function(data, dataresp, idealresp, theta, a, b) { .Call('_CDM_cdm_rcpp_generalized_distance_method', PACKAGE='CDM', data, dataresp, idealresp, theta, a, b) } cdm_rcpp_ideal_resp_pattern <- function(qmatrix, skillspace) { .Call('_CDM_cdm_rcpp_ideal_resp_pattern', PACKAGE='CDM', qmatrix, skillspace) } cdm_rcpp_irt_classify_individuals <- function(like) { .Call('_CDM_cdm_rcpp_irt_classify_individuals', PACKAGE='CDM', like) } cdm_rcpp_irt_predict <- function(resp, irf1, K, TP) { .Call('_CDM_cdm_rcpp_irt_predict', PACKAGE='CDM', resp, irf1, K, TP) } cdm_rcpp_itemfit_sx2_calc_scoredistribution <- function(P1, Q1) { .Call('_CDM_cdm_rcpp_itemfit_sx2_calc_scoredistribution', PACKAGE='CDM', P1, Q1) } cdm_rcpp_kli_id <- function(pjk, sc) { .Call('_CDM_cdm_rcpp_kli_id', PACKAGE='CDM', pjk, sc) } cdm_rcpp_mcdina_probs_pcm_groups <- function(dat, dat_resp_bool, group, probs, CC, TP) { .Call('_CDM_cdm_rcpp_mcdina_probs_pcm_groups', PACKAGE='CDM', dat, dat_resp_bool, group, probs, CC, TP) } cdm_rcpp_mcdina_calccounts_pcm_groups <- function(dat, dat_resp_bool, group, fyiqk, pik, CC, weights) { .Call('_CDM_cdm_rcpp_mcdina_calccounts_pcm_groups', PACKAGE='CDM', dat, dat_resp_bool, group, fyiqk, pik, CC, weights) } cdm_rcpp_modelfit_cor2 <- function(posterior, data, data_resp_bool, probs1, probs0, ip, expiijj) { .Call('_CDM_cdm_rcpp_modelfit_cor2', PACKAGE='CDM', posterior, data, data_resp_bool, probs1, probs0, ip, expiijj) } cdm_rcpp_modelfit_cor_counts <- function(data, data_resp_bool) { .Call('_CDM_cdm_rcpp_modelfit_cor_counts', PACKAGE='CDM', data, data_resp_bool) } cdm_rcpp_sim_model_item_responses <- function(theta_index, irfprob, dim_irfprob) { .Call('_CDM_cdm_rcpp_sim_model_item_responses', PACKAGE='CDM', theta_index, irfprob, dim_irfprob) } cdm_rcpp_slca_calc_probs <- function(XdesM, dimXdes, Xlambda) { .Call('_CDM_cdm_rcpp_slca_calc_probs', PACKAGE='CDM', XdesM, dimXdes, Xlambda) } cdm_rcpp_slca_calc_deriv <- function(XdesM, dimXdes, Xlambda, probs, nik, Nik) { .Call('_CDM_cdm_rcpp_slca_calc_deriv', PACKAGE='CDM', XdesM, dimXdes, Xlambda, probs, nik, Nik) } cdm_rcpp_slca_calc_Xdes <- function(XDES, dimXdes) { .Call('_CDM_cdm_rcpp_slca_calc_Xdes', PACKAGE='CDM', XDES, dimXdes) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/RcppExports.R
## File Name: WaldTest.R ## File Version: 0.13 ################################################# # helper function Wald test WaldTest <- function( delta, vcov, R, nobs, cvec=NULL, eps=1E-10 ) { NR <- nrow(R) if ( is.null(cvec) ){ cvec <- rep( 0, NR ) } Rdii <- R %*% delta - cvec v1 <- R %*% vcov %*% t(R) diag(v1) <- diag(v1) * ( 1 + eps ) stat <- ( t( Rdii ) %*% solve( v1 ) %*% Rdii )[1,1] stats <- list() stats["X2"] <- stat stats["df"] <- NR stats["p"] <- stats::pchisq( stat, df=NR, lower.tail=FALSE) l1 <- stats$X2 / stats$df - 1 l1 <- if ( l1 < 0 ){ 0 } else { sqrt(l1 / ( nobs - 1 )) } stats["RMSEA"] <- l1 return(stats) } ##############################################
/scratch/gouwar.j/cran-all/cranData/CDM/R/WaldTest.R
## File Name: abs_approx.R ## File Version: 0.08 # quadratic approximation of the absolute value function abs_approx <- function( x, eps=1E-5) { res <- sqrt( x^2 + eps ) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/abs_approx.R
## File Name: abs_approx_D1.R ## File Version: 0.04 abs_approx_D1 <- function( x, eps=1E-5){ res <- x / sqrt( x^2 + eps ) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/abs_approx_D1.R
## File Name: anova.din.R ## File Version: 1.14 anova.din <- IRT.anova anova.gdina <- anova.din anova.gdm <- anova.din anova.mcdina <- anova.din anova.slca <- anova.din anova.reglca <- anova.din
/scratch/gouwar.j/cran-all/cranData/CDM/R/anova.din.R
## File Name: array3_sum.R ## File Version: 0.01 #*** summation of the third margin in a three-dimensional array array3_sum <- function( arr ) { dimA <- dim(arr) K <- dimA[ 3 ] h1 <- arr[,,1] if (K>=2){ for (kk in 2:K){ h1 <- h1 + arr[,,kk] } } return(h1) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/array3_sum.R
## File Name: attach_environment.R ## File Version: 0.04 ####################################### # attach all elements in an environment attach_environment <- function( res, envir ) { CC <- length(res) for (cc in 1:CC){ assign( names(res)[cc], res[[cc]], envir=envir ) } } .attach.environment <- attach_environment
/scratch/gouwar.j/cran-all/cranData/CDM/R/attach_environment.R
## File Name: bivariate_table_statistics.R ## File Version: 0.06 bivariate_table_statistics <- function( freqtable, values=NULL) { freqtable <- expand_matrix(x=freqtable) K <- nrow(freqtable) if (is.null(values)){ values <- seq(0,K-1) } #--- first item freq1 <- rowSums(freqtable) res <- univar_table_statistics(freq=freq1) M1 <- res$M SD1 <- res$SD K1 <- sum( freq1 > 0 ) #--- second item freq2 <- colSums(freqtable) res <- univar_table_statistics(freq=freq2) M2 <- res$M SD2 <- res$SD K2 <- sum( freq2 > 0 ) #--- bivariate statistics valuesM <- outer( values, values ) probs <- freqtable / sum( freqtable ) cov1 <- sum( probs * valuesM ) - M1 * M2 cor1 <- cov1 / SD1 / SD2 #--- output res <- list( cov=cov1, cor=cor1, M1=M1, SD1=SD1, K1=K1, M2=M2, SD2=SD2, K2=K2) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/bivariate_table_statistics.R
## File Name: cat_paste.R ## File Version: 0.03 cat_paste <- function(...) { args <- list(...) N1 <- length(args) res <- args[[1]] if (N1 > 1){ for (nn in 2:N1){ res <- paste0( res, args[[nn]] ) } } cat(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cat_paste.R
## File Name: cdi.kli.R ## File Version: 0.181 #--- cognitive diagnostic indices cdi.kli <- function( object ) { # object must be of class din or gdina if ( ! inherits(object, c("din","gdina") ) ){ stop("This functions only supports objects of class din or gdina!") } items <- colnames( object$data ) q.matrix <- object$q.matrix pjk0 <- pjk <- object$pjk # [ items, categories, skills ] skillclasses <- as.matrix( object$attribute.patt.splitted ) #-- rearrange probabilities pjk <- aperm( pjk, c(1,3,2) ) dpjk <- dim(pjk) pjk <- matrix( pjk, nrow=dpjk[1], ncol=dpjk[2]*dpjk[3] ) eps <- 1E-7 # prevent division by zero pjk <- ( pjk + eps ) / ( 1 + 2*eps ) #-- apply Rcpp function for calculation res0 <- cdm_rcpp_kli_id( pjk=pjk, sc=skillclasses ) #-- arrange Kullback Leibler information kli <- array( res0$kli, dim=c( res0$TP, res0$TP, res0$I ) ) kli <- aperm( kli, c(3,1,2) ) # arrange output res <- list( test_disc=sum(res0$glob_item), attr_disc=colSums(res0$attr_item), glob_item_disc=res0$glob_item, attr_item_disc=res0$attr_item, KLI=kli, skillclasses=res0$skillclasses, hdist=res0$hdist, pjk=pjk0, q.matrix=q.matrix ) # complete summary in a table dfr <- cbind( res0$glob_item, res0$attr_item ) l1 <- c( sum(res0$glob_item), colSums( res0$attr_item ) ) dfr <- rbind( l1, dfr ) rownames(dfr) <- NULL colnames(dfr) <- c( "cdi_test", paste0( "cdi_skill", 1:( ncol(dfr) -1 ) ) ) dfr <- data.frame( "item"=c("test", items ), dfr ) # names names(res$attr_disc) <- colnames(res$attr_item_disc) <- colnames(q.matrix) dimnames(res$KLI)[[1]] <- items names(res$glob_item_disc) <- rownames(res$attr_item_disc) <- items res$summary <- dfr class(res) <- "cdi.kli" return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdi.kli.R
## File Name: cdm.est.calc.accuracy.R ## File Version: 2.52 #**** CDM classification accuracy cdm.est.class.accuracy <- function( cdmobj, n.sims=0, version=2 ) { CALL <- match.call() #*** version 1 if (version==1){ dfr <- cdm_est_calc_accuracy_version1(cdmobj=cdmobj, n.sims=n.sims) res <- list( statistics=dfr, version=version) cdm_est_calc_accuracy_version1_print(object=res) } #*** version 2 if (version==2){ dfr <- cdm_est_calc_accuracy_version2( cdmobj=cdmobj, n.sims=n.sims ) res <- list( statistics=dfr, version=version) cdm_est_calc_accuracy_version1_print(object=res) } #--- output res$CALL <- CALL class(res) <- "cdm.est.class.accuracy" return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm.est.calc.accuracy.R
## File Name: cdm_add_ridge_diagonal.R ## File Version: 0.03 cdm_add_ridge_diagonal <- function(x, eps=1E-10 ) { D <- ncol(x) rx <- x + diag( eps, D ) return(rx) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_add_ridge_diagonal.R
## File Name: cdm_attach_exported_function.R ## File Version: 0.01 cdm_attach_exported_function <- function(pack, fun) { CDM_require_namespace(pkg=pack) fn <- paste0(pack, paste0(rep(":",2), collapse=""), fun) fn <- eval(parse(text=fn)) return(fn) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_attach_exported_function.R
## File Name: cdm_attach_internal_function.R ## File Version: 0.03 cdm_attach_internal_function <- function(pack, fun) { CDM_require_namespace(pkg=pack) fn <- paste0(pack, paste0(rep(":",3), collapse=""), fun) fn <- eval(parse(text=fn)) return(fn) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_attach_internal_function.R
## File Name: cdm_calc_increment.R ## File Version: 0.142 cdm_calc_increment <- function( d1, d2, max.increment, eps=1E-10, adj_fac=.98, type=1 ) { increment <- d1 / ( abs( d2 + eps ) ) increment[ is.na(increment) ] <- 0 #--- vector increment if (is.vector(increment) ){ increment <- cdm_trim_increment( increment=increment, max.increment=max.increment, type=type ) } #--- matrix increment if (is.matrix(increment) ){ K <- ncol(increment) for (kk in 1:K){ increment[,kk] <- cdm_trim_increment( increment=increment[,kk], max.increment=max.increment, type=type) } } #--- adjust maximum increment max.increment <- max(abs(increment)) / adj_fac #--- output res <- list(increment=increment, max.increment=max.increment) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_calc_increment.R
## File Name: cdm_calc_increment_regularization.R ## File Version: 0.363 cdm_calc_increment_regularization <- function( d1, d2, x0, regular_lam_used, max.increment, regular_type, regular_n=1, eps=1E-10, adj_fac=.98) { d2a <- ( abs(d2) + eps ) val <- x0 + d1 / d2a vt <- d2a val1 <- vt*val regular_lam_used <- regular_n * regular_lam_used updated <- cdm_parameter_regularization(x=val1, regular_type=regular_type, regular_lam=regular_lam_used) updated <- updated / vt increment <- updated - x0 max.increment <- 3 increment <- cdm_trim_increment( increment=increment, max.increment=max.increment ) updated <- x0 + increment #- compute indicator for regularized estimate parm_regularized <- ( regular_lam_used > 0 ) & ( abs(updated) < eps ) numb_regularized <- sum(parm_regularized) max.increment <- max(abs(increment)) / adj_fac #--- output res <- list( increment=increment, updated=updated, max.increment=max.increment, parm_regularized=parm_regularized, numb_regularized=numb_regularized) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_calc_increment_regularization.R
## File Name: cdm_calc_information_criteria.R ## File Version: 0.03 cdm_calc_information_criteria <- function(ic) { dev <- ic$deviance # AIC ic$AIC <- dev + 2*ic$np # BIC ic$BIC <- dev + ( log(ic$n) )*ic$np # CAIC (conistent AIC) ic$CAIC <- dev + ( log(ic$n) + 1 )*ic$np # corrected AIC ic$AICc <- ic$AIC + 2*ic$np * ( ic$np + 1 ) / ( ic$n - ic$np - 1 ) #--- output return(ic) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_calc_information_criteria.R
## File Name: cdm_calc_ll_with_counts.R ## File Version: 0.03 cdm_calc_ll_with_counts <- function( an.ik, pjk, eps=1E-20 ) { colSums( colSums( an.ik * log(pjk+eps) )) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_calc_ll_with_counts.R
## File Name: cdm_calc_posterior.R ## File Version: 1.175 #--- compute posterior distribution cdm_calc_posterior <- function(rprobs, gwt, resp, nitems, resp.ind.list, normalization=TRUE, thetasamp.density=NULL, snodes=0 ) { if ( snodes==0 ){ fx <- gwt } else { # calculate individual 'sampling weight' swt <- fx <- gwt / outer( rep(1, nrow(gwt)), thetasamp.density ) } nstud <- nrow(fx) # using C Code storage.mode(resp) <- "integer" fx <- .Call( '_CDM_calcfx', PACKAGE='CDM', fx, rprobs, resp.ind.list, resp) # numerical integration if ( snodes==0 ){ rfx <- rowSums(fx) if (normalization ){ hwt <- fx / rfx } else { hwt <- fx } } # Monte Carlo integration if ( snodes > 0 ){ rfx <- rowMeans(fx) if (normalization ){ hwt <- fx / rfx } else { hwt <- fx } } res <- list("hwt"=hwt, "rfx"=rfx ) if ( snodes > 0 ){ res[["swt" ]] <- swt } return(res) } #.......................................................... calc_posterior.v2 <- cdm_calc_posterior
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_calc_posterior.R
## File Name: cdm_create_vector.R ## File Version: 0.01 cdm_create_vector <- function(names, val) { N <- length(names) y <- rep(val, N) names(y) <- names return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_create_vector.R
## File Name: cdm_est_calc_accuracy_version1.R ## File Version: 0.045 cdm_est_calc_accuracy_version1 <- function( cdmobj, n.sims=0 ) { # original data data <- cdmobj$data # likelihood p.xi.aj <- cdmobj$like # class probabilities class.prob <- cdmobj$attribute.patt$class.prob # MLE (orginal data) c1 <- cdm_est_calc_accuracy_version1_computation( p.xi.aj=p.xi.aj, est.class=cdmobj$pattern$mle.est, class.prob=class.prob) # MAP (original data) c2 <- cdm_est_calc_accuracy_version1_computation( p.xi.aj=p.xi.aj, est.class=cdmobj$pattern$map.est, class.prob=class.prob) dfr <- rbind( c1, c2 ) rownames(dfr) <- c("MLE", "MAP" ) if ( inherits(cdmobj,"gdina") ){ n.sims <- 0 } #--- simulated classification if ( sum(is.na(data) ) > 0 & ( n.sims > 0 ) ){ n.sims <- nrow(data) } if ( n.sims > 0 ){ I <- ncol(data) # splitted attribute pattern attr.splitted <- cdmobj$attribute.patt.splitted # attribute classes ac <- cdmobj$attribute.patt rownames(attr.splitted) <- rownames(ac) class.prob <- ac$class.prob CC <- nrow(ac) # sample patterns classes.sim <- sample( 1:CC, prob=class.prob, size=n.sims, replace=TRUE) alpha.sim <- attr.splitted[ classes.sim,,drop=FALSE] # simulate according to the din model guess0 <- cdmobj$guess[,1] slip0 <- cdmobj$slip[,1] # simulated data (first data set) simdata1 <- sim.din(N=n.sims, q.matrix=cdmobj$q.matrix, guess=guess0, slip=slip0, rule=cdmobj$rule,alpha=alpha.sim) # simulated data (second data set) simdata2 <- sim.din(N=n.sims, q.matrix=cdmobj$q.matrix, guess=guess0, slip=slip0, rule=cdmobj$rule,alpha=alpha.sim) # handle missings d1 <- simdata1$dat if ( sum( is.na( data) ) > 0 ){ d1[ is.na(data) ] <- NA } simdata1$dat <- d1 d1 <- simdata2$dat if ( sum( is.na( data) ) > 0 ){ d1[ is.na(data) ] <- NA } simdata2$dat <- d1 # classification of students according # fixed item parameters # I <- length(guess0) mod1 <- din( simdata1$dat, q.matrix=cdmobj$q.matrix, constraint.guess=cbind(1:I, guess0), constraint.slip=cbind(1:I, slip0 ), rule=cdmobj$rule, progress=FALSE, skillclasses=attr.splitted, zeroprob.skillclasses=cdmobj$zeroprob.skillclasses ) mod2 <- din( simdata2$dat, q.matrix=cdmobj$q.matrix, constraint.guess=cbind(1:I, guess0), constraint.slip=cbind(1:I, slip0 ), rule=cdmobj$rule, progress=FALSE, skillclasses=attr.splitted, zeroprob.skillclasses=cdmobj$zeroprob.skillclasses) # compare MLE estimates dfr1 <- data.frame( "true"=paste( rownames(alpha.sim) ) ) dfr1$mle.simdata1 <- paste( mod1$pattern$mle.est ) dfr1$mle.simdata2 <- paste( mod2$pattern$mle.est ) # classification consistency dfr11 <- data.frame( "P_c_sim"=mean( dfr1[,2]==dfr1[,3] ) ) dfr11$P_a_sim <- mean( dfr1[,1]==dfr1[,3] ) # compare MAP estimates dfr1 <- data.frame( "true"=paste( rownames(alpha.sim) ) ) dfr1$mle.simdata1 <- paste( mod1$pattern$map.est ) dfr1$mle.simdata2 <- paste( mod2$pattern$map.est ) dfr12 <- data.frame( "P_c_sim"=mean( dfr1[,2]==dfr1[,3] ) ) # accuracy dfr12$P_a_sim <- mean( dfr1[,1]==dfr1[,3] ) dfr11 <- rbind( dfr11, dfr12 ) rownames(dfr11) <- c( "MLE", "MAP" ) dfr <- cbind( dfr, dfr11 ) } #******************************************** # marginal classification # likelihood p.xi.aj <- cdmobj$like # splitted attribute pattern attribute.patt.splitted <- cdmobj$attribute.patt.splitted # number of skills K <- ncol( attribute.patt.splitted ) dfr10 <- NULL for (kk in 1:K){ # compute marginal likelihood distribution ind.kk0 <- which( attribute.patt.splitted[, kk ]==0 ) ind.kk1 <- which( attribute.patt.splitted[, kk ]==1 ) # marginal likelihood pxiaj_kk <- cbind( rowSums( p.xi.aj[, ind.kk0 ] ), rowSums( p.xi.aj[, ind.kk1 ])) colnames(pxiaj_kk) <- c("0", "1" ) patt1 <- 1 * ( cdmobj$pattern[, paste0( "post.attr", kk ) ] > .5 ) c1 <- cdmobj$skill.patt[kk,1] class.prob.kk <- c( 1-c1, c1 ) c2 <- cdm_est_calc_accuracy_version1_computation( p.xi.aj=pxiaj_kk, est.class=patt1, class.prob=class.prob.kk) dfr10 <- rbind( dfr10, c2 ) } rownames(dfr10) <- paste0( "MAP_Skill", 1:K) if (n.sims>0){ dfr10 <- cbind( dfr10, NA, NA ) } colnames(dfr10) <- colnames(dfr) dfr <- rbind( dfr, dfr10 ) dfr <- dfr[, order( paste(colnames(dfr))) ] return(dfr) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_est_calc_accuracy_version1.R
## File Name: cdm_est_calc_accuracy_version1_computation.R ## File Version: 0.03 #**** estimate classification accuracy and consistency cdm_est_calc_accuracy_version1_computation <- function( p.xi.aj, est.class, class.prob ) { m0 <- matrix( colSums(p.xi.aj), nrow=nrow(p.xi.aj), ncol=ncol(p.xi.aj), byrow=TRUE) p.xi.aj <- p.xi.aj / m0 # calculate class index est.class.index <- match( paste(est.class), colnames(p.xi.aj ) ) # calculate formula (5) CC <- ncol( p.xi.aj ) # classification probability matrix class.prob.matrix2 <- class.prob.matrix1 <- matrix( NA, CC, CC ) for (aa in 1:CC){ for (cc in 1:CC){ class.prob.matrix1[cc,aa] <- sum( p.xi.aj[ est.class.index==cc, aa ] )^2 class.prob.matrix2[cc,aa] <- sum( p.xi.aj[ est.class.index==cc, aa ] ) } } # classification consistency P_c <- sum( colSums( class.prob.matrix1 ) * class.prob ) # marginal classification accuracy P_a <- sum( diag( class.prob.matrix2 ) * class.prob ) #**** calculate kappa M1 <- class.prob.matrix1 p1 <- rowSums(M1) p2 <- colSums(M1) h1 <- outer( p1, p2 ) #--- output res <- data.frame( P_c=P_c, P_a=P_a ) return(res) } .est.class.accuracy <- cdm_est_calc_accuracy_version1_computation
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_est_calc_accuracy_version1_computation.R
## File Name: cdm_est_calc_accuracy_version1_print.R ## File Version: 0.01 cdm_est_calc_accuracy_version1_print <- function( object, digits=3 ) { print( object$statistics, digits=digits ) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_est_calc_accuracy_version1_print.R
## File Name: cdm_est_calc_accuracy_version2.R ## File Version: 0.153 cdm_est_calc_accuracy_version2 <- function( cdmobj, n.sims=0 ) { #*** simulation based measures irfprob <- IRT.irfprob(cdmobj) theta <- attr(irfprob, "theta") TP <- nrow(theta) prob_theta <- attr(irfprob, "prob.theta") prob_theta <- cdm_shrink_positive(x=prob_theta) prior <- prob_theta data <- IRT.data(cdmobj) N <- nrow(data) if (n.sims==0){ n.sims <- N } sizes <- round( n.sims*prob_theta ) theta_index <- rep(1:TP, sizes) K <- ncol(theta) # number of skills skill_names <- colnames(theta) if (is.null(skill_names)){ skill_names <- paste0("skill",1:K) } statistics_names <- c( "MLE_patt", "MAP_patt", paste0("MLE_", skill_names), paste0("MAP_", skill_names) ) #-- simulate item responses dat1 <- sim_model(object=NULL, irfprob=irfprob, theta_index=theta_index, data=data, N_sim=n.sims )$dat dat2 <- sim_model(object=NULL, irfprob=irfprob, theta_index=theta_index, data=data, N_sim=n.sims )$dat #-- compute classifications based on simulated data class1 <- cdm_est_calc_accuracy_version2_classify_simulated_data( data=dat1, irfprob=irfprob, prior=prior) class2 <- cdm_est_calc_accuracy_version2_classify_simulated_data( data=dat2, irfprob=irfprob, prior=prior) #-- compute accuracy and consistency dfr <- as.data.frame( matrix( 0, nrow=2*(1+K), ncol=4 ) ) colnames(dfr) <- c("Pa_est", "Pa_sim", "Pc_est", "Pc_sim") rownames(dfr) <- statistics_names estimators <- c("MLE", "MAP") #-- multivariate pattern for (pp in estimators ){ dfr[ paste0(pp, "_patt"), "Pa_sim" ] <- ( mean( theta_index==class1[[pp]] ) + mean( theta_index==class2[[pp]] ) ) / 2 dfr[ paste0(pp, "_patt"), "Pc_sim" ] <- mean( class1[[pp]]==class2[[pp]] ) } #-- single skills for (kk in 1:K){ for (pp in estimators){ theta_index_kk <- theta[ theta_index, kk] est1_kk <- theta[ class1[[pp]], kk] est2_kk <- theta[ class2[[pp]], kk] dfr[ paste0(pp, "_", skill_names[kk] ), "Pa_sim" ] <- ( mean( theta_index_kk==est1_kk ) + mean( theta_index_kk==est2_kk ) ) / 2 dfr[ paste0(pp, "_", skill_names[kk] ), "Pc_sim" ] <- mean( est1_kk==est2_kk ) } } #**** estimated statistics like <- IRT.likelihood(cdmobj) post <- IRT.posterior(cdmobj) eps <- 1E-7 for (pp in estimators){ if (pp=="MLE"){ matr <- like } else { matr <- post } est <- cdm_rcpp_irt_classify_individuals(like=as.matrix(matr))$class_index index <- cbind(1:N, est) #*** accuracy dfr[ paste0(pp, "_patt"), "Pa_est" ] <- sum( post[ index ] ) / N theta_est <- theta[ est, ] for (kk in 1:K){ post0 <- sum( post[, theta[,kk]==0 ] * ( theta_est[, kk ]==0 ) ) / N post1 <- sum( post[, theta[,kk]==1 ] * ( theta_est[, kk ]==1 ) ) / N dfr[ paste0(pp, "_", skill_names[kk] ), "Pa_est" ] <- post0 + post1 } #*** consistency val <- cdm_rcpp_est_calc_accuracy_version2_consistency_helper( post=post, est=est-1, max_est_index=TP, N=N, prob_theta=prob_theta, eps=eps ) dfr[ paste0(pp, "_patt"), "Pc_est" ] <- val for (kk in 1:K){ est_kk <- theta[ est, kk] val <- cdm_rcpp_est_calc_accuracy_version2_consistency_helper( post=post, est=est_kk, max_est_index=2, N=N, prob_theta=prob_theta, eps=eps ) dfr[ paste0(pp, "_", skill_names[kk] ), "Pc_est" ] <- val } } #--- output return(dfr) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_est_calc_accuracy_version2.R
## File Name: cdm_est_calc_accuracy_version2_classify_simulated_data.R ## File Version: 0.04 cdm_est_calc_accuracy_version2_classify_simulated_data <- function( data, irfprob, prior) { like <- eval_likelihood(data=data, irfprob=irfprob, prior=NULL, normalization=FALSE) res_like <- cdm_rcpp_irt_classify_individuals(like=like) post <- eval_likelihood(data=data, irfprob=irfprob, prior=prior, normalization=TRUE) res_post <- cdm_rcpp_irt_classify_individuals(like=post) #--- output res <- list( like=like, MLE=res_like$class_index, post=post, MAP=res_post$class_index) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_est_calc_accuracy_version2_classify_simulated_data.R
## File Name: cdm_fa1.R ## File Version: 0.08 #--- function for unidimensional factor analysis cdm_fa1 <- function(Sigma, method=1, maxit=50, conv=1E-5) { # Sigma is a correlation matrix I <- ncol(Sigma) L <- matrix(.6, nrow=I, ncol=1) iter <- 0 iterate <- TRUE #--- begin iterations while (iterate){ L0 <- L #--- method=1 -> different item loadings if (method==1){ for (ll in 1:I){ y <- Sigma[ll,-ll] x <- L[-ll,1] L[ll,1] <- sum(x*y) / sum(x^2) } } #--- method=2 -> equal item loadings if (method==2){ val <- ( sum(Sigma) - I ) / ( I^2 - I ) L <- matrix( sqrt(val), nrow=I, ncol=1) } iter <- iter + 1 parchange <- max( abs(L0 - L)) if (iter >=maxit){ iterate <- FALSE } if (parchange < conv){ iterate <- FALSE } } #--- output res <- list(iter=iter, L=L, parchange=parchange) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_fa1.R
## File Name: cdm_fisherz.R ## File Version: 0.04 ## copy from psych::fisherz cdm_fisherz <- function(rho) { 0.5 * log((1 + rho)/(1 - rho)) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_fisherz.R
## File Name: cdm_fit_normal.R ## File Version: 0.051 cdm_fit_normal <- function(x, w) { theta.k <- x if ( is.vector(theta.k) ){ theta.k <- matrix( theta.k, ncol=1 ) } D <- ncol(theta.k) #---- unidimensional model if (D==1){ mg <- sum( theta.k[,1] * w ) sdg <- sum( theta.k[,1]^2 * w ) - mg^2 } #---- multidimensional model if (D>1){ mean.gg <- rep(0,D) Sigma.gg <- diag(0,D) for (dd in 1:D){ mean.gg[dd] <- sum( w * theta.k[,dd] ) } for (dd1 in 1:D){ for (dd2 in dd1:D){ Sigma.gg[dd1,dd2] <- sum( w * (theta.k[,dd1] - mean.gg[dd1] )* (theta.k[,dd2] - mean.gg[dd2] ) ) Sigma.gg[dd2,dd1] <- Sigma.gg[dd1,dd2] } } mg <- mean.gg sdg <- Sigma.gg } #---- output res <- list( Mu=mg, Sigma=sdg) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_fit_normal.R
## File Name: cdm_gini.R ## File Version: 0.03 # Gini coefficient, function simply copied from the R ineq package cdm_gini <- function(x) { n <- length(x) x <- sort(x) G <- sum(x * 1:n) G <- 2 * G/(n * sum(x)) G - 1 - (1/n) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_gini.R
## File Name: cdm_ginv.R ## File Version: 0.02 cdm_ginv <- function(x,...) { CDM_require_namespace("MASS") y <- MASS::ginv(X=x) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_ginv.R
## File Name: cdm_include_fixed_parameters.R ## File Version: 0.04 cdm_include_fixed_parameters <- function( parm, se_parm, parm_fixed ) { #--- vector of parameters if ( is.vector(parm) ){ if ( ! is.null( parm_fixed ) ){ parm[ parm_fixed[,1] ] <- parm_fixed[,2] se_parm[ parm_fixed[,1] ] <- 0 } } #--- matrix of parameters if ( is.matrix(parm) ){ if ( ! is.null( parm_fixed ) ){ parm[ parm_fixed[,1:2,drop=FALSE] ] <- parm_fixed[,3,drop=FALSE] se_parm[ parm_fixed[,1:2,drop=FALSE] ] <- 0 } } #------------------------------------- #--- output res <- list( parm=parm, se_parm=se_parm) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_include_fixed_parameters.R
## File Name: cdm_increment_trimming_after_mstep.R ## File Version: 0.072 cdm_increment_trimming_after_mstep <- function( parm, parm0, max.increment0, type ) { c1 <- cdm_trim_increment( increment=parm-parm0, max.increment=max.increment0, type=2 ) parm <- parm0 + c1 max.increment0 <- min( max.increment0, max(abs(parm - parm0)) ) #--- output res <- list( parm=parm, max.increment0=max.increment0) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_increment_trimming_after_mstep.R
## File Name: cdm_ll_numerical_differentiation.R ## File Version: 0.04 cdm_ll_numerical_differentiation <- function(ll0, ll1, ll2, h) { d1 <- ( ll1 - ll2 ) / ( 2 * h ) # negative sign? # second order derivative # f(x+h)+f(x-h)=2*f(x) + f''(x)*h^2 d2 <- ( ll1 + ll2 - 2*ll0 ) / h^2 #--- output res <- list( d1=d1, d2=d2) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_ll_numerical_differentiation.R
## File Name: cdm_log.R ## File Version: 0.04 cdm_log <- function(x, eps) { x <- ifelse(x < eps, eps, x) y <- log(x) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_log.R
## File Name: cdm_matrix1.R ## File Version: 0.02 cdm_matrix1 <- function( x, ncol ) { x <- as.vector(x) y <- matrix( x, nrow=length(x), ncol=ncol, byrow=FALSE ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_matrix1.R
## File Name: cdm_matrix2.R ## File Version: 0.03 cdm_matrix2 <- function( x, nrow ) { x <- as.vector(x) y <- matrix( x, nrow=nrow, ncol=length(x), byrow=TRUE ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_matrix2.R
## File Name: cdm_matrixstring.R ## File Version: 0.01 ############################################################ # calculates a string pattern consisting of matrix entries # matr <- skillclasses # string <- "Q" cdm_matrixstring <- function( matr, string ) { VV <- ncol(matr) l1 <- string for ( vv in 1:VV){ l1 <- paste0( l1, matr[,vv] ) } return(l1) } #################################################################
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_matrixstring.R
## File Name: cdm_parameter_regularization.R ## File Version: 0.213 cdm_parameter_regularization <- function(x, regular_type, regular_lam, regular_alpha=NULL, regular_tau=NULL) { y <- x #--- scad if ( regular_type=="scad"){ y <- cdm_penalty_threshold_scad( beta=x, lambda=regular_lam) } #--- lasso if ( regular_type=="lasso"){ y <- cdm_penalty_threshold_lasso( val=x, eta=regular_lam ) } #--- ridge if ( regular_type=="ridge"){ y <- cdm_penalty_threshold_ridge( beta=x, lambda=regular_lam ) } #--- elastic net if ( regular_type=="elnet"){ y <- cdm_penalty_threshold_elnet( beta=x, lambda=regular_lam, alpha=regular_alpha ) } #--- scadL2 if ( regular_type=="scadL2"){ y <- cdm_penalty_threshold_scadL2( beta=x, lambda=regular_lam, alpha=regular_alpha) } #--- tlp if ( regular_type=="tlp"){ y <- cdm_penalty_threshold_tlp( beta=x, lambda=regular_lam, tau=regular_tau) } #--- mcp if ( regular_type=="mcp"){ y <- cdm_penalty_threshold_mcp( beta=x, lambda=regular_lam) } return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_parameter_regularization.R
## File Name: cdm_pem_acceleration.R ## File Version: 0.132 cdm_pem_acceleration <- function( iter, pem_parameter_index, pem_parameter_sequence, pem_pars, PEM_itermax, parmlist, ll_fct, ll_args, deviance.history=NULL ) { res0 <- ll <- NULL PEM <- TRUE #-- transform into a vector pem_parm <- cdm_pem_collect_parameters( parmlist=parmlist, pem_parameter_index=pem_parameter_index ) #-- collect parameters in initial iterations pem_parameter_sequence <- cdm_pem_parameter_sequence_initial_iterations( pem_parm=pem_parm, pem_parameter_sequence=pem_parameter_sequence, iter=iter ) pem_update <- FALSE if ( ( iter %% 2==0 ) & ( iter > 0 ) & ( iter < PEM_itermax ) ){ pem_update <- TRUE pem_parameter_sequence$P2 <- pem_parm #** baseline likelihood ll_args <- cdm_pem_include_ll_args( ll_args=ll_args, pem_parm=pem_parm, pem_pars=pem_pars, pem_parameter_index=pem_parameter_index ) res0 <- res <- do.call( what=ll_fct, args=ll_args ) ll0 <- ll <- res$ll P0 <- pem_parameter_sequence$P0 P1 <- pem_parameter_sequence$P1 P2 <- pem_parameter_sequence$P2 iterate <- TRUE ii <- 0 #--- begin PEM iterations while (iterate){ ll_args0 <- ll_args res0 <- res ll0 <- ll tt <- cdm_pem_algorithm_compute_t( i=ii ) Pnew <- cdm_pem_algorithm_compute_Pnew( tt=tt, P0=P0, P1=P1, P2=P2 ) ll_args <- cdm_pem_include_ll_args( ll_args=ll_args, pem_parm=Pnew, pem_pars=pem_pars, pem_parameter_index=pem_parameter_index ) res <- do.call( what=ll_fct, args=ll_args ) ll <- res$ll if ( is.na(ll) ){ ll <- -Inf } if ( ll < ll0 ){ iterate <- FALSE } ii <- ii + 1 } #--- end PEM iterations ll <- res0$ll pem_parameter_sequence$P0 <- P1 pem_parameter_sequence$P1 <- P2 } if (iter > PEM_itermax){ PEM <- FALSE } if ( ! is.null( deviance.history) ){ diff_history <- diff( deviance.history[ 1:iter ] ) NL0 <- 15 NL <- min( NL0, iter ) # number of lags if ( iter > NL0 ){ diff2 <- diff_history[ seq( iter - 1, iter - NL, -1 ) ] PEM <- ! ( sum( ( diff2 < 0 ) ) > ( .35 * NL0 ) ) } } #--- output res <- list(ll=ll, pem_parameter_sequence=pem_parameter_sequence, PEM=PEM, res_ll_fct=res0, pem_update=pem_update ) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_acceleration.R
## File Name: cdm_pem_acceleration_assign_output_parameters.R ## File Version: 0.03 cdm_pem_acceleration_assign_output_parameters <- function(res_ll_fct, vars, envir, update) { if (update){ for (vv in vars){ assign( vv, res_ll_fct[[ vv ]], envir=envir ) } } }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_acceleration_assign_output_parameters.R
## File Name: cdm_pem_adjust_dimension.R ## File Version: 0.04 cdm_pem_adjust_dimension <- function(x, x_dim ) { if ( length(x_dim)==2 ){ x <- matrix(x, nrow=x_dim[1], ncol=x_dim[2] ) } if ( length(x_dim) >=3){ x <- array(x, dim=x_dim ) } return(x) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_adjust_dimension.R
## File Name: cdm_pem_algorithm_compute_Pnew.R ## File Version: 0.01 cdm_pem_algorithm_compute_Pnew <- function( tt, P0, P1, P2) { Pnew <- (1-tt)^2 * P0 + 2*tt*(1-tt)*P1 + tt^2 * P2 return(Pnew) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_algorithm_compute_Pnew.R
## File Name: cdm_pem_algorithm_compute_t.R ## File Version: 0.01 cdm_pem_algorithm_compute_t <- function( i, a=1.5, h=0.1) { return( 1 + a^i * h ) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_algorithm_compute_t.R
## File Name: cdm_pem_collect_parameters.R ## File Version: 0.03 cdm_pem_collect_parameters <- function( parmlist, pem_parameter_index ) { NV <- pem_parameter_index[["__length_parameter_vector"]] parm <- rep(0, NV) NP <- pem_parameter_index[["__n_parameters"]] NPL <- length(parmlist) for (pp in 1:NPL){ var_pp <- names(parmlist)[pp] parm_index_pp <- pem_parameter_index[[ var_pp ]] x <- as.vector(parmlist[[pp]]) parm[ parm_index_pp$index ] <- x } return(parm) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_collect_parameters.R
## File Name: cdm_pem_create_parameter_index.R ## File Version: 0.07 cdm_pem_create_parameter_index <- function( parmlist ) { NP <- length(parmlist) parm_index <- list() parmlist_names <- names(parmlist) parm_index_names <- rep("", NP) last_index <- 0 for (pp in 1:NP){ x <- parmlist[[pp]] x_pp <- list() parm_index_names[pp] <- parmlist_names[pp] x_dim <- cdm_pem_extract_dimension(x=x) NX <- prod(x_dim) x_index <- last_index + seq( 1, NX ) x_pp$index <- x_index last_index <- max(x_index) x_pp$dim <- x_dim parm_index[[pp]] <- x_pp } names(parm_index) <- parm_index_names #-- more meta-information about parameters parm_index[["__n_parameters"]] <- NP parm_index[["__length_parameter_vector"]] <- last_index return(parm_index) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_create_parameter_index.R
## File Name: cdm_pem_extract_dimension.R ## File Version: 0.01 cdm_pem_extract_dimension <- function(x) { x_dim <- NULL if ( is.vector(x) ){ x_dim <- length(x) } if ( is.matrix(x) | is.array(x) | is.data.frame(x) ){ x_dim <- dim(x) } return(x_dim) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_extract_dimension.R
## File Name: cdm_pem_extract_parameters.R ## File Version: 0.01 cdm_pem_extract_parameters <- function( parm, parmgroup, pem_parameter_index ) { info <- pem_parameter_index[[ parmgroup ]] x_dim <- info$dim x <- parm[ info$index ] x <- cdm_pem_adjust_dimension(x=x, x_dim=x_dim ) return(x) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_extract_parameters.R
## File Name: cdm_pem_include_ll_args.R ## File Version: 0.032 cdm_pem_include_ll_args <- function(ll_args, pem_parm, pem_pars, pem_parameter_index) { for (pp in pem_pars){ ll_args[[ pp ]] <- cdm_pem_extract_parameters( parm=pem_parm, parmgroup=pp, pem_parameter_index=pem_parameter_index ) } return(ll_args) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_include_ll_args.R
## File Name: cdm_pem_inits.R ## File Version: 0.05 cdm_pem_inits <- function( parmlist ) { pem_parameter_index <- cdm_pem_create_parameter_index( parmlist=parmlist ) pem_parameter_sequence <- list() #--- output res <- list(pem_parameter_index=pem_parameter_index, pem_parameter_sequence=pem_parameter_sequence ) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_inits.R
## File Name: cdm_pem_inits_assign_parmlist.R ## File Version: 0.02 cdm_pem_inits_assign_parmlist <- function(pem_pars, envir) { parmlist <- list() NP <- length(pem_pars) for (pp in 1:NP){ p1 <- get( pem_pars[pp], pos=envir ) parmlist[[ pp ]] <- p1 } names(parmlist) <- pem_pars return(parmlist) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_inits_assign_parmlist.R
## File Name: cdm_pem_parameter_sequence_initial_iterations.R ## File Version: 0.04 cdm_pem_parameter_sequence_initial_iterations <- function( pem_parm, pem_parameter_sequence, iter ) { if (iter < 3){ for (ii in 0:2){ if (iter==ii){ pem_parameter_sequence[[ paste0("P",ii) ]] <- pem_parm } } } return(pem_parameter_sequence) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_pem_parameter_sequence_initial_iterations.R
## File Name: cdm_penalty_threshold_elnet.R ## File Version: 0.05 cdm_penalty_threshold_elnet <- function( beta, lambda, alpha ) { lam1 <- lambda * alpha lam2 <- lambda * ( 1 - alpha ) res <- cdm_soft_threshold( val=beta, eta=lam1 ) res <- res / ( 1 + 2*lam2 ) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_threshold_elnet.R
## File Name: cdm_penalty_threshold_lasso.R ## File Version: 0.05 cdm_penalty_threshold_lasso <- function( val, eta ) { res <- val res <- ifelse( abs(val) < eta, 0, res ) res <- ifelse( val > eta, val - eta, res ) res <- ifelse( val < - eta, val + eta, res ) return(res) } cdm_soft_threshold <- cdm_penalty_threshold_lasso
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_threshold_lasso.R
## File Name: cdm_penalty_threshold_mcp.R ## File Version: 0.04 cdm_penalty_threshold_mcp <- function(beta, lambda, a=3.7) { y <- cdm_penalty_threshold_lasso(val=beta, eta=lambda ) / ( 1 - 1/ a ) y <- ifelse( abs(beta) > a*lambda, beta, y ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_threshold_mcp.R
## File Name: cdm_penalty_threshold_ridge.R ## File Version: 0.03 cdm_penalty_threshold_ridge <- function(beta, lambda) { y <- beta / ( 1 + 2*lambda ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_threshold_ridge.R
## File Name: cdm_penalty_threshold_scad.R ## File Version: 0.05 cdm_penalty_threshold_scad <- function(beta, lambda, a=3.7) { sign_beta <- sign(beta) #-- 2*lambda < abs(beta) < a*lambda y <- ( ( a - 1 ) * beta - sign_beta * a * lambda ) / ( a - 2 ) #-- abs(beta) > a*lambda y <- ifelse( abs(beta) > a*lambda, beta, y ) #-- abs(beta) < 2*lambda z <- ( abs(beta) - lambda ) z <- ifelse( z < 0, 0, z ) y <- ifelse( abs(beta) < 2*lambda, z*sign_beta, y ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_threshold_scad.R
## File Name: cdm_penalty_threshold_scadL2.R ## File Version: 0.05 cdm_penalty_threshold_scadL2 <- function(beta, lambda, alpha, a=3.7) { lam1 <- lambda*alpha lam2 <- lambda*(1-alpha) sign_beta <- sign(beta) #-- 2*lambda < abs(beta) < a*lambda y <- ( ( a - 1 ) * beta - sign_beta * a * lam1 ) / ( -1 + ( a - 1 )*(1+2*lam2) ) #-- abs(beta) > a*lambda y <- ifelse( abs(beta) > a*lambda, beta / ( 1 + 2*lam2), y ) #-- abs(beta) < 2*lambda z <- ( abs(beta) - lambda ) z <- ifelse( z < 0, 0, z ) y <- ifelse( abs(beta) < 2*lambda, z*sign_beta / ( 1 + 2*lam2), y ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_threshold_scadL2.R
## File Name: cdm_penalty_threshold_tlp.R ## File Version: 0.06 cdm_penalty_threshold_tlp <- function( beta, tau, lambda ) { lambda_j <- lambda / tau * ( abs(beta) < tau ) res <- cdm_penalty_threshold_lasso( val=beta, eta=lambda_j) return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_threshold_tlp.R
## File Name: cdm_penalty_values.R ## File Version: 0.202 cdm_penalty_values <- function(x, regular_type, regular_lam, regular_tau=NULL, regular_alpha=NULL) { penalty <- 0 if (regular_type=="scad"){ penalty <- cdm_penalty_values_scad( x=x, lambda=regular_lam ) } if (regular_type=="lasso"){ penalty <- cdm_penalty_values_lasso( x=x, lambda=regular_lam ) } if (regular_type=="ridge"){ penalty <- cdm_penalty_values_ridge( x=x, lambda=regular_lam ) } if (regular_type=="elnet"){ penalty <- cdm_penalty_values_elnet( x=x, lambda=regular_lam, alpha=regular_alpha ) } if (regular_type=="scadL2"){ penalty <- cdm_penalty_values_scadL2( x=x, lambda=regular_lam, alpha=regular_alpha ) } if (regular_type=="tlp"){ penalty <- cdm_penalty_values_tlp_approximation( x=x, lambda=regular_lam, tau=regular_tau ) } if (regular_type=="mcp"){ penalty <- cdm_penalty_values_mcp( x=x, lambda=regular_lam ) } return(penalty) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values.R
## File Name: cdm_penalty_values_elnet.R ## File Version: 0.02 cdm_penalty_values_elnet <- function( x, lambda, alpha ) { lam1 <- lambda * alpha lam2 <- lambda * ( 1 - alpha ) y <- lam1 * abs(x) + lam2 * x^2 return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_elnet.R
## File Name: cdm_penalty_values_lasso.R ## File Version: 0.05 cdm_penalty_values_lasso <- function( x, lambda ) { y <- lambda * abs(x) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_lasso.R
## File Name: cdm_penalty_values_mcp.R ## File Version: 0.05 cdm_penalty_values_mcp <- function( x, lambda, a=3.7 ) { y <- lambda * abs(x) - x^2 / ( 2 * a ) y <- ifelse( abs(x) > a*lambda, a * lambda^2 / 2, y ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_mcp.R
## File Name: cdm_penalty_values_ridge.R ## File Version: 0.02 cdm_penalty_values_ridge <- function( x, lambda ) { y <- lambda * x^2 return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_ridge.R
## File Name: cdm_penalty_values_scad.R ## File Version: 0.18 cdm_penalty_values_scad <- function( x, lambda, a=3.7 ) { y <- lambda * abs(x) y <- ifelse( ( abs(x) > lambda ) & ( abs(x) < a*lambda ), -( x^2 - 2*a*lambda*abs(x) + lambda^2 )/ 2 / ( a-1), y ) y <- ifelse( abs(x) > a*lambda, (a+1) * lambda^2 / 2, y ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_scad.R
## File Name: cdm_penalty_values_scadL2.R ## File Version: 0.03 cdm_penalty_values_scadL2 <- function( x, lambda, alpha ) { lam1 <- lambda * alpha lam2 <- lambda * ( 1 - alpha ) y <- cdm_penalty_values_scad(x=x, lambda=lam1) + lam2*x^2 return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_scadL2.R
## File Name: cdm_penalty_values_tlp.R ## File Version: 0.04 cdm_penalty_values_tlp <- function(x, tau ) { y <- abs(x) / tau y <- ifelse( y > 1, 1, y ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_tlp.R
## File Name: cdm_penalty_values_tlp_approximation.R ## File Version: 0.10 cdm_penalty_values_tlp_approximation <- function(x, tau, lambda ) { y <- abs(x) / tau J1 <- y J2 <- y - ifelse( y - 1 < 0, y, 1 ) z <- lambda*(J1 - J2) return(z) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_penalty_values_tlp_approximation.R
## File Name: cdm_positivity_restriction.R ## File Version: 0.02 cdm_positivity_restriction <- function(x, positive) { x <- ifelse( ( x < 0 ) & positive, 0, x ) return(x) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_positivity_restriction.R
## File Name: cdm_print_summary_call.R ## File Version: 0.05 cdm_print_summary_call <- function(object, call_name="call") { CALL <- object[[ call_name ]] s3 <- paste0(CALL, collapse=" ") if ( nchar(s3) < 3000 ){ cat("Call:\n") print( CALL ) cat("\n") } }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_print_summary_call.R
## File Name: cdm_print_summary_computation_time.R ## File Version: 0.04 cdm_print_summary_computation_time <- function(object, time_name="time", time_start="s1", time_end="s2") { comp_time <- object[[ time_name ]] s1 <- comp_time[[ time_start ]] s2 <- comp_time[[ time_end ]] cat( "Date of Analysis:", paste( s2 ), "\n" ) cat("Computation Time:", print( s2 - s1), "\n\n") }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_print_summary_computation_time.R
## File Name: cdm_print_summary_data_frame.R ## File Version: 0.072 cdm_print_summary_data_frame <- function(obji, from=NULL, to=NULL, digits=3, rownames_null=FALSE) { if (is.vector(obji)){ obji <- round(obji, digits) } else { if (is.null(from)){ from <- 1 } if (is.null(to)){ to <- ncol(obji) } ind <- seq( from, to ) for (vv in ind){ obji_vv <- obji[,vv] if ( is.numeric(obji_vv) ){ obji[, vv ] <- round( obji_vv, digits ) } } } if (rownames_null){ rownames(obji) <- NULL } print(obji) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_print_summary_data_frame.R
## File Name: cdm_print_summary_information_criteria.R ## File Version: 0.212 cdm_print_summary_information_criteria <- function(object, digits_crit=0, digits_penalty=2) { deviance <- object$ic$deviance #*** labels information criteria labels <- list( AIC=paste0("AIC ","=", " -2*LL + 2*p" ) ) labels$AICc <- paste0("AICc ","=", " -2*LL + 2*p + 2*p*(p+1)/(n-p-1) (bias corrected AIC)" ) labels$BIC <- paste0("BIC ","=", " -2*LL + log(n)*p " ) labels$CAIC <- paste0("CAIC ","=", " -2*LL + [log(n)+1]*p (consistent AIC) " ) #*** display criteria crits <- intersect(names(labels), names(object$ic)) for (crit_name in crits){ res <- cdm_print_summary_information_criteria_one_criterium( object=object, crit_name=crit_name, labels=labels, digits_crit=digits_crit, digits_penalty=digits_penalty ) } cat("\n") }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_print_summary_information_criteria.R
## File Name: cdm_print_summary_information_criteria_one_criterium.R ## File Version: 0.042 cdm_print_summary_information_criteria_one_criterium <- function(object, crit_name, labels, digits_crit, digits_penalty) { deviance <- object$ic$deviance crit <- object$ic[[ crit_name ]] cat( crit_name, "=", round( crit, digits_crit ), " | penalty", "=", round( crit - deviance, digits_penalty ), " |", labels[[ crit_name ]], " \n" ) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_print_summary_information_criteria_one_criterium.R
## File Name: cdm_print_summary_package.R ## File Version: 0.03 cdm_print_summary_package <- function(pack="CDM") { d1 <- utils::packageDescription(pack) cat( paste( d1$Package, " ", d1$Version, " (", d1$Date, ")", sep=""), "\n" ) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_print_summary_package.R
## File Name: cdm_replace_inf.R ## File Version: 0.04 cdm_replace_inf <- function(x) { x[ x==Inf ] <- NA return(x) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_replace_inf.R
## File Name: cdm_shrink_positive.R ## File Version: 0.02 cdm_shrink_positive <- function(x, sum1=TRUE) { y <- ifelse(x<0, 0, x) if (sum1){ y <- y / sum(y) } return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_shrink_positive.R
## File Name: cdm_sort_unique.R ## File Version: 0.01 cdm_sort_unique <- function(x) { if( ! ( is.numeric(x) ) ){ gr2 <- unique( sort(paste(x) )) } else { gr2 <- unique( sort(x) ) } return(gr2) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_sort_unique.R
## File Name: cdm_squeeze.R ## File Version: 0.05 #********************************* # copied squeeze function from mice package cdm_squeeze <- function (x, bounds ) { x[x < bounds[1] ] <- bounds[1] x[x > bounds[2] ] <- bounds[2] return(x) } squeeze.cdm <- cdm_squeeze
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_squeeze.R
## File Name: cdm_summary_display.R ## File Version: 0.01 cdm_summary_display <- function(symbol="-", len=65) { res <- paste0( paste0( rep(symbol, len), collapse="" ), "\n") return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_summary_display.R
## File Name: cdm_sumnorm.R ## File Version: 0.02 cdm_sumnorm <- function(vec, norm=1) { vec <- as.vector(vec) res <- vec / sum(vec) * norm return(res) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_sumnorm.R
## File Name: cdm_sumnorm_squeeze.R ## File Version: 0.01 cdm_sumnorm_squeeze <- function(vec, bounds) { y <- cdm_sumnorm( vec=cdm_squeeze(x=vec, bounds=bounds) ) return(y) }
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_sumnorm_squeeze.R
## File Name: cdm_tetrachoric.R ## File Version: 0.20 #################################################################### # hogdina: tetrachoric correlation cdm_tetrachoric <- function( dat, weights, rho_init=NULL, delta=.007, maxit=10000, maxval=.999 ) { # process data dat <- as.matrix(dat) # calculate tau tau <- - stats::qnorm( colSums( dat * weights ) / sum( weights ) ) w2 <- sqrt(weights) dat.resp <- 1 - is.na(dat) dat[ dat.resp==0] <- 9 I <- ncol(dat) # calculate frequencies dfr <- data.frame( "item1"=rep(1:I,I), "item2"=rep(1:I, each=I ) ) h1 <- crossprod( 1*(dat==1), (dat==0) ) dfr$f11 <- matrix( crossprod( ( dat==1 )*w2, (w2*( dat==1 ))), ncol=1, byrow=TRUE ) dfr$f10 <- matrix( crossprod( ( dat==1 )*w2, (w2*( dat==0 ))), ncol=1, byrow=TRUE ) dfr$f01 <- matrix( crossprod( ( dat==0 )*w2, (w2*( dat==1 ))), ncol=1, byrow=TRUE ) dfr$f00 <- matrix( crossprod( ( dat==0 )*w2, (w2*( dat==0 ))), ncol=1, byrow=TRUE ) dfr$ftot <- dfr$f11 + dfr$f10 + dfr$f01 + dfr$f00 dfr$p11 <- dfr$f11 / dfr$ftot dfr$pi1 <- ( dfr$f11 + dfr$f10 ) / dfr$ftot dfr$pi2 <- ( dfr$f11 + dfr$f01 ) / dfr$ftot # subdataset of dfr dfr <- dfr[ dfr$item1 > dfr$item2, ] dfr <- dfr[ dfr$ftot > 0, ] dfr$qi1 <- stats::qnorm( dfr$pi1) dfr$qi2 <- stats::qnorm( dfr$pi2) # functions defined by Cengiz Zopluoglu L <- function(r,h,k) { (1/(2*pi*sqrt(1-r^2)))*exp(-((h^2-2*h*k*r+k^2)/(2*(1-r^2)))) } S <- function(x,r,h,k) { x-(L(r,h,k)*delta) } A0 <- dfr$A0 <- dfr$p11 - dfr$pi1 * dfr$pi2 dfr$r0 <- delta / 2 #-- include inits if provided if ( ( ! is.null(rho_init) ) & ( is.matrix(rho_init) ) ){ dfr$r0 <- rho_init[ lower.tri(rho_init) ] dfr$r0 <- ifelse( dfr$r0==maxval, maxval - 10*( 1 - maxval ), dfr$r0 ) } dfr$iter <- 0 dfr$iter <- 0 dfr$conv <- 0 ii <- 0 vars <- c("A0","r0","iter","conv") while( ( mean( dfr$conv) < 1 ) & ( ii < maxit ) ){ # iterations dfr0 <- dfr #*** ii <- ii+1 dfr$A0 <- dfr$A0 - delta * L( dfr$r0, dfr$qi1, dfr$qi2 ) dfr$r0 <- dfr$r0 + delta dfr$iter <- ii ind <- which( dfr$A0 < 0 ) if ( length(ind) > 0 ){ h1 <- dfr$r0 - delta/2 + dfr$A0 / L( dfr0$r0, dfr$qi1, dfr$qi2 ) dfr$r0[ind] <- h1[ind] dfr$conv[ind] <- 1 } i2 <- which( dfr$conv==1 & dfr0$conv==1 ) if (length(i2) > 0){ dfr[ i2, vars] <- dfr0[ i2, vars] } dfr$r0 <- ifelse( abs(dfr$r0) > maxval, sign(dfr$r0)*maxval, dfr$r0 ) dfr$conv <- ifelse( abs(dfr$r0) >=maxval, 1, dfr$conv ) } TC <- matrix(NA, I, I ) diag(TC) <- 1 TC[ as.matrix(dfr[, c("item1","item2") ] ) ] <- dfr$r0 TC[ as.matrix(dfr[, c("item2","item1") ] ) ] <- dfr$r0 res <- list("tau"=tau, "rho"=TC ) return(res) } .tetrachoric.hogdina <- cdm_tetrachoric
/scratch/gouwar.j/cran-all/cranData/CDM/R/cdm_tetrachoric.R