---
title: "350.week8.composites"
author: "William Revelle"
date: "`r Sys.Date()`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(width=100)
```

# Forming composites to enhance reliability and validity


1. We know from our prior discussions of reliability and test theory that forming composites of items will enhance prediction and reliability.
2. Known since Spearman (1904) the the power of aggregation is that error averages out and that signal averages in.
3. How to form aggregates?
• Item sums
• item averages
• These are obviously just transforms of each, unless we have missing data
4. Multiple ways of doing this:
• Scripts (one off)
• Write your own function
• Explore the psych functions

Look at https://personality-project.org/courses/350/350.week8.composites.pdf  for the slides

### first make psych active and check the date
```{r}
library(psych)
library(psychTools)
packageDate("psych")
```

### Now we can do our first example

```{r}
data <- ability
Cscores <- rowSums(data)   #total scores
Mscores <- rowMeans(data)  #mean scores
cor2(Cscores,Mscores)
length(Cscores)
#do it again, but with na.rm=TRUE
Cscores <- rowSums(data, na.rm=TRUE)   #total scores
Mscores <- rowMeans(data,na.rm=TRUE)  #mean scores
cor2(Cscores,Mscores)
plot(Mscores ~ Cscores,main="means versus sum scores")
```


## Using alpha or scoreFast 

```{r}
  al <- alpha(data)
cor2(al$scores,Mscores)
sf <- scoreFast(list(colnames(data)),data)
cor2(sf,Mscores)
keys <- list(colnames(data))
sf <- scoreFast(keys,data)

```

### Show some keys

```{r}
ability.keys
bfi.keys
spi.keys
```

Keys can be used to show the content if we have a dictionary

```{r}
lookupFromKeys(bfi.keys,bfi.dictionary[2:3])
lookupFromKeys(spi.keys, spi.dictionary[,c(2,5)])
```

#  Scale construction using `bestScales`

`bestScales` is a simple "Machine Learning" learning algorithm that does supervised learning by finding the items that best correlate with a criterion based upon K-folds or n.iter bootstraps

## no iterations

```{r}
bs <-   bestScales(x=bfi[1:25], criteria = bfi[26:28],
             dictionary=bfi.dictionary[2:3])
            
bs
```

## 100 bootstraps

```{r}

bs1 <-   bestScales(x=bfi[1:25], criteria = bfi[26:28],
             dictionary=bfi.dictionary[2:3], n.iter=100)
bs1
```
