R studio IRT model to Excel

Hello ,

I have dichotomous 2 PL IRT model and its output. I want to get those outputs as an excel file i tried xlsx and even stargazer. However, i have a certain problem to get those results in a data.frame function if you would help i would be really appriciated.

Here is the codes in case you need to check:

install.packages("mvtnorm")
install.packages("msm")
install.packages('mirt')
install.packages("ltm")
install.packages("psych")



library(ltm)
library(mirt)
library(psych)

datapasta::df_paste(head(IRT, 6)[, c('item1', 'item2' ,'item3','item4', 'item5b', 'item6b')])
data.frame(
      item1 = c(0, 1, 1,0, 1, 1),
         item2 = c(0, 1, 1,1, 1, 1),
  item3 = c(0, 1, 1,0, 0, 1),
  item4 = c(0, 1, 0,0, 1, 1),
  item5b = c(1, 1, 1,0, 1, 0),
  item6b = c(1, 0, 1,0, 1, 1),
   )

Eldor <- na.omit(read.csv2('/Users/arinon/Desktop/Eldor veriler/Eldor2.csv'))
head(Eldor)

#Model and Fit
IRTmodel = ltm(Eldor ~ z1,IRT.param = TRUE)
summ <- summary(IRTmodel)
coef(IRTmodel)
item.fit(IRTmodel)

#Cronbach's alpha values
alpha(Eldor)

#Plots
plot(IRTmodel, type = 'ICC')

#Factor scores of the latent trait 
factor.scores(IRTmodel)

We really need reproducible example (reprex). Have a look at

A handy way to supply sample data is to use the dput() function. See ?dput. If you have a very large data set then something like head(dput(myfile), 100) will likely supply enough data for us to work with.

Hello, thanks for your reply i am new here excuse my lack of knowledge .I am uploading the data needed

Thanks for the data and code but it seems incomplete. Am I correct in assuming that ltm() comes from the ltm package?

You code says

IRTmodel = ltm(Eldor ~ z1,IRT.param = TRUE)

but there is no hint of where Eldor or z1

BTW your data.frame has one too many commas.

I believe it should be

IRT <-   data.frame( item1 = c(0, 1, 1,0, 1, 1),
 item2 = c(0, 1, 1,1, 1, 1),
  item3 = c(0, 1, 1,0, 0, 1),
  item4 = c(0, 1, 0,0, 1, 1),
  item5b = c(1, 1, 1,0, 1, 0),
  item6b = c(1, 0, 1,0, 1, 1)
)

Oh yes my bad not to add ltm packages and psych package. Eldor is the name of my original data so update the code again so sorry.

Okay. I assume that we can substitute what I have called IRT for Eldor. Where is z1? For that matter what is it? I have never used the ltm package. It looks like a vector but ....

We really need an actual working example. Something that will load the sample data, run the ltm function and give us some output or some error messages.

Would you include so examples of how you are trying to get your results into Excel?

Come to think of it, just what results do you want?

If your original ltm() equation is working trp

str(IRTmodel)

which should provide you with a list of the information that ltm() returns.

Thanks.

First of all thank you very much for your help.

z1 in otm function represents the latent variable scores. My main concern is I need the item.fit, factor.scores,plots and summary results into excel directly. For instance I can get results for factor.scores which is my main concern however, in order to include them into my study I need to adjusted over excel but Im not able to get those results into excel sadly.

Here is a quick example taken from the ltm manual

library(ltm)
dat1 <-  ltm(WIRS ~ z1, constr = rbind(c(1, 1, 1), c(6, 2, -0.5)))
dat1
str(dat1)

If you run it you get the basic and the list of the values that ltm returns. Which of them contain the factors that you need? Possibly the one called coefficients?

If so here is an example using an ltm function example from the manual where I look at what the function returns, extract the coefficients ant write them to an Excel file.


library(ltm)
library(xlsx)
dat1 <-  ltm(WIRS ~ z1, constr = rbind(c(1, 1, 1), c(6, 2, -0.5)))
dat1 # look at basic output
str(dat1) # look at all of the output data.

coef <-   dat1$coefficients

write.xlsx(coef, "Onarinon.coef1.xlsx")

I am not sure what entries in the results corresponds to your "item.fit, factor.scores, and summary results " but one should be able to pull out each one using the same method in my example.

This may be a stupid question but what can you do in Excel that you cannot do in R?

Re getting a plot into Excel I suppose you could save it it to disk and import is as a .png file.
Just do

plot(IRTmodel)

then right click on the plot and save it.

Thank you very much for your help! I will try these asap.

Well, for my thesis report i need to tidy them up so that the reason i need excel. And to add them in my report i also need Excel i guess. But if there is anything that might also be useful, i am very keen on learning.

Actually, probably not. You are almost certain to be able to produce thesis or report with tidied-up tables and plots using a combination of R and Rmarkdown or my old favorite combination of R and Lyx ( a Latex based document processor). Rmarkdown is remarkably easy to use and "runs" from within RStudio. See https://rmarkdown.rstudio.com/ for a bit of information.

Both the Rmarkdown and "lyx + R" approaches have the advantage that text and code are all in one document or file. It really reduces problems such as errors in copy & pastes or updating tables, plots and so on if you suddenly need to update a data file or change an analysis.

However, if you are under time pressures to complete the thesis or report it may not be the time to try learning a new authoring system. In the longer term though Rmarkdown &/or Lyx + R are the ways to go.

Best wishes on the thesis.

Thank you very very much !

Rmarkdown is such a facilitating tool. It saved lots of time using this right now with knitter package. However, i am sti,ll struglling with the factor scores. I just couldnt manage to get all the output to a tidy table format with kable function. I believe the problem occurs because i am not able to create a full data frame with the results. But since they are too many how may i able to create the data frame out of them . Is there a an easy way to do so via kable function.

I do not know as I seldom have used kable but there probably is

I still am not clear on which are the factor scores,. Can you post them or if we run this code:


library(ltm)
library(xlsx)
dat1 <-  ltm(WIRS ~ z1, constr = rbind(c(1, 1, 1), c(6, 2, -0.5)))
dat1 # look at basic output
str(dat1) # look at all of the output data.

What do you want in the table? The output from dat1?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.