need help writing function to calculate Cronbach's alpha. No error message, but also no output in console

EDIT again: SOLUTION: added a * to multiply and created a new project which helped with the console not even giving me outputs for the simplest of code.
EDIT: I am a beginner. I have no idea what I'm doing wrong. R is not creating the function in the environment when I run the code, so I guess that is why it is not working, but I do not know why. This is for a homework assignment, so I need to create a function and not use a cronbachs alpha function that already exists.

Based on my calculations on paper, my answer should be 0.54347826

# Create Data frame
data <- data.frame(item1 = c(0,0,0,1,0),
                   item2 = c(0,0,1,1,1),
                   item3 = c(0,1,1,1,1),
                   item4 = c(0,1,1,1,0),
                   item5 = c(0,1,0,0,1))

#Add new column that is sum of each row
data$total <- data$item1 + data$item2 + data$item3 + data$item4 + data$item5

#calculate item and total variance

# Create function to calculate cronbach's alpha
alpha.function <- function(v1,v2,v3,v4,v5,vt){
  alpha <- (5/(5-1))(1-(v1+v2+v3+v4+v5)/(vt))

alpha.function(var1, var2, var3, var4, var5, var.t)

Is this in namespace?

perhaps this should be:

alpha.function <- function(v1,v2,v3,v4,v5,vt){
  alpha <- (5/(5-1))*(1-(v1+v2+v3+v4+v5)/(vt))

But even then it needs a bit more work to match the formula you want: reliability - How to compute Cronbach's alpha with only one measurement per subject? - Cross Validated (

I think I left out things in the post! But I tried that and am still getting nothing. I tried testing creating a different simple function from a youtube video example and still nothing.... I think something is wrong with my R I don't know. I may post again that has all the information

Do you really want to program a Cronbach's Alpha or do you just need to calculate one for some test data?

If the first a google inquiry will turn up several leads.

If you need to calculate a cronbach's alpha. the R packcage psych will do this nicely. See alpha function - RDocumentation

1 Like

I have to create a function for homework. I have updated the post just now so it should be easier to understand what I'm dealing with now.

1 Like

Your updated post works with this:

# Create function to calculate cronbach's alpha
alpha.function <- function(v1,v2,v3,v4,v5,vt){
  alpha <- (5/(5-1))*(1-(v1+v2+v3+v4+v5)/(vt)) # added the *

alpha.function(var1, var2, var3, var4, var5, var.t)
# [1] 0.5434783



# Cronbach's alpha for the 'data[1:5]' data-set
# Items: 5
# Sample units: 5
# alpha: 0.543
1 Like

Hooray! Okay, so apparently I wasn't getting an error message or any output for some weird reason. I created a new R project and now my code is working again. You found the * error that was not causing the main issue, but would have caused another issue after I solved this one. Thank you!!!!!

1 Like

This topic was automatically closed 7 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.