---
title: "SF-12"
output: pdf_document
---
### The present output can be created by using a MarkDown Script in RStudio.

----

### Please download and install both R-software and RStudio at https://www.r-project.org/ and https://www.rstudio.com/.

----

### Install and call the necesary libraries
```{r Install and call libraries, echo=TRUE, message=FALSE, warning=FALSE}
# Get the following packages from CRAN if you need
# AUTOMATING PACKAGES NEEDED FOR ANALYSES -
haspackage = require("xlsx")
if (haspackage==FALSE){
  install.packages("xlsx")
}
library(xlsx)

haspackage = require("Hmisc")
if (haspackage==FALSE){
  install.packages("Hmisc")
}
library(lavaan)

haspackage = require("Hmisc")
if (haspackage==FALSE){
  install.packages("Hmisc")
}
library(lavaan)

#Advanced Matrix Operations 

```

### Read the actual working folder:
```{r Set the working folder, message=FALSE, warning=FALSE, include=FALSE}
getwd()
```

### Set the workig folder where the data are. 
#### In the present case, a folder called SF-12 was created
```{r Set working folder, include=FALSE}
setwd("C:/../..")

```

```{r Clear the working memory, message=FALSE, warning=FALSE, include=FALSE}
rm(list=ls())
```

#### Read the external dataframe
```{r Load the external dataframe, include=FALSE}

### Uncomment and use this line to get data from SPSS file
# sf.12.spss <- spss.get("sf.12.scoring.sav", use.value.labels=TRUE)
# OR
### Uncomment and use this line to get data from Excel (xlsx) file
# sf.12<-read.xlsx("sf.12.scoring.xlsx") 
# OR
### Uncomment and use this line to get data from CSV file
sf.12<-read.csv("sf.12.scoring.csv",sep=",") 
```

```{r Values recoding, include=FALSE}
##################
##################
#### Step 1#######
##################
##################

# Clean and reverse the data and mofify the oulier values for each item 

## RP2 RP3 RE2 RE3
sf.12$RP2[sf.12$RP2<1|sf.12$RP2>2]<-NA
sf.12$RP3[sf.12$RP3<1|sf.12$RP2>2]<-NA
sf.12$RE2[sf.12$RE2<1|sf.12$RE2>2]<-NA
sf.12$RE3[sf.12$RE3<1|sf.12$RE3>2]<-NA

## PF02 PF04
sf.12$PF02[sf.12$PF02<1|sf.12$PF02>3]<-NA
sf.12$PF04[sf.12$PF02<1|sf.12$PF04>3]<-NA

## GH1 BP2 SF2
sf.12$GH1[sf.12$GH1<1|sf.12$GH1>5]<-NA
sf.12$BP2[sf.12$BP2<1|sf.12$BP2>5]<-NA
sf.12$SF2[sf.12$SF2<1|sf.12$SF2>5]<-NA

## VT2 MH3 MH4
sf.12$VT2[sf.12$VT2<1|sf.12$VT2>6]<-NA
sf.12$MH3[sf.12$MH3<1|sf.12$MH3>6]<-NA
sf.12$MH4[sf.12$MH4<1|sf.12$MH4>6]<-NA


```

```{r Compute new variables by subtracting 6 from the respective values, include=FALSE}

# COMPUTE RBP2=6-BP2.
# EXECUTE.
sf.12$RBP2<-(6-sf.12$BP2)
# COMPUTE RGH1=6-GH1.
# EXECUTE.
sf.12$RGH1<-(6-sf.12$GH1)
# COMPUTE RVT2=7-VT2.
# EXECUTE.
sf.12$RVT2<-(7-sf.12$VT2)
# COMPUTE RMH3=7-MH3.
# EXECUTE.
sf.12$RMH3<-(7-sf.12$MH3)
```


