Libraries/themes
library(ggplot2)
library(tidyverse)
library(gridExtra)
library(finalfit)
library(kableExtra)
library(xtable)
jrothsch_theme <- theme_bw() +
theme(text = element_text(size = 10, face = "bold", color = "deepskyblue4"),panel.grid = element_blank(),axis.text = element_text(size = 10, color = "gray13"), axis.title = element_text(size = 10, color = "red"), legend.text = element_text(colour="Black", size=10), legend.title = element_text(colour="Black", size=7), plot.subtitle = element_text(size=14, face="italic", color="black"))
rm(list = ls())
setwd("~/Downloads")
library(foreign)
sex_data <- read.spss("sex_data.sav", use.value.label=TRUE, to.data.frame=TRUE)
Making table function
format_endtable <- function(endtable){
endtable <- endtable %>%
mutate(likewant = Want_Mar + Like_Mar ) %>%
arrange(desc(likewant)) %>%
select(-likewant)
endtable <- round_df(endtable, digits=3)
i = 0
for (val in endtable$name){
i = i + 1
if(identical(val, "Control")){
j = i
}
}
endtable <- endtable %>%
mutate(Want_Mod_T = cell_spec(Want_Mod_T, "html", background = ifelse(Want_Mod_T < -2.5, "#FF6347",
ifelse(Want_Mod_T > 2.5,"lightgreen", "white"))),
Like_Mod_T = cell_spec(Like_Mod_T, "html", background = ifelse(Like_Mod_T < -2.5, "#FF6347",
ifelse(Like_Mod_T > 2.5,"lightgreen", "white"))),
Freq_Mod_T = cell_spec(Freq_Mod_T, "html", background = ifelse(Freq_Mod_T < -2.5, "#FF6347",
ifelse(Freq_Mod_T > 2.5,"lightgreen", "white"))),
Want_Mar = cell_spec(Want_Mar, "html", background = ifelse(Want_Mar > -1, "gold", "white")),
Like_Mar = cell_spec(Like_Mar, "html", background = ifelse(Like_Mar > -1, "gold", "white")),
Freq_Mar = cell_spec(Freq_Mar, "html", background = ifelse(Freq_Mar > -.1, "gold", "white")),
name = cell_spec(name, "html", bold = T, color = "black", background = "white")
)
colnames(endtable) <- c("Moderator", "Marriage Est, Want", "Marriage Est, Like", "Marriage Est, Freq", "Marriage T , Want", "Marriage T , Like", "Marriage T , Freq", "Moderator Est, Want", "Moderator Est, Like, ", "Moderator Est, Freq", "Moderator T, Want", "Moderator T, Like", "Moderator T, Freq")
kable(endtable, row.names= T, align=c("l","l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l", "l"), booktabs=TRUE, escape = F) %>%
kable_styling(font_size=8) %>%
row_spec(j, bold = T, color = "black", background = "azure")
}
Round function
round_df <- function(df, digits) {
nums <- vapply(df, is.numeric, FUN.VALUE = logical(1))
df[,nums] <- round(df[,nums], digits = digits)
(df)
}
Make table function
make_table <- function(mods, names){
i = 0
for (mod in mods){
i = i +1
mod = as.numeric(mod)
txwant <- lm(sex_data$want ~ sex_data$married + sex_data$age + sex_data$Years_PrimaryPartner + sex_data$MALE + mod)
txwant <- xtable(txwant)
txlike <- sex_data %>% lm(totlike ~ married + age + Years_PrimaryPartner + MALE + mod, data = .)
txlike <- xtable(txlike)
txfreq <- sex_data %>% lm(freq_num ~ married + age + Years_PrimaryPartner + MALE + mod, data = .)
txfreq <- xtable(txfreq)
Want_Mar = append(Want_Mar, c(txwant$Estimate[2]))
Like_Mar = append(Like_Mar, c(txlike$Estimate[2]))
Freq_Mar = append(Freq_Mar, c(txfreq$Estimate[2]))
Want_Mod = append(Want_Mod, c(txwant$Estimate[6]))
Like_Mod = append(Like_Mod, c(txlike$Estimate[6]))
Freq_Mod = append(Freq_Mod, c(txfreq$Estimate[6]))
Want_Mar_T = append(Want_Mar_T, c(txwant$`t value`[2]))
Like_Mar_T = append(Like_Mar_T, c(txlike$`t value`[2]))
Freq_Mar_T = append(Freq_Mar_T, c(txfreq$`t value`[2]))
Want_Mod_T = append(Want_Mod_T, c(txwant$`t value`[6]))
Like_Mod_T = append(Like_Mod_T, c(txlike$`t value`[6]))
Freq_Mod_T = append(Freq_Mod_T, c(txfreq$`t value`[6]))
name = append(name, names[i])
}
res_tab = data.frame(name, Want_Mar, Like_Mar, Freq_Mar, Want_Mar_T, Like_Mar_T, Freq_Mar_T, Want_Mod, Like_Mod, Freq_Mod, Want_Mod_T, Like_Mod_T, Freq_Mod_T)
return(res_tab)
}
Removing obserations without our key variables
sex_data <- sex_data %>%
filter(!is.na(status) & !is.na(age) & !is.na(Years_PrimaryPartner) & !is.na(MALE) & !is.na(sexfreq)) %>%
mutate(freq_num = 7 - as.numeric(sexfreq))
Base table
txwant <- sex_data %>% lm(want ~ married + age + Years_PrimaryPartner + MALE, data = .)
txwant <- xtable(txwant)
txlike <- sex_data %>% lm(totlike ~ married + age + Years_PrimaryPartner + MALE + as.numeric(EnjoySex), data = .)
txlike <- xtable(txlike)
txfreq <- sex_data %>% lm(freq_num ~ married + age + Years_PrimaryPartner + MALE, data = .)
txfreq <- xtable(txfreq)
Reset vectors
Want_Mar = c(txwant$Estimate[2])
Like_Mar= c(txlike$Estimate[2])
Freq_Mar = c(txfreq$Estimate[2])
Want_Mar_T = c(txwant$`t value` [2])
Like_Mar_T = c(txlike$`t value`[2])
Freq_Mar_T = c(txfreq$`t value`[2])
Want_Mod = c(0)
Like_Mod= c(0)
Freq_Mod = c(0)
Want_Mod_T = c(0)
Like_Mod_T = c(0)
Freq_Mod_T = c(0)
name = c("Control")
mods = sex_data %>%
select(seximpor, initiate, i_do, they_do, frustration, satis2, satis9, PartnerLucky, satis22, satis23, satis21, emotion, workpower, PhysicalAttractive_PrimaryPartner)
names <- c('Importance', 'Initiating Ratio', 'Unreciprocated Wanting', 'Partner Unreciprocated Wanting', 'Frustated With Unreciprocated Desire', "Adds a Lot", "Attractive Partner", 'Partner Sexually Pleased, Partner Is Lucky', 'Lucky To Be With Partner', 'Attraction To Partner', 'Partner Sexually Attracted', 'Emotional Proximity', 'Partner Work Power', 'Partner Attractiveness')
endtable <- make_table(mods, names)
format_endtable((endtable))
| Moderator | Marriage Est, Want | Marriage Est, Like | Marriage Est, Freq | Marriage T , Want | Marriage T , Like | Marriage T , Freq | Moderator Est, Want | Moderator Est, Like, | Moderator Est, Freq | Moderator T, Want | Moderator T, Like | Moderator T, Freq | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Attractive Partner | -1.353 | -1.113 | -0.029 | -5.192 | -4.562 | -0.652 | 3.183 | 5.977 | 0.484 | 35.821 | 71.518 | 31.826 |
| 2 | Partner Sexually Attracted | -1.596 | -1.354 | -0.065 | -5.818 | -5.104 | -1.403 | 2.715 | 5.727 | 0.431 | 27.626 | 60.256 | 26.108 |
| 3 | Adds a Lot | -1.513 | -1.7 | -0.059 | -6.314 | -6.715 | -1.388 | 3.826 | 5.596 | 0.545 | 48.127 | 66.22 | 38.39 |
| 4 | Attraction To Partner | -1.603 | -1.661 | -0.099 | -6.516 | -7.038 | -2.138 | 3.885 | 6.303 | 0.400 | 44.707 | 75.625 | 24.396 |
| 5 | Partner Attractiveness | -1.904 | -2.22 | -0.174 | -5.090 | -5.356 | -2.520 | 2.129 | 3.062 | 0.195 | 24.557 | 31.873 | 12.155 |
| 6 | Control | -2.215 | -1.971 | -0.149 | -7.621 | -4.310 | -3.040 | 0.000 | 0.000 | 0.000 | 0 | 0 | 0 |
| 7 | Importance | -1.922 | -2.628 | -0.136 | -7.481 | -7.440 | -2.850 | 2.105 | 1.930 | 0.308 | 43.102 | 28.26 | 34.064 |
| 8 | Partner Work Power | -2.135 | -2.503 | -0.151 | -7.227 | -7.099 | -3.050 | 0.402 | 0.817 | 0.014 | 6.788 | 11.548 | 1.389 |
| 9 | Partner Sexually Pleased, Partner Is Lucky | -2.156 | -2.529 | -0.201 | -5.395 | -6.039 | -2.908 | 2.390 | 4.832 | 0.310 | 15.865 | 30.621 | 11.897 |
| 10 | Partner Unreciprocated Wanting | -2.125 | -2.565 | -0.147 | -7.398 | -7.255 | -3.003 | -1.143 | -1.257 | 0.059 | -10.425 | -9.3 | 3.176 |
| 11 | Unreciprocated Wanting | -2.365 | -2.456 | -0.13 | -8.236 | -6.935 | -2.660 | 1.168 | -1.185 | -0.090 | 11.175 | -9.122 | -5.065 |
| 12 | Initiating Ratio | -2.216 | -2.624 | -0.141 | -7.672 | -7.355 | -2.888 | -0.967 | 0.346 | 0.092 | -7.503 | 2.157 | 4.198 |
| 13 | Lucky To Be With Partner | -2.21 | -2.648 | -0.162 | -8.124 | -9.590 | -3.401 | 2.813 | 5.442 | 0.317 | 28.557 | 54.422 | 18.429 |
| 14 | Frustated With Unreciprocated Desire | -2.32 | -2.58 | -0.15 | -8.164 | -7.246 | -3.071 | 0.980 | -0.394 | 0.032 | 15.208 | -4.849 | 2.925 |
| 15 | Emotional Proximity | -2.289 | -2.802 | -0.163 | -8.310 | -9.581 | -3.413 | 2.093 | 3.905 | 0.237 | 26.581 | 46.725 | 17.336 |
Want_Mar = c(txwant$Estimate[2])
Like_Mar= c(txlike$Estimate[2])
Freq_Mar = c(txfreq$Estimate[2])
Want_Mar_T = c(txwant$`t value` [2])
Like_Mar_T = c(txlike$`t value`[2])
Freq_Mar_T = c(txfreq$`t value`[2])
Want_Mod = c(0)
Like_Mod= c(0)
Freq_Mod = c(0)
Want_Mod_T = c(0)
Like_Mod_T = c(0)
Freq_Mod_T = c(0)
mods = sex_data %>%
select(MasturbationFreq, FakeOrgasm, lube, drugs, howmany, Masturbation)
names <- c('Masturbation Freq', 'Orgasm Faking', 'Lube Use', 'Enhancing Drug Use', 'Num Previous Partners', 'Masturbation Binary')
endtable = make_table(mods, names)
format_endtable(endtable)
| Moderator | Marriage Est, Want | Marriage Est, Like | Marriage Est, Freq | Marriage T , Want | Marriage T , Like | Marriage T , Freq | Moderator Est, Want | Moderator Est, Like, | Moderator Est, Freq | Moderator T, Want | Moderator T, Like | Moderator T, Freq | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Masturbation Freq | -1.433 | -2.406 | -0.141 | -2.647 | -3.696 | -1.557 | -1.036 | 0.294 | 0.001 | -5.24 | 1.239 | 0.023 |
| 2 | Control | -2.215 | -1.971 | -0.149 | -7.621 | -4.310 | -3.040 | 0.000 | 0.000 | 0.000 | 0 | 0 | 0 |
| 3 | Lube Use | -2.162 | -2.598 | -0.146 | -7.312 | -7.279 | -2.995 | 0.507 | 0.488 | 0.161 | 5.334 | 4.249 | 10.265 |
| 4 | Enhancing Drug Use | -2.175 | -2.619 | -0.152 | -7.338 | -7.324 | -3.076 | 0.324 | 0.124 | 0.073 | 2.464 | 0.783 | 3.327 |
| 5 | Masturbation Binary | -2.241 | -2.926 | -0.237 | -5.383 | -5.792 | -3.329 | 1.987 | -0.661 | -0.079 | 5.199 | -1.411 | -1.216 |
| 6 | Orgasm Faking | -2.478 | -3.013 | -0.223 | -5.969 | -6.111 | -3.129 | -1.158 | -2.722 | 0.051 | -4.898 | -9.653 | 1.253 |
| 7 | Num Previous Partners | -1.666 | -4.07 | -0.175 | -2.156 | -4.550 | -1.405 | 0.000 | 0.000 | 0.000 | -0.274 | -0.747 | -0.852 |
Want_Mar = c(txwant$Estimate[2])
Like_Mar= c(txlike$Estimate[2])
Freq_Mar = c(txfreq$Estimate[2])
Want_Mar_T = c(txwant$`t value` [2])
Like_Mar_T = c(txlike$`t value`[2])
Freq_Mar_T = c(txfreq$`t value`[2])
Want_Mod = c(0)
Like_Mod= c(0)
Freq_Mod = c(0)
Want_Mod_T = c(0)
Like_Mod_T = c(0)
Freq_Mod_T = c(0)
mods = sex_data %>%
select(postmeno, SexualDesire, EnjoySex, MasturbationOrgasm, orgasm, o_satis, Desire6m, Enjoy6m, satis4, comfort)
names <- c('Post Menopause', 'Sexual Desire -', 'Enjoy Sex -', 'Enjoyment Of Masturbation', 'Orgasm Freq', 'Orgasm Satisfaction', 'Desire In First 6 Months', 'Enjoyment First 6 Months', 'Turned On Before Physical Contact', 'Comfort Discussing')
endtable <- make_table(mods, names)
format_endtable(endtable)
| Moderator | Marriage Est, Want | Marriage Est, Like | Marriage Est, Freq | Marriage T , Want | Marriage T , Like | Marriage T , Freq | Moderator Est, Want | Moderator Est, Like, | Moderator Est, Freq | Moderator T, Want | Moderator T, Like | Moderator T, Freq | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Turned On Before Physical Contact | -0.869 | -1.053 | -0.034 | -3.883 | -3.964 | -0.731 | 4.459 | 5.513 | 0.402 | 57.938 | 60.258 | 25.273 |
| 2 | Enjoy Sex - | -1.609 | -1.971 | -0.139 | -4.377 | -4.310 | -2.035 | -4.326 | -4.572 | -0.441 | -25.595 | -21.574 | -14.093 |
| 3 | Enjoyment Of Masturbation | -1.539 | -2.338 | -0.121 | -2.829 | -3.590 | -1.339 | 0.640 | 0.333 | 0.082 | 5.081 | 2.204 | 3.957 |
| 4 | Sexual Desire - | -1.804 | -2.311 | -0.156 | -4.727 | -4.791 | -2.267 | -4.196 | -3.638 | -0.455 | -21.024 | -14.294 | -12.655 |
| 5 | Orgasm Satisfaction | -1.908 | -2.208 | -0.114 | -7.213 | -7.167 | -2.431 | 1.774 | 2.623 | 0.207 | 31.418 | 38.979 | 20.654 |
| 6 | Control | -2.215 | -1.971 | -0.149 | -7.621 | -4.310 | -3.040 | 0.000 | 0.000 | 0.000 | 0 | 0 | 0 |
| 7 | Comfort Discussing | -1.882 | -2.391 | -0.127 | -6.351 | -6.610 | -2.536 | 1.010 | 0.899 | 0.101 | 14.212 | 10.363 | 8.391 |
| 8 | Orgasm Freq | -2.148 | -2.534 | -0.143 | -7.996 | -8.037 | -2.995 | 2.542 | 3.740 | 0.261 | 28.495 | 35.25 | 16.499 |
| 9 | Enjoyment First 6 Months | -2.586 | -3.079 | -0.24 | -6.437 | -6.488 | -3.409 | 1.359 | 1.972 | 0.125 | 13.404 | 16.307 | 7.017 |
| 10 | Desire In First 6 Months | -2.725 | -3.245 | -0.248 | -6.742 | -6.688 | -3.493 | 1.308 | 1.641 | 0.097 | 12.531 | 13.037 | 5.281 |
| 11 | Post Menopause | -3.099 | -4.288 | -0.134 | -3.728 | -3.796 | -0.880 | -0.857 | -0.980 | 0.089 | -1.022 | -0.843 | 0.579 |
Want_Mar = c(txwant$Estimate[2])
Like_Mar= c(txlike$Estimate[2])
Freq_Mar = c(txfreq$Estimate[2])
Want_Mar_T = c(txwant$`t value` [2])
Like_Mar_T = c(txlike$`t value`[2])
Freq_Mar_T = c(txfreq$`t value`[2])
Want_Mod = c(0)
Like_Mod= c(0)
Freq_Mod = c(0)
Want_Mod_T = c(0)
Like_Mod_T = c(0)
Freq_Mod_T = c(0)
mods = sex_data %>%
select(satis5, satis14, satis16, satis18, satis24, satis25, satis26, intellectualstim)
names <- c('Monotony', 'Fantasize About Other Partner', 'Sex Too Fast', 'Distracted During Sex', 'Tries New Things', 'Mysterious Partner', 'Discovering New Things About Partners', 'Intellectual Stimulating Partner')
endtable <- make_table(mods, names)
format_endtable(endtable)
| Moderator | Marriage Est, Want | Marriage Est, Like | Marriage Est, Freq | Marriage T , Want | Marriage T , Like | Marriage T , Freq | Moderator Est, Want | Moderator Est, Like, | Moderator Est, Freq | Moderator T, Want | Moderator T, Like | Moderator T, Freq | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Tries New Things | -0.83 | -0.31 | 0.006 | -3.119 | -1.124 | 0.132 | 3.018 | 5.107 | 0.368 | 34.329 | 55.97 | 23.65 |
| 2 | Discovering New Things About Partners | -0.931 | -0.668 | -0.042 | -3.400 | -2.166 | -0.863 | 2.662 | 4.114 | 0.249 | 29.623 | 40.675 | 15.588 |
| 3 | Mysterious Partner | -1.264 | -1.293 | -0.072 | -4.474 | -3.890 | -1.465 | 2.198 | 3.139 | 0.209 | 23.049 | 27.999 | 12.632 |
| 4 | Sex Too Fast | -1.938 | -1.768 | -0.129 | -6.562 | -5.247 | -2.613 | -0.909 | -2.981 | -0.109 | -8.482 | -24.379 | -6.092 |
| 5 | Control | -2.215 | -1.971 | -0.149 | -7.621 | -4.310 | -3.040 | 0.000 | 0.000 | 0.000 | 0 | 0 | 0 |
| 6 | Distracted During Sex | -2.087 | -2.413 | -0.155 | -7.244 | -7.267 | -3.148 | -1.760 | -3.352 | -0.088 | -15.862 | -26.21 | -4.628 |
| 7 | Monotony | -2.112 | -2.553 | -0.145 | -7.218 | -7.425 | -2.952 | -0.646 | -1.933 | -0.097 | -7.284 | -18.514 | -6.545 |
| 8 | Intellectual Stimulating Partner | -2.17 | -2.582 | -0.15 | -7.964 | -8.874 | -3.128 | 2.081 | 3.683 | 0.209 | 28.592 | 47.392 | 16.322 |
| 9 | Fantasize About Other Partner | -2.235 | -2.721 | -0.163 | -7.615 | -7.981 | -3.302 | -1.238 | -3.318 | -0.075 | -8.915 | -20.563 | -3.206 |
Want_Mar = c(txwant$Estimate[2])
Like_Mar= c(txlike$Estimate[2])
Freq_Mar = c(txfreq$Estimate[2])
Want_Mar_T = c(txwant$`t value` [2])
Like_Mar_T = c(txlike$`t value`[2])
Freq_Mar_T = c(txfreq$`t value`[2])
Want_Mod = c(0)
Like_Mod= c(0)
Freq_Mod = c(0)
Want_Mod_T = c(0)
Like_Mod_T = c(0)
Freq_Mod_T = c(0)
sex_data <-sex_data %>% mutate(dif = age - partner_age)
mods = sex_data %>%
select(econ_secure, econ_hurt, econ_anxious, educ, HouseholdIncome, partner_age, dif, children)
names <- c('Economic Security', 'Hurt By Recession', 'Economic Anxiety', 'Education Level', 'Income Level', 'Partner Age', 'Age Difference', 'Children In House')
# Didn't get to look at religion
endtable <- make_table(mods, names)
format_endtable(endtable)
| Moderator | Marriage Est, Want | Marriage Est, Like | Marriage Est, Freq | Marriage T , Want | Marriage T , Like | Marriage T , Freq | Moderator Est, Want | Moderator Est, Like, | Moderator Est, Freq | Moderator T, Want | Moderator T, Like | Moderator T, Freq | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | Children In House | -1.615 | -0.669 | -0.025 | -0.776 | -0.237 | -0.078 | -0.208 | -0.630 | 0.003 | -0.376 | -0.839 | 0.04 |
| 2 | Control | -2.215 | -1.971 | -0.149 | -7.621 | -4.310 | -3.040 | 0.000 | 0.000 | 0.000 | 0 | 0 | 0 |
| 3 | Age Difference | -2.135 | -2.505 | -0.127 | -7.324 | -7.004 | -2.587 | 0.060 | 0.093 | 0.016 | 3.143 | 3.938 | 5.139 |
| 4 | Partner Age | -2.135 | -2.505 | -0.127 | -7.324 | -7.004 | -2.587 | -0.060 | -0.093 | -0.016 | -3.143 | -3.938 | -5.139 |
| 5 | Economic Anxiety | -2.192 | -2.574 | -0.152 | -7.279 | -7.113 | -3.039 | -0.067 | -0.542 | -0.043 | -0.921 | -6.18 | -3.523 |
| 6 | Education Level | -2.203 | -2.615 | -0.155 | -7.309 | -7.180 | -3.085 | 0.035 | -0.153 | 0.016 | 0.398 | -1.455 | 1.081 |
| 7 | Hurt By Recession | -2.198 | -2.624 | -0.156 | -7.300 | -7.242 | -3.114 | -0.055 | -0.428 | -0.031 | -0.795 | -5.141 | -2.71 |
| 8 | Economic Security | -2.248 | -2.734 | -0.166 | -7.488 | -7.625 | -3.321 | 0.361 | 0.873 | 0.075 | 5.415 | 10.967 | 6.783 |
| 9 | Income Level | -2.264 | -2.77 | -0.246 | -5.201 | -5.319 | -3.354 | -0.067 | -0.016 | 0.048 | -0.634 | -0.125 | 2.656 |