# tidy coding
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ ggplot2 3.4.2 ✔ purrr 1.0.1
## ✔ tibble 3.2.1 ✔ dplyr 1.1.2
## ✔ tidyr 1.3.0 ✔ stringr 1.5.0
## ✔ readr 2.0.1 ✔ forcats 0.5.1
## Warning: package 'ggplot2' was built under R version 4.1.2
## Warning: package 'tibble' was built under R version 4.1.2
## Warning: package 'tidyr' was built under R version 4.1.2
## Warning: package 'purrr' was built under R version 4.1.2
## Warning: package 'dplyr' was built under R version 4.1.2
## Warning: package 'stringr' was built under R version 4.1.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
library(dplyr)
library(broom)
network_knowledge_task_data <- read_csv("network_knowledge_task_data.csv")
## Rows: 156600 Columns: 14
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): semester
## dbl (13): sub_id, from_id, to_id, friend_guess, micro_friend, meso_comm, sub...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
This generates 2 warning messages associated with subjects for whom the model encountered convergence issues; this data is then removed from analysis.
network_knowledge_estimates_raw <- network_knowledge_task_data %>%
# Group by individual subjects
group_by(semester, across(starts_with("sub_"))) %>%
nest() %>%
# Extract beta coefficients from logistic regression that predicts subjects'
# trial-by-trial responses based on true micro- and meso-level social ties
mutate(
knowledge = map(
.x = data,
.f = ~glm(friend_guess ~ micro_friend + meso_comm,
family = "binomial", data = .x) %>%
tidy() %>%
select(term, estimate, std.error, p.value) %>%
filter(term != "(Intercept)")
)
) %>%
unnest(knowledge) %>%
ungroup() %>%
select(-data)
## Warning: There were 2 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `knowledge = map(...)`.
## ℹ In group 36: `semester = "fall"`, `sub_id = 5098`, `sub_influence_fall =
## 0.129404`, `sub_influence_spring = NA`, `sub_friend_count_fall = 7`,
## `sub_friend_count_spring = NA`, `sub_extroversion = 24`, `sub_influence_delta
## = NA`, `sub_friend_count_delta = NA`.
## Caused by warning:
## ! glm.fit: fitted probabilities numerically 0 or 1 occurred
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
network_knowledge_estimates <- network_knowledge_estimates_raw %>%
# Remove model convergence issues
group_by(semester, term) %>%
mutate(z_sd = (std.error - mean(std.error))/sd(std.error)) %>%
ungroup() %>%
filter(abs(z_sd) < 3) %>%
# Remove outliers
group_by(semester, term) %>%
mutate(z_estimate = (estimate - mean(estimate))/sd(estimate)) %>%
ungroup() %>%
filter(abs(z_estimate) < 3) %>%
select(!c(std.error, p.value, z_sd, z_estimate))
# Delta influence predicted by micro & meso knowledge:
fall_knowledge_delta_influence <- network_knowledge_estimates %>%
filter(semester == "fall") %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_influence_delta ~ micro_friend + meso_comm,
data = .)
summary(fall_knowledge_delta_influence)
##
## Call:
## lm(formula = sub_influence_delta ~ micro_friend + meso_comm,
## data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.66009 -0.16413 0.00410 0.09741 0.73150
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.19944 0.05707 -3.494 0.000757 ***
## micro_friend 0.06490 0.03139 2.067 0.041734 *
## meso_comm 0.07858 0.02394 3.282 0.001496 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2529 on 85 degrees of freedom
## (10 observations deleted due to missingness)
## Multiple R-squared: 0.1456, Adjusted R-squared: 0.1255
## F-statistic: 7.244 on 2 and 85 DF, p-value: 0.001244
confint(fall_knowledge_delta_influence, "micro_friend", level = 0.95)
## 2.5 % 97.5 %
## micro_friend 0.002485514 0.1273192
confint(fall_knowledge_delta_influence, "meso_comm", level = 0.95)
## 2.5 % 97.5 %
## meso_comm 0.03098046 0.1261893
# ...and controlling for extroversion and Fall friend count:
fall_knowledge_delta_influence_ctrl <- network_knowledge_estimates %>%
filter(semester == "fall") %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_influence_delta ~ micro_friend + meso_comm +
sub_friend_count_fall + sub_extroversion,
data = .)
summary(fall_knowledge_delta_influence_ctrl)
##
## Call:
## lm(formula = sub_influence_delta ~ micro_friend + meso_comm +
## sub_friend_count_fall + sub_extroversion, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.53309 -0.13817 -0.03193 0.09543 0.72867
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.222428 0.120692 -1.843 0.06895 .
## micro_friend 0.052573 0.032151 1.635 0.10585
## meso_comm 0.078804 0.024071 3.274 0.00155 **
## sub_friend_count_fall -0.007103 0.004930 -1.441 0.15348
## sub_extroversion 0.004167 0.004178 0.997 0.32151
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2524 on 82 degrees of freedom
## (11 observations deleted due to missingness)
## Multiple R-squared: 0.1716, Adjusted R-squared: 0.1312
## F-statistic: 4.246 on 4 and 82 DF, p-value: 0.003574
confint(fall_knowledge_delta_influence_ctrl, "micro_friend", level = 0.95)
## 2.5 % 97.5 %
## micro_friend -0.01138621 0.1165315
confint(fall_knowledge_delta_influence_ctrl, "meso_comm", level = 0.95)
## 2.5 % 97.5 %
## meso_comm 0.03091955 0.1266883
fall_knowledge_delta_friend_count <- network_knowledge_estimates %>%
filter(semester == "fall") %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_friend_count_delta ~ micro_friend + meso_comm,
data = .)
summary(fall_knowledge_delta_friend_count)
##
## Call:
## lm(formula = sub_friend_count_delta ~ micro_friend + meso_comm,
## data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -12.5470 -1.6767 0.5476 2.1829 8.5099
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.35550 0.83282 -4.029 0.000121 ***
## micro_friend 0.63190 0.45808 1.379 0.171374
## meso_comm 0.07587 0.34937 0.217 0.828600
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.691 on 85 degrees of freedom
## (10 observations deleted due to missingness)
## Multiple R-squared: 0.02218, Adjusted R-squared: -0.0008306
## F-statistic: 0.9639 on 2 and 85 DF, p-value: 0.3855
confint(fall_knowledge_delta_friend_count, "micro_friend", level = 0.95)
## 2.5 % 97.5 %
## micro_friend -0.2788854 1.542679
confint(fall_knowledge_delta_friend_count, "meso_comm", level = 0.95)
## 2.5 % 97.5 %
## meso_comm -0.6187695 0.770511
fall_knowledge_fall_influence <- network_knowledge_estimates %>%
filter(semester == "fall") %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_influence_fall ~ micro_friend + meso_comm,
data = .)
summary(fall_knowledge_fall_influence)
##
## Call:
## lm(formula = sub_influence_fall ~ micro_friend + meso_comm, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.29533 -0.15279 -0.04588 0.09283 0.60490
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.310490 0.045797 6.780 1.18e-09 ***
## micro_friend -0.044144 0.023982 -1.841 0.0689 .
## meso_comm -0.008456 0.018754 -0.451 0.6531
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2053 on 91 degrees of freedom
## (4 observations deleted due to missingness)
## Multiple R-squared: 0.03663, Adjusted R-squared: 0.01546
## F-statistic: 1.73 on 2 and 91 DF, p-value: 0.1831
confint(fall_knowledge_fall_influence, "micro_friend", level = 0.95)
## 2.5 % 97.5 %
## micro_friend -0.09178164 0.003493175
confint(fall_knowledge_fall_influence, "meso_comm", level = 0.95)
## 2.5 % 97.5 %
## meso_comm -0.04570945 0.02879719
fall_knowledge_fall_friend_count <- network_knowledge_estimates %>%
filter(semester == "fall") %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_friend_count_fall ~ micro_friend + meso_comm,
data = .)
summary(fall_knowledge_fall_friend_count)
##
## Call:
## lm(formula = sub_friend_count_fall ~ micro_friend + meso_comm,
## data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -10.3722 -4.6285 -0.8453 3.4838 14.4492
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 11.0591 1.3257 8.342 7.46e-13 ***
## micro_friend -1.1311 0.6942 -1.629 0.107
## meso_comm 0.3926 0.5429 0.723 0.471
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.944 on 91 degrees of freedom
## (4 observations deleted due to missingness)
## Multiple R-squared: 0.03651, Adjusted R-squared: 0.01533
## F-statistic: 1.724 on 2 and 91 DF, p-value: 0.1841
confint(fall_knowledge_fall_friend_count, "micro_friend", level = 0.95)
## 2.5 % 97.5 %
## micro_friend -2.510075 0.2478533
confint(fall_knowledge_fall_friend_count, "meso_comm", level = 0.95)
## 2.5 % 97.5 %
## meso_comm -0.685786 1.470964
spring_knowledge_spring_influence <- network_knowledge_estimates %>%
filter(semester == "spring") %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_influence_spring ~ micro_friend + meso_comm,
data = .)
summary(spring_knowledge_spring_influence)
##
## Call:
## lm(formula = sub_influence_spring ~ micro_friend + meso_comm,
## data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.32820 -0.16017 -0.08212 0.08750 0.71102
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -0.01167 0.07076 -0.165 0.86953
## micro_friend 0.13827 0.04854 2.849 0.00574 **
## meso_comm 0.05613 0.03675 1.527 0.13116
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2477 on 71 degrees of freedom
## (6 observations deleted due to missingness)
## Multiple R-squared: 0.1552, Adjusted R-squared: 0.1314
## F-statistic: 6.52 on 2 and 71 DF, p-value: 0.002515
confint(spring_knowledge_spring_influence, "micro_friend", level = 0.95)
## 2.5 % 97.5 %
## micro_friend 0.04148433 0.2350513
confint(spring_knowledge_spring_influence, "meso_comm", level = 0.95)
## 2.5 % 97.5 %
## meso_comm -0.01715451 0.1294064
spring_knowledge_spring_friend_count <- network_knowledge_estimates %>%
filter(semester == "spring") %>%
pivot_wider(names_from = term,
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_friend_count_spring ~ micro_friend + meso_comm,
data = .)
summary(spring_knowledge_spring_friend_count)
##
## Call:
## lm(formula = sub_friend_count_spring ~ micro_friend + meso_comm,
## data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -6.238 -3.647 -1.786 2.750 15.388
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 5.1117 1.4748 3.466 0.0009 ***
## micro_friend 1.3063 1.0116 1.291 0.2008
## meso_comm 0.7958 0.7660 1.039 0.3024
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5.162 on 71 degrees of freedom
## (6 observations deleted due to missingness)
## Multiple R-squared: 0.04725, Adjusted R-squared: 0.02042
## F-statistic: 1.761 on 2 and 71 DF, p-value: 0.1793
confint(spring_knowledge_spring_friend_count, "micro_friend", level = 0.95)
## 2.5 % 97.5 %
## micro_friend -0.7108565 3.32336
confint(spring_knowledge_spring_friend_count, "meso_comm", level = 0.95)
## 2.5 % 97.5 %
## meso_comm -0.7314831 2.323059
# Spring influence predicted by Fall meso * Spring micro knowledge:
meso_fall_micro_spring_knowledge_spring_influence <-
network_knowledge_estimates %>%
pivot_wider(names_from = c(semester, term),
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_influence_spring ~ fall_meso_comm*spring_micro_friend,
data = .)
summary(meso_fall_micro_spring_knowledge_spring_influence)
##
## Call:
## lm(formula = sub_influence_spring ~ fall_meso_comm * spring_micro_friend,
## data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.38490 -0.15090 -0.08552 0.10612 0.73117
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.201792 0.096732 2.086 0.0406 *
## fall_meso_comm -0.127292 0.065563 -1.942 0.0562 .
## spring_micro_friend -0.008592 0.075228 -0.114 0.9094
## fall_meso_comm:spring_micro_friend 0.102138 0.040425 2.527 0.0138 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.242 on 70 degrees of freedom
## (25 observations deleted due to missingness)
## Multiple R-squared: 0.203, Adjusted R-squared: 0.1688
## F-statistic: 5.943 on 3 and 70 DF, p-value: 0.001138
confint(meso_fall_micro_spring_knowledge_spring_influence,
"fall_meso_comm:spring_micro_friend", level = 0.95)
## 2.5 % 97.5 %
## fall_meso_comm:spring_micro_friend 0.02151186 0.182764
# Spring influence predicted by all combinations of Fall/Spring
# meso/micro knowledge:
fall_spring_knowledge_spring_influence <-
network_knowledge_estimates %>%
pivot_wider(names_from = c(semester, term),
values_from = estimate) %>%
as.data.frame() %>%
lm(sub_influence_spring ~ fall_meso_comm*spring_micro_friend +
fall_meso_comm*spring_meso_comm +
fall_micro_friend*spring_micro_friend +
fall_micro_friend*spring_meso_comm,
data = .)
summary(fall_spring_knowledge_spring_influence)
##
## Call:
## lm(formula = sub_influence_spring ~ fall_meso_comm * spring_micro_friend +
## fall_meso_comm * spring_meso_comm + fall_micro_friend * spring_micro_friend +
## fall_micro_friend * spring_meso_comm, data = .)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.46608 -0.13698 -0.05948 0.08357 0.69514
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 0.29035 0.13589 2.137 0.0365 *
## fall_meso_comm -0.15228 0.06861 -2.220 0.0301 *
## spring_micro_friend -0.18009 0.12172 -1.480 0.1440
## spring_meso_comm 0.02738 0.09810 0.279 0.7811
## fall_micro_friend -0.04000 0.08617 -0.464 0.6441
## fall_meso_comm:spring_micro_friend 0.10443 0.04634 2.254 0.0277 *
## fall_meso_comm:spring_meso_comm 0.02402 0.03796 0.633 0.5292
## spring_micro_friend:fall_micro_friend 0.11335 0.06705 1.691 0.0959 .
## spring_meso_comm:fall_micro_friend -0.03482 0.05223 -0.667 0.5074
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2384 on 63 degrees of freedom
## (27 observations deleted due to missingness)
## Multiple R-squared: 0.2965, Adjusted R-squared: 0.2072
## F-statistic: 3.32 on 8 and 63 DF, p-value: 0.003152
confint(fall_spring_knowledge_spring_influence,
"fall_meso_comm:spring_micro_friend", level = 0.95)
## 2.5 % 97.5 %
## fall_meso_comm:spring_micro_friend 0.01183317 0.1970201
sessionInfo()
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-apple-darwin17.0 (64-bit)
## Running under: macOS Big Sur 10.16
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.1/Resources/lib/libRlapack.dylib
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] broom_0.7.9 forcats_0.5.1 stringr_1.5.0 dplyr_1.1.2
## [5] purrr_1.0.1 readr_2.0.1 tidyr_1.3.0 tibble_3.2.1
## [9] ggplot2_3.4.2 tidyverse_1.3.1
##
## loaded via a namespace (and not attached):
## [1] tidyselect_1.2.0 xfun_0.26 bslib_0.7.0 haven_2.4.3
## [5] colorspace_2.0-2 vctrs_0.6.3 generics_0.1.0 htmltools_0.5.8.1
## [9] yaml_2.2.1 utf8_1.2.2 rlang_1.1.1 jquerylib_0.1.4
## [13] pillar_1.9.0 withr_2.5.0 glue_1.6.2 DBI_1.1.1
## [17] bit64_4.0.5 dbplyr_2.1.1 modelr_0.1.8 readxl_1.3.1
## [21] lifecycle_1.0.3 munsell_0.5.0 gtable_0.3.0 cellranger_1.1.0
## [25] rvest_1.0.1 evaluate_0.14 knitr_1.34 tzdb_0.1.2
## [29] fastmap_1.2.0 parallel_4.1.1 fansi_0.5.0 Rcpp_1.0.7
## [33] backports_1.2.1 scales_1.2.1 cachem_1.1.0 vroom_1.5.5
## [37] jsonlite_1.7.2 bit_4.0.4 fs_1.5.0 hms_1.1.0
## [41] digest_0.6.27 stringi_1.7.4 grid_4.1.1 cli_3.6.1
## [45] tools_4.1.1 magrittr_2.0.1 sass_0.4.9 crayon_1.4.1
## [49] pkgconfig_2.0.3 ellipsis_0.3.2 xml2_1.3.2 reprex_2.0.1
## [53] lubridate_1.7.10 assertthat_0.2.1 rmarkdown_2.11 httr_1.4.2
## [57] rstudioapi_0.13 R6_2.5.1 compiler_4.1.1