Advertisement
adapap

BT 466 Final Code

Apr 9th, 2020
731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 1.63 KB | None | 0 0
  1. setwd("C:/Users/debom/Documents/_Stevens/S2020/BT 466/R")
  2. # Library imports
  3. library(car)
  4. library(data.table)
  5. library(stringr)
  6. library(multilevel)
  7. library(mediation)
  8.  
  9. # Read input data
  10. data <- read.csv("CSM Posts.csv", stringsAsFactors=FALSE, header=TRUE)
  11.  
  12. # Utiliity functions
  13. replace_all <- function(df, pattern, replacement) {
  14.   char <- vapply(df, function(x) is.factor(x) || is.character(x), logical(1))
  15.   df[char] <- lapply(df[char], str_replace_all, pattern, replacement)  
  16.   df
  17. }
  18.  
  19. # Data cleaning / preparation
  20. data <- replace_all(data, ",", ".")
  21. username <- data$Username
  22. wc <- data$WC
  23. data <- data[,12:ncol(data)]
  24. data <- as.data.frame(sapply(data, as.numeric))
  25. data <- cbind(username, data)
  26. data <- cbind(wc, data)
  27.  
  28. # Adding custom data points for use in regression analysis
  29. # Number of posts made (by user)
  30. num_posts <- as.numeric(ave(username, username, FUN = length))
  31. data <- cbind(num_posts, data)
  32. cleaned <- data.frame(
  33.   affiliation=scale(data$affiliation),
  34.   username=data$username,
  35.   num_posts=scale(data$num_posts),
  36.   family=scale(data$family),
  37.   friend=scale(data$friend),
  38.   wc=scale(data$wc),
  39.   social=scale(data$social),
  40.   drives=scale(data$drives),
  41.   achieve=scale(data$achieve),
  42.   power=scale(data$power),
  43.   percept=scale(data$percept),
  44.   reward=scale(data$reward),
  45.   home=scale(data$home)
  46. )
  47. # Moderator
  48. social_drives <- cleaned$social * cleaned$drives
  49. moderator <-lm(affiliation ~ social + drives + social_drives, data=cleaned)
  50. summary(moderator)
  51. # Indirect effect is 0.184
  52. # Social is moderated by drives, low p-value -> significant
  53.  
  54. # Mediator
  55. # Sobel arguments: (pred, med, out)
  56. mediator <- sobel(data$family, data$social, data$affiliation)
  57. mediator
  58. p_value_2_sided <- 2*pnorm(-abs(mediator$z.value))
  59. p_value_2_sided
  60. # Indirect Effect: 0.219
  61. # 2-Sided P-Value: 0 (significant?)
  62. # Family is mediated by social
  63. # Achievement is mediated by reward
  64.  
  65. # Multiple Regression
  66. model <- lm(affiliation ~ . - username, data=cleaned)
  67. options(scipen=999)
  68. summary(model)
  69. # R^2: 58.92%
  70.  
  71.  
  72. # Moderated-Mediation
  73. # This doesn't work at all
  74.  
  75. # Moderator: drives
  76. low.drives <- mean(data$drives)-sd(data$drives)
  77. high.drives <- mean(data$drives)+sd(data$drives)
  78.  
  79. # Mediator: social
  80. Mod.Med.Model.1 <- lm(affiliation ~ drives * family, data=data)
  81. Mod.Med.Model.2 <- lm(affiliation ~ drives * family + social, data=data)
  82.  
  83. Mod.Med.LowDrives <- mediate(Mod.Med.Model.1, Mod.Med.Model.2, covariates=list(drives=low.drives), boot = TRUE, boot.ci.type = "bca", sims = 10, treat="affiliation", mediator="social")
  84. summary(Mod.Med.LowDrives)
  85.  
  86. Mod.Med.HighDrives <- mediate(Mod.Med.Model.1, Mod.Med.Model.2, covariates=list(drives=high.drives), boot = TRUE, boot.ci.type = "bca", sims = 10, treat="affiliation", mediator="social")
  87. summary(Mod.Med.HighDrives)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement