# Chronbachs Alpha calculation

I am trying to add the Chronbachs Alpha calculation to every line.It should be a rolling calculation based on every 2 sales values.
so ,one calc for item 1 and item 2 ,then a separate one for item 2 and 3 ,for 3 and 4 ,etc.etc
so ,the Alpha calc should be instead of value ' z = 40/2.'
how do i put the Chronbachs Alpha code in that column Z ?

I have the following code:
input

``````df <- tibble::tribble(
~seq, ~date, ~sales,
1, "3/01/2017", 40,
2, "4/01/2017", 2,
3, "5/01/2017", 2,
4, "6/01/2017", 2,
5, "7/01/2017", 30,
6, "8/01/2017", 2,
7, "1/02/2017", 9,
8, "2/02/2017", 5,
9, "3/02/2017", 65,
10, "4/02/2017", 3,
11, "5/02/2017", 65
)
library(tibble)
add_column(df, z = 40/2, w = 1)

output

# A tibble: 11 x 5
seq date      sales     z     w
1 1 3/01/2017 40 20 1
2 2 4/01/2017 2 20 1
3 3 5/01/2017 2 20 1
4 4 6/01/2017 2 20 1
5 5 7/01/2017 30 20 1
6 6 8/01/2017 2 20 1
7 7 1/02/2017 9 20 1
8 8 2/02/2017 5 20 1
9 9 3/02/2017 65 20 1
10 10 4/02/2017 3 20 1
11 11 5/02/2017 65 20 1

library(psy)
data(expsy)
cronbach(cbind(expsy[,c(1,3:10)],-1*expsy[,2]))``````

The cronbach alpha for any two values, is meaningless, how could it not be ?
but here is how that would be demonstrated...

``````df <- tibble::tribble(
~seq, ~date, ~sales,
1, "3/01/2017", 40,
2, "4/01/2017", 2,
3, "5/01/2017", 2,
4, "6/01/2017", 2,
5, "7/01/2017", 30,
6, "8/01/2017", 2,
7, "1/02/2017", 9,
8, "2/02/2017", 5,
9, "3/02/2017", 65,
10, "4/02/2017", 3,
11, "5/02/2017", 65
)
library(tidyverse)
library(magrittr)
library(psy)

df %<>% mutate(lagsales = lag(sales))
df2 <- rowwise(df) %>%  mutate(z = cronbach(cbind(sales,lagsales))\$alpha) %>% ungroup

df2
# # A tibble: 11 x 5
# seq date      sales lagsales     z
# <dbl> <chr>     <dbl>    <dbl> <dbl>
#  1 3/01/2017    40       NA    NA
#  2 4/01/2017     2       40    NA
#  3 5/01/2017     2        2    NA
#  4 6/01/2017     2        2    NA
#  5 7/01/2017    30        2    NA
#  6 8/01/2017     2       30    NA
#  7 1/02/2017     9        2    NA
#  8 2/02/2017     5        9    NA
#  9 3/02/2017    65        5    NA
#  10 4/02/2017     3       65    NA
#  11 5/02/2017    65        3    NA
``````

here the cronbach of the pair 40,2 is

``````> cronbach(cbind(40,2))
\$sample.size
[1] 1

\$number.of.items
[1] 2

\$alpha
[1] NA
``````

i.e. alpha not-applicable

great support.! i will try this out and see if i can get that working
cheers

I tried it and i got the following message:
Warning in install.packages :
package ‘library(magrittr)’ is not available (for R version 3.6.0)
should i 3.6 out and install 3.5 again?

Hi, I had to amend the coding slightly as your package magritter does not exist in version 3.6.

it looks like this

df <- tibble::tribble(
~seq, ~date, ~sales,
1, "3/01/2017", 40,
2, "4/01/2017", 2,
3, "5/01/2017", 2,
4, "6/01/2017", 2,
5, "7/01/2017", 30,
6, "8/01/2017", 2,
7, "1/02/2017", 9,
8, "2/02/2017", 5,
9, "3/02/2017", 65,
10, "4/02/2017", 3,
11, "5/02/2017", 65
)
library(tidyverse)
library(plyr)
library(psy)

df %<>% mutate(sales = lag(sales))
df2 <- rowwise(df) %>% mutate(z = cronbach(cbind(sales,lagsales))\$alpha) %>% ungroup

i get the following errorcode:

df %<>% mutate(sales = lag(sales))
Error in df %<>% mutate(sales = lag(sales)) :
could not find function "%<>%"
df2 <- rowwise(df) %>% mutate(z = cronbach(cbind(sales,lagsales))\$alpha) %>% ungroup

any ideas how to amend the code?

cheers

`magrittr` does exist for R 3.6 (hard to know what the real problem is, because of lack of information) but this part of the code

Is equivalent to this, without using `magrittr` (kind of, since part of it is already imported by `dplyr`)

``````df <- df %>% mutate(lagsales = lag(sales))
``````

great work ! Andrés Castro Socolich , you are a great support!
It provided the Alpha calc.

Hello Andre
Quick question

I noticed the Alpha is calculated for the whole line .
How do i calculate a separate rolling Alpha for every two items,so item 2 and 3 , then item 3 and 4 .etc.etc.?

#code:

df <- tibble::tribble(
~seq, ~date, ~sales,
1, "3/01/2017", 40,
2, "4/01/2017", 2,
3, "5/01/2017", 2,
4, "6/01/2017", 2,
5, "7/01/2017", 30,
6, "8/01/2017", 2,
7, "1/02/2017", 9,
8, "2/02/2017", 5,
9, "3/02/2017", 65,
10, "4/02/2017", 3,
11, "5/02/2017", 65
)
library(tidyverse)
library(magrittr)
library(psy)

A tibble: 11 x 5
seq date sales lagsales z

• 1 1 3/01/2017 40 NA -1.23
2 2 4/01/2017 2 40 -1.23
3 3 5/01/2017 2 2 -1.23
4 4 6/01/2017 2 2 -1.23
5 5 7/01/2017 30 2 -1.23
6 6 8/01/2017 2 30 -1.23
7 7 1/02/2017 9 2 -1.23
8 8 2/02/2017 5 9 -1.23
9 9 3/02/2017 65 5 -1.23
10 10 4/02/2017 3 65 -1.23
11 11 5/02/2017 65 3 -1.23

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