---
title: "Code for Yes Fit and Muscular but No Asians, Blacks, or Latinos"
output: html_notebook
---

```{r}
library(sem)
library(lavaan)

##Scoring measures
##White Men who have Sex with Men sample
##dataset - white.csv

##Drive for Muscularity Subscales
white$dmattitude=white$dm1+white$dm6+white$dm7+white$dm9+white$dm11+white$dm12+white$dm13+white$dm14+white$dm15
white$dmbehavior= white$dm2+white$dm3+white$dm4+white$dm5+white$dm8

##Body-Image Related Psychological Flexibility
white$aaqbi<-white$aaqbi1+white$aaqbi2+white$aaqbi3+white$aaqbi4+white$aaqbi5+white$aaqbi6+white$aaqbi7+white$aaqbi8+white$aaqbi9+white$aaqbi10+white$aaqbi11+white$aaqbi12

##Male Body Attitude Scale subscales
white$mbasbodyfat=white$mbas2+white$mbas4R+white$mbas8+white$mbas10+white$mbas13+white$mbas14+white$mbas18R+white$mbas20+white$mbas21+white$mbas22+white$mbas23+white$mbas24

white$mbasmuscularity=white$mbas1+white$mbas3+white$mbas5+white$mbas6+white$mbas7+white$mbas9+white$mbas10+white$mbas11+white$mbas15+white$mbas16+white$mbas17R+white$mbas18R+white$mbas22+white$mbas23


```


```{r}
##Scoring measures
##Men of Color who have Sex with Men sample
##dataset - mcsm_multiracial_multiracial.csv

##Drive for Muscularity Subscales
mcsm_multiracial$dmattitude=mcsm_multiracial$dm1+mcsm_multiracial$dm6+mcsm_multiracial$dm7+mcsm_multiracial$dm9+mcsm_multiracial$dm11+mcsm_multiracial$dm12+mcsm_multiracial$dm13+mcsm_multiracial$dm14+mcsm_multiracial$dm15
mcsm_multiracial$dmbehavior= mcsm_multiracial$dm2+mcsm_multiracial$dm3+mcsm_multiracial$dm4+mcsm_multiracial$dm5+mcsm_multiracial$dm8

##Body-Image Related Psychological Flexibility
mcsm_multiracial$aaqbi<-mcsm_multiracial$aaqbi1+mcsm_multiracial$aaqbi2+mcsm_multiracial$aaqbi3+mcsm_multiracial$aaqbi4+mcsm_multiracial$aaqbi5+mcsm_multiracial$aaqbi6+mcsm_multiracial$aaqbi7+mcsm_multiracial$aaqbi8+mcsm_multiracial$aaqbi9+mcsm_multiracial$aaqbi10+mcsm_multiracial$aaqbi11+mcsm_multiracial$aaqbi12

##Male Body Attitude Scale subscales
mcsm_multiracial$mbasbodyfat=mcsm_multiracial$mbas2+mcsm_multiracial$mbas4R+mcsm_multiracial$mbas8+mcsm_multiracial$mbas10+mcsm_multiracial$mbas13+mcsm_multiracial$mbas14+mcsm_multiracial$mbas18R+mcsm_multiracial$mbas20+mcsm_multiracial$mbas21+mcsm_multiracial$mbas22+mcsm_multiracial$mbas23+mcsm_multiracial$mbas24

mcsm_multiracial$mbasmuscularity=mcsm_multiracial$mbas1+mcsm_multiracial$mbas3+mcsm_multiracial$mbas5+mcsm_multiracial$mbas6+mcsm_multiracial$mbas7+mcsm_multiracial$mbas9+mcsm_multiracial$mbas10+mcsm_multiracial$mbas11+mcsm_multiracial$mbas15+mcsm_multiracial$mbas16+mcsm_multiracial$mbas17R+mcsm_multiracial$mbas18R+mcsm_multiracial$mbas22+mcsm_multiracial$mbas23

##Experienced Sexual Racism Scale scoring
mcsm_multiracial$exsr=mcsm_multiracial$exsr1+mcsm_multiracial$exsr2+mcsm_multiracial$exsr3+mcsm_multiracial$exsr4+mcsm_multiracial$exsr5+mcsm_multiracial$exsr6

```

