library(shiny) library(eRm) # Create RM analysis module UI rmanalysisModuleUI <- function(id) { ns <- NS(id) tagList( sidebarLayout( sidebarPanel( actionButton(ns("run_analysis"), "Run the Code"), br(), checkboxInput(ns("show_difficulty"), "Item Difficulty"), checkboxInput(ns("show_fit_stats"), "Item Fit Statistics"), checkboxInput(ns("show_separation"), "Reliability Statistics"), checkboxInput(ns("show_theta"), "Person Measures"), checkboxInput(ns("show_personfit"), "Person Fit Statistics"), checkboxInput(ns("show_norm"), "Percentile Norms"), checkboxInput(ns("show_pca"), "Residual Principal Component Analysis"), ), mainPanel( h3("RM Analysis Output"), verbatimTextOutput(ns("output")) ) ) ) } # Create RM analysis module server rmanalysisModuleServer <- function(id) { moduleServer( id, function(input, output, session) { data <- reactiveValues(data = NULL) observeEvent(input$file, { req(input$file) df <- read.csv(input$file$datapath, header = TRUE, row.names = 1) data$data <- df }) output$output <- renderText({ req(data$data) res <- RM(data$data, se = TRUE) output_text <- character() #get fit p.res <- person.parameter(res) pmt<-pmat(p.res) residuals(p.res) i.fit<-itemfit(p.res) p.fit <-personfit(p.res) #get item parameters ease<- res$betapar beta<- res$betapar * -1 beta.se<- res$se.beta #get person parameters theta<-(p.res$thetapar) theta.se<-(p.res$se.theta) if (input$show_difficulty) { disc<-i.fit$i.disc idisc <- data.frame(disc) item.stat <- cbind(beta, beta.se, idisc) colnames(item.stat) <- c("Item Difficulty","Standard Error","Point-Measure Corr") print(item.stat) cat("\n") } if (input$show_fit_stats) { print(i.fit) cat("\n") } if (input$show_separation) { #Person reliability pscores<-unlist(theta) p.se<-unlist(theta.se) SSD.scores<-var(pscores) person.MSE <- sum((p.se)^2) / length(p.se) person.separation.rel <- (SSD.scores-person.MSE) / SSD.scores c.alpha <- cronbach.alpha(data(), standardized = TRUE) alpha<-c.alpha$alpha sd_scores<-sd(pscores) sem <- sd_scores/ sqrt(length(pscores)) per.rel<- data.frame( c("Person Separation:", "Standard Square Deviations:", "Mean Square Error Variance:", "Cronbach Alpha:", "Standard Error of Measurement:"), c(person.separation.rel, SSD.scores, person.MSE,alpha,sem) ) colnames(per.rel)<- NULL cat("Person Reliability Statistics\n") print(per.rel, row.names=FALSE) cat("\n") #Item reliability item.dif<- unlist(beta) i.se<-unlist(beta.se) SSD.items<-var(item.dif) item.MSE <- sum((i.se)^2) / length(i.se) item.separation.rel <- (SSD.items-item.MSE) / SSD.items item.sep <- data.frame( c("Item Separation:", "Standard Square Deviations:", "Mean Square Error Variance:"), c(item.separation.rel, SSD.items, item.MSE) ) colnames(item.sep)<- NULL cat("Item Reliability Statistics\n") print(item.sep, row.names=FALSE) cat("\n") } if (input$show_theta) { summary(p.res) cat("\n") } if (input$show_personfit) { print(p.fit) cat("\n") } if (input$show_norm) { rawscore<-rowSums(data()) pertile <- seq(1, 99) pertile_raw <- quantile(rawscore, probs = pertile/100) raw_rounded <- round(pertile_raw, digits = 0) theta<-unlist(p.res$thetapar) pertile <- seq(1, 99) pertile_theta <- round((quantile(theta, probs = pertile/100)),digits=3) #rescale to 100 sf100 <- 100 / (max(theta)- min(theta)) rs100 <- (theta - min(theta)) * sf100 + 0 pertile <- seq(1, 99) pertile100 <- round((quantile(rs100, probs = pertile/100)),digits=0) #rescale to 500_100 sf500 <- 400 / (max(theta)- min(theta)) rs500 <- (theta - min(theta)) * sf500 + 100 pertile <- seq(1, 99) pertile500 <- round((quantile(rs500, probs = pertile/100)),digits=0) percentile_table <- data.frame("Raw Score" = raw_rounded, Theta = pertile_theta, "ReScaled 100" =pertile100, "ReScaled 500" =pertile500, "Percentile Rank" = pertile) print(percentile_table, row.names = FALSE) cat("\n") } if (input$show_pca) { std.resids<-i.fit$st.res pca <- pca(std.resids, nfactors = ncol(data()), rotate = "none") eigenvalues <- pca$values eigen.df <- data.frame("Eigenvalues" = eigenvalues) rownames(eigen.df) <- colnames(pca$loadings) indices <- which(eigenvalues >= 2) if (length(indices) == 0) { print("No component with eigenvalue greater than 2") } else { pca_eigen2 <- matrix(pca$loadings[, indices], ncol = 1) colnames(pca_eigen2) <- rownames(eigen.df)[indices] pca_eigen2_load <- cbind(pca_eigen2) cat("Components with Eigenvalue > 2:\n") print(pca_eigen2_load) } cat("\n") print(eigen.df) } cat(output_text, sep = "\n") }) }) }
The function add takes two arguments a and b and returns the sum of a and b.