Psych Library polychoric Function Error "You have more than 8 categories for your items, polychoric is probably not needed"
I'm new to R and running into an error with the polychoric
function in the psych
package. I'm attempting to store the polychoric correlation matrix in a data frame from using the following syntax:
RPOL36 < polychoric(norm.kdqol36,smooth=TRUE)
where norm.kdqol36
is a data frame with vectors of ordered variables, each with 5 levels (0, 25, 50, 75, 100). Here is an example:
0 25 50 75 100
11962 19953 4987 12998 8261
Despite each variable having 5 levels, I get this error:
Error in polychoric(norm.kdqol36, smooth = TRUE) : You have more than 8 categories for your items, polychoric is probably not needed
Could there be a formatting issue causing the polychoric
function to read my variables as having more than 5 categories?
See also questions close to this topic

Warnings when restoring graphical parameters
I am writing my first R package and currently working on a function to make a plot using some particular graphical parameters. I want the user defined graphical parameters to get restored after the plot is made but always get same warning messages:
opar < par() par(oma = c(5, 4, 0, 0) + 0.1, mar = c(0, 0, 1, 1) + 0.1) par(opar)
Warning messages:
1: In par(opar) : graphical parameter "cin" cannot be set
2: In par(opar) : graphical parameter "cra" cannot be set
3: In par(opar) : graphical parameter "csi" cannot be set
4: In par(opar) : graphical parameter "cxy" cannot be set
5: In par(opar) : graphical parameter "din" cannot be set
6: In par(opar) : graphical parameter "page" cannot be setIs there a better way of doing that? I know the
suppressWarnings()
function but 1. I don't want the messages to get hided and 2. if the function is called two times, a warning message appears:> There were 12 warnings (use warnings() to see them)

Fail to authenticate BigQuery with R under the bigrquery package
I am trying to use
set_service_token
in thebigrquery
package for a noninteractive authentication.Here is my code:
library(bigrquery) set_service_token("client_secret.json")
But it kept showing the error message below:
Error in read_input(file) :
file must be connection, raw vector or file pathHowever, when I simply to read the JSON path, it works:
lapply(fromJSON("client_secret.json"), names)
$`installed`
[1] "client_id" "project_id" "auth_uri" "token_uri" "auth_provider_x509_cert_url" "client_secret" "redirect_uris"Can anyone help me with this? Thank you very much!

Plotting mutiple graph in one graph in R with using function
I am trying to plot a graph in one plot only. I have 4 different plots coming by using a function. This is my code:
hazard.plot.w2p(beta = beta.spreda, eta = eta.spreda, time = exa1.dat$time, line.colour = "blue") hazard.plot.w2p(beta = 1.076429, eta = 26.21113, time = exa1.dat$time, line.colour = "blue") hazard.plot.w2p(beta = 5, eta = 32.97954, time = exa1.dat$time, line.colour = "blue") hazard.plot.w2p(beta = 2, eta = 32.9795, time = exa1.dat$time, line.colour = "blue")
Here is a function i used to get output:
hazard.plot.w2p < function(beta, eta, time, line.colour, nincr = 500) { max.time < max(time, na.rm = F) t < seq(0, max.time, length.out = nincr) r < numeric(length(t)) for (i in 1:length(t)) { r[i] < failure.rate.w2p(beta, eta, t[i]) } plot(t, r, type = 'l', bty = 'l', col = line.colour, lwd = 2, main = "", xlab = "Time", ylab = "Failure rate", las = 1, adj = 0.5, cex.axis = 0.85, cex.lab = 1.2) }
I want to plot all the 4 plots in one plot only.
Here is a sample data set:
fail time a 4.55 a 4.65 a 5.21 b 3.21 a 1.21 a 5.65 a 7.12