```{r}
##Cronbach alphas for scales

##White MSM sample
##dataset - white.csv

##Drive for Muscularity Behavior subscale
dmbehaviorw<-data.frame(white$dm2,white$dm3,white$dm4,white$dm5,white$dm8)

alpha(dmbehaviorw)

##Drive for Muscularity Attitudes towards Muscularity subscale
dmattitudew<-data.frame(white$dm1,white$dm6,white$dm7,white$dm9,white$dm11,white$dm12,white$dm13,white$dm14,white$dm15)

alpha(dmattitudew)

##Body Image Psychological Flexibility Scale
aaqbiwhite<-data.frame(white$aaqbi1,white$aaqbi2,white$aaqbi3,white$aaqbi4,white$aaqbi5,white$aaqbi6,white$aaqbi7,white$aaqbi8,white$aaqbi9,white$aaqbi10,white$aaqbi11,white$aaqbi12)

alpha(aaqbiwhite)

##Male Body Attitude Body Fat susbcale
mbasfatw<-data.frame(white$mbas2,white$mbas4R,white$mbas8,white$mbas10,white$mbas13,white$mbas14,white$mbas18R,white$mbas20,white$mbas21,white$mbas22,white$mbas23,white$mbas24)

alpha(mbasfatw)

##Male Body Attitude Muscularity subscale
mbasmuscw<-data.frame(white$mbas1,white$mbas3,white$mbas5,white$mbas6,white$mbas7,white$mbas9,white$mbas10,white$mbas11,white$mbas15,white$mbas16,white$mbas17R,white$mbas18R,white$mbas22,white$mbas23)

alpha(mbasmuscw)

```

```{r}

##Cronbach alphas for scales

##mcsm_multiracial sample
##dataset - mcsm_multiracial.csv

##Drive for Muscularity Behavior subscale
dmbehaviorc<-data.frame(mcsm_multiracial$dm2,mcsm_multiracial$dm3,mcsm_multiracial$dm4,mcsm_multiracial$dm5,mcsm_multiracial$dm8)

alpha(dmbehaviorc)

##Drive for Muscularity Attitudes towards Muscularity subscale
dmattitudec<-data.frame(mcsm_multiracial$dm1,mcsm_multiracial$dm6,mcsm_multiracial$dm7,mcsm_multiracial$dm9,mcsm_multiracial$dm11,mcsm_multiracial$dm12,mcsm_multiracial$dm13,mcsm_multiracial$dm14,mcsm_multiracial$dm15)

alpha(dmattitudec)

##Body Image Psychological Flexibility Scale
aaqbimcsm_multiracial<-data.frame(mcsm_multiracial$aaqbi1,mcsm_multiracial$aaqbi2,mcsm_multiracial$aaqbi3,mcsm_multiracial$aaqbi4,mcsm_multiracial$aaqbi5,mcsm_multiracial$aaqbi6,mcsm_multiracial$aaqbi7,mcsm_multiracial$aaqbi8,mcsm_multiracial$aaqbi9,mcsm_multiracial$aaqbi10,mcsm_multiracial$aaqbi11,mcsm_multiracial$aaqbi12)

alpha(aaqbimcsm_multiracial)

##Male Body Attitude Body Fat susbcale
mbasfatc<-data.frame(mcsm_multiracial$mbas2,mcsm_multiracial$mbas4R,mcsm_multiracial$mbas8,mcsm_multiracial$mbas10,mcsm_multiracial$mbas13,mcsm_multiracial$mbas14,mcsm_multiracial$mbas18R,mcsm_multiracial$mbas20,mcsm_multiracial$mbas21,mcsm_multiracial$mbas22,mcsm_multiracial$mbas23,mcsm_multiracial$mbas24)

alpha(mbasfatc)

##Male Body Attitude Muscularity subscale
mbasmuscc<-data.frame(mcsm_multiracial$mbas1,mcsm_multiracial$mbas3,mcsm_multiracial$mbas5,mcsm_multiracial$mbas6,mcsm_multiracial$mbas7,mcsm_multiracial$mbas9,mcsm_multiracial$mbas10,mcsm_multiracial$mbas11,mcsm_multiracial$mbas15,mcsm_multiracial$mbas16,mcsm_multiracial$mbas17R,mcsm_multiracial$mbas18R,mcsm_multiracial$mbas22,mcsm_multiracial$mbas23)

alpha(mbasmuscc)

##Experienced Sexual Racism scale

exsrc<-data.frame(mcsm_multiracial$exsr1, mcsm_multiracial$exsr2, mcsm_multiracial$exsr3, mcsm_multiracial$exsr4, mcsm_multiracial$exsr5, mcsm_multiracial$exsr6)

alpha(exsrc)
```