```{r Recode, include=FALSE}
##################
##################
#### Step 2#######
##################
##################

# PF02
sf.12$PF02_1<-ifelse(is.na(sf.12$PF02), NA, ifelse(sf.12$PF02== 1, 1, 0))
sf.12$PF02_2<-ifelse(is.na(sf.12$PF02), NA, ifelse(sf.12$PF02== 2, 1, 0))

# PF04
sf.12$PF04_1<-ifelse(is.na(sf.12$PF04), NA, ifelse(sf.12$PF04== 1, 1, 0))
sf.12$PF04_2<-ifelse(is.na(sf.12$PF04), NA, ifelse(sf.12$PF04== 2, 1, 0))

# RP2
sf.12$RP2_1<-ifelse(is.na(sf.12$RP2), NA, ifelse(sf.12$RP2== 1, 1, 0))

# RP3
sf.12$RP3_1<-ifelse(is.na(sf.12$RP3), NA, ifelse(sf.12$RP3== 1, 1, 0))

# (R)BP2
sf.12$BP2_1<-ifelse(is.na(sf.12$RBP2), NA, ifelse(sf.12$RBP2== 1, 1, 0))
sf.12$BP2_2<-ifelse(is.na(sf.12$RBP2), NA, ifelse(sf.12$RBP2== 2, 1, 0))
sf.12$BP2_3<-ifelse(is.na(sf.12$RBP2), NA, ifelse(sf.12$RBP2== 3, 1, 0))
sf.12$BP2_4<-ifelse(is.na(sf.12$RBP2), NA, ifelse(sf.12$RBP2== 4, 1, 0))

##(R)GH1
sf.12$GH1_1<-ifelse(is.na(sf.12$RGH1), NA, ifelse(sf.12$RGH1== 1, 1, 0))
sf.12$GH1_2<-ifelse(is.na(sf.12$RGH1), NA, ifelse(sf.12$RGH1== 2, 1, 0))
sf.12$GH1_3<-ifelse(is.na(sf.12$RGH1), NA, ifelse(sf.12$RGH1== 3, 1, 0))
sf.12$GH1_4<-ifelse(is.na(sf.12$RGH1), NA, ifelse(sf.12$RGH1== 4, 1, 0))

##(R)RVT2
sf.12$VT2_1<-ifelse(is.na(sf.12$RVT2), NA, ifelse(sf.12$RVT2== 1, 1, 0))
sf.12$VT2_2<-ifelse(is.na(sf.12$RVT2), NA, ifelse(sf.12$RVT2== 2, 1, 0))
sf.12$VT2_3<-ifelse(is.na(sf.12$RVT2), NA, ifelse(sf.12$RVT2== 3, 1, 0))
sf.12$VT2_4<-ifelse(is.na(sf.12$RVT2), NA, ifelse(sf.12$RVT2== 4, 1, 0))
sf.12$VT2_5<-ifelse(is.na(sf.12$RVT2), NA, ifelse(sf.12$RVT2== 5, 1, 0))
                    
## SF2

sf.12$SF2_1<-ifelse(is.na(sf.12$SF2), NA, ifelse(sf.12$SF2== 1, 1, 0))
sf.12$SF2_2<-ifelse(is.na(sf.12$SF2), NA, ifelse(sf.12$SF2== 2, 1, 0))
sf.12$SF2_3<-ifelse(is.na(sf.12$SF2), NA, ifelse(sf.12$SF2== 3, 1, 0))
sf.12$SF2_4<-ifelse(is.na(sf.12$SF2), NA, ifelse(sf.12$SF2== 4, 1, 0))

##RE2
sf.12$RE2_1<-ifelse(is.na(sf.12$RE2), NA, ifelse(sf.12$RE2== 1, 1, 0))

##RE3
sf.12$RE3_1<-ifelse(is.na(sf.12$RE3), NA, ifelse(sf.12$RE3== 1, 1, 0))

## MH3
sf.12$MH3_1<-ifelse(is.na(sf.12$RMH3), NA, ifelse(sf.12$RMH3== 1, 1, 0))
sf.12$MH3_2<-ifelse(is.na(sf.12$RMH3), NA, ifelse(sf.12$RMH3== 2, 1, 0))
sf.12$MH3_3<-ifelse(is.na(sf.12$RMH3), NA, ifelse(sf.12$RMH3== 3, 1, 0))
sf.12$MH3_4<-ifelse(is.na(sf.12$RMH3), NA, ifelse(sf.12$RMH3== 4, 1, 0))
sf.12$MH3_5<-ifelse(is.na(sf.12$RMH3), NA, ifelse(sf.12$RMH3== 5, 1, 0))

## MH4

sf.12$MH4_1<-ifelse(is.na(sf.12$MH4), NA, ifelse(sf.12$MH4== 1, 1, 0))
sf.12$MH4_2<-ifelse(is.na(sf.12$MH4), NA, ifelse(sf.12$MH4== 2, 1, 0))
sf.12$MH4_3<-ifelse(is.na(sf.12$MH4), NA, ifelse(sf.12$MH4== 3, 1, 0))
sf.12$MH4_4<-ifelse(is.na(sf.12$MH4), NA, ifelse(sf.12$MH4== 4, 1, 0))
sf.12$MH4_5<-ifelse(is.na(sf.12$MH4), NA, ifelse(sf.12$MH4== 5, 1, 0))

```