chart.correlation or pairs.panels NxN view with multiple groups
Good morning, I was wondering if it is possible to visualise a two correlation matrices in one NxN chart as I am used to doing with chart.correlation from PerformanceAnalytics, or pairs.panels from Psych? One of my dataframe's columns includes a group denominator, e.g. a factor with 2 levels, "patients" and "controls". Would it be possible to get 1 NxN chart which would plot "patient" rows with one type of symbol/line and "controls" with another, and the other half of the chart would include R values and significances for both groups separately? Or is my only feasible choice to construct separate NxN charts and compare them myself?
Thank you very much for your insight!

Using tidyverse to get raw_alpha values with the psych package from different datasets / databases
I spent one day looking for this answer and I'm almost giving up. Actually, I really imagine is a pretty simple situation, but I'll be glad of any help.
Let's say I have two datasets, the first get all ID of all students
library(tidyverse) library(psych) ds_of_students < data.frame(id=(1:4), school=c("public","private"))
The second one has all the results of a test. Let's say each column is an ID.
ds_of_results < structure(list(i1 = c(1, 2, 4, 4), i2 = c(3, 3, 2, 2), i3 = c(2, 3, 3, 5), i4 = c(4, 1, 3, 2)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 4L))
And now I need to report a table of students ID , groupped by school, and they results (Actually, It's a Cronbach alpha results, what is pretty common in Psychology).
ds_of_students %>% group_by(school) %>% summarise(n=n(), id = paste(id, collapse = ",")) %>% mutate(item2=psych::alpha(ds_of_results[c(id)])$total[1])
I've got this message
Error in mutate_impl(.data, dots) : Evaluation error: Columns `2,4`, `1,3` not found.
But When I run in the traditional fashion, it works
psych::alpha(ds_of_results[c(1,3)])$total[1]
I've tried to work with paste, noquote, gsub ans strcol
Please, run this code to have reproducible results. Thanks much!
library(tidyverse) library(psych) ds_of_students < data.frame(id=(1:4), school=c("public","private")) ds_of_results < structure(list(i1 = c(1, 2, 4, 4), i2 = c(3, 3, 2, 2), i3 = c(2, 3, 3, 5), i4 = c(4, 1, 3, 2)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 4L)) ds_of_students %>% group_by(school) %>% summarise(n=n(), id = paste(id, collapse = ",")) %>% mutate(item2=psych::alpha(ds_of_results[c(id)])$total[1]) alpha(ds_of_results[c(1,3)])$total[1]
My desired output is something like that
And just to give some reality to my question, that's the real dataset, where I have to compute the Cronbach's alpha item the items of each group.

Factor scores from factor analysis on ordinal categorical data in R
I'm having trouble computing factor scores from an exploratory factor analysis on ordered categorical data. I've managed to assess how many factors to draw, and to run the factor analysis using the psych package, but can't figure out how to get factor scores for individual participants, and haven't found much help online. Here is where I'm stuck:
library(polycor) library(nFactors) library(psych) # load data dat < read.csv("https://raw.githubusercontent.com/paulrconnor/datasets/master/data.csv") # convert to ordered factors for(i in 1:length(dat)){ dat[,i] < as.factor(dat[,i]) } # compute polychoric correlations pc < hetcor(dat,ML=T) # 2. choose number of factors ev < eigen(pc) ap < parallel(subject = nrow(dat), var=ncol(dat),rep=100,cent=.05) nS < nScree(x = ev$values, aparallel = ap$eigen$qevpea) dev.new(height=4,width=6,noRStudioGD = T) plotnScree(nS) # 2 factors, maybe 1 # run FA faPC < fa(r=pc$correlations, nfactors = 2, rotate="varimax",fm="ml") faPC$loadings
Edit: I've found a way to get scores using irt.fa() and scoreIrt(), but it involved converting my ordered categories to numeric so I'm not sure it's valid. Any advice would be much appreciated!
x = as.matrix(dat) fairt < irt.fa(x = x,nfactors=2,correct=TRUE,plot=TRUE,n.obs=NULL,rotate="varimax",fm="ml",sort=FALSE) for(i in 1:length(dat)){dat[,i] < as.numeric(dat[,i])} scoreIrt(stats = fairt, items = dat, cut = 0.2, mod="logistic")