```{r}
##Means and SDs for scales

##White MSM sample

mean(white$aaqbi)
sd(white$aaqbi)

mean(white$dmbehavior)
sd(white$dmbehavior)

mean(white$dmattitude)
sd(white$dmattitude)

mean(white$mbasmuscularity)
sd(white$mbasmuscularity)

mean(white$mbasbodyfat)
sd(white$mbasbodyfat)

##mcsm_multiracial sample

mean(mcsm_multiracial$aaqbi)
sd(mcsm_multiracial$aaqbi)

mean(mcsm_multiracial$dmbehavior)
sd(mcsm_multiracial$dmbehavior)

mean(mcsm_multiracial$dmattitude)
sd(mcsm_multiracial$dmattitude)

mean(mcsm_multiracial$mbasmuscularity)
sd(mcsm_multiracial$mbasmuscularity)

mean(mcsm_multiracial$mbasbodyfat)
sd(mcsm_multiracial$mbasbodyfat)

mean(mcsm_multiracial$exsr)
sd(mcsm_multiracial$exsr)

```

```{r}
##correlations between main variables of interest

##For mcsm_multiracial sample

correlationmcsm_multiracial<-data.frame(mcsm_multiracial$aaqbi, mcsm_multiracial$exsr, mcsm_multiracial$dmbehavior, mcsm_multiracial$dmattitude, mcsm_multiracial$mbasmuscularity, mcsm_multiracial$mbasbodyfat)
corrmcsm_multiracial<-corr.test(correlationmcsm_multiracial)
print(corrmcsm_multiracial, short=FALSE)

##for White MSM sample

correlationwhite<-data.frame(white$aaqbi, white$exsr, white$dmbehavior, white$dmattitude, white$mbasmuscularity, white$mbasbodyfat)
corrwhite<-corr.test(correlationwhite)
print(corrwhite, short=FALSE)

```

```{r}
##MANOVA and ANOVA to test difference on main variables by ethnicity

manova1 <- manova(cbind(aaqbi, mbasbodyfat, mbasmuscularity, dmbehavior, dmattitude) ~ ethnicity1, data = combined)
summary(manova1)
summary.aov(manova1)
etasq(manova1)

aovDMb<-aov(dmbehavior~ethnicity1, data=combined)
summary(aovDMb)
etasq(aovDMb)

aovAAQBI<-aov(aaqbi~ethnicity1, data=combined)
summary(aovAAQBI)
etasq(aovAAQBI)

```


```{r}
##moderation analysis

mod<-lm(dmbehavior~exsr*aaqbi, data=mcsm_multiracial)
summary(mod)
confint(mod, 'exsr', level=0.95)
confint(mod, 'aaqbi', level=.95)
confint(mod, 'exsr:aaqbi', level=.95)
```



```{r}
##Johnson-Neyman analysis
jn<-jn(mod,dv='dmbehavior', iv='exsr',mod='aaqbi',  alpha=.05)
jn

##plotting Johnson-Neyman
jnmod<-johnson_neyman(mod, pred = exsr, modx = aaqbi,alpha = .05)

##Pick-a-point analysis
pick1<-pickapoint(mod,dv='dmbehavior', iv='exsr',mod='aaqbi',  alpha=.05)
pick1
```