```{r Calculate the totale vlues, include=FALSE}
##################
##################
#### Step 3#######
##################
##################

# Aggregate and weight the indicators by physical and mental given parameters

sf.12$RAWPCS12<-
(-7.23216*sf.12$PF02_1
+-3.45555*sf.12$PF02_2
+-6.24397*sf.12$PF04_1
+-2.73557*sf.12$PF04_2
+-4.61617*sf.12$RP2_1
+-5.51747*sf.12$RP3_1
+-11.25544*sf.12$BP2_1
+-8.38063*sf.12$BP2_2
+-6.50522*sf.12$BP2_3
+-3.80130*sf.12$BP2_4
+-8.37399*sf.12$GH1_1
+-5.56461*sf.12$GH1_2
+-3.02396*sf.12$GH1_3
+-1.31872*sf.12$GH1_4
+-2.44706*sf.12$VT2_1
+-2.02168*sf.12$VT2_2
+-1.6185*sf.12$VT2_3
+-1.14387*sf.12$VT2_4
+-0.42251*sf.12$VT2_5
+-0.33682*sf.12$SF2_1
+-0.94342*sf.12$SF2_2
+-0.18043*sf.12$SF2_3 
+0.11038*sf.12$SF2_4 
+3.04365*sf.12$RE2_1 
+2.32091*sf.12$RE3_1 
+3.46638*sf.12$MH3_1 
+2.90426*sf.12$MH3_2 
+2.37241*sf.12$MH3_3 
+1.36689*sf.12$MH3_4
+0.66514*sf.12$MH3_5 
+4.61446*sf.12$MH4_1
+3.41593*sf.12$MH4_2 
+2.34247*sf.12$MH4_3 
+1.28044*sf.12$MH4_4 
+0.41188*sf.12$MH4_5)


sf.12$RAWMCS12 = (3.93115*sf.12$PF02_1 
+1.8684*sf.12$PF02_2 
+2.68282*sf.12$PF04_1 
+1.43103*sf.12$PF04_2 
+1.4406*sf.12$RP2_1 
+1.66968*sf.12$RP3_1 
+1.48619*sf.12$BP2_1 
+1.76691*sf.12$BP2_2 
+1.49384*sf.12$BP2_3 
+0.90384*sf.12$BP2_4 
+-1.71175*sf.12$GH1_1 
+-0.16891*sf.12$GH1_2 
+0.03482*sf.12$GH1_3 
+-0.06064*sf.12$GH1_4 
+-6.02409*sf.12$VT2_1 
+-4.88962*sf.12$VT2_2 
+-3.29805*sf.12$VT2_3 
+-1.65178*sf.12$VT2_4 
+-0.92057*sf.12$VT2_5 
+-6.29724*sf.12$SF2_1 
+-8.26066*sf.12$SF2_2 
+-5.63286*sf.12$SF2_3 
+-3.13896*sf.12$SF2_4 
+-6.82672*sf.12$RE2_1 
+-5.69921*sf.12$RE3_1 
+-10.19085*sf.12$MH3_1 
+-7.92717*sf.12$MH3_2 
+-6.31121*sf.12$MH3_3 
+-4.09842*sf.12$MH3_4 
+-1.94949*sf.12$MH3_5 
+-16.15395*sf.12$MH4_1 
+-10.77911*sf.12$MH4_2 
+-8.09914*sf.12$MH4_3 
+-4.59055*sf.12$MH4_4 
+-1.95934*sf.12$MH4_5)

```


```{r Compute the total indexes, echo=FALSE}
##################
##################
#### Step 4#######
##################
##################

# Standardize the resuling scales 

sf.12$PCS12 = sf.12$RAWPCS12 + 56.57706
sf.12$MCS12 = sf.12$RAWMCS12 + 60.75781

```

### Run the script and check for the resulting databases: A csv file named  sf.12.result.csv, and an Excel(x) file named sf.12.result.xlxs will appear within the directory you indicated at the beginning. 
```{r output, include=FALSE}

write.table(sf.12[c(1,53:56)],"sf.12.results.csv",sep="\t") 

write.xlsx(sf.12[c(1,53:56)],"sf.12.results.xlsx",sheetName = "sf.12.r.results", row.names = FALSE)

```

#### Here you can have a quick look at your data by a series of descriptive statistics
```{r Descriptive statistics, echo=FALSE}
descriptive.1<-describe(sf.12[c(53:56)],fast=T)
print(descriptive.1)
```

