library(foreign)
d1 <- read.spss(file = "D:/STATISTIKA/SEMESTER 5/AMS/multiple_moderators.sav", to.data.frame = TRUE)
## re-encoding from CP1252
str(d1)
## 'data.frame': 1774 obs. of 4 variables:
## $ age : Factor w/ 9 levels "9","10","11",..: 4 2 4 3 2 2 3 2 2 2 ...
## $ rum : num NA 1 2 1.25 2 2 2 1 2 3.75 ...
## $ stress : num 1 1 3.75 2.25 1.75 1.5 1.75 2 1.75 3.25 ...
## $ selfharm: num 1 1 1 1 1 1 1.5 1 1 1 ...
## - attr(*, "variable.labels")= Named chr [1:4] "Age" "Rumination" "Stress" "Self-harm"
## ..- attr(*, "names")= chr [1:4] "age" "rum" "stress" "selfharm"
## - attr(*, "codepage")= int 1252
d1$agec <- ifelse(d1$age == "13" | d1$age == "14" | d1$age == "15" | d1$age == "16" | d1$age == "17", 1, 0)
head(d1)
## age rum stress selfharm agec
## 1 12 NA 1.00 1 0
## 2 10 1.00 1.00 1 0
## 3 12 2.00 3.75 1 0
## 4 11 1.25 2.25 1 0
## 5 10 2.00 1.75 1 0
## 6 10 2.00 1.50 1 0
library(psych)
## Warning: package 'psych' was built under R version 4.5.2
des <- describe(d1)
print(des, digits = 4)
## vars n mean sd median trimmed mad min max range skew
## age* 1 1774 4.1246 1.7328 4.00 4.0394 2.9652 1 8 7 0.1662
## rum 2 1757 2.3775 0.8364 2.25 2.3316 0.7413 1 5 4 0.5037
## stress 3 1732 2.2823 0.9186 2.00 2.1890 0.7413 1 5 4 0.8542
## selfharm 4 1750 1.1849 0.5444 1.00 1.0407 0.0000 1 5 4 4.0808
## agec 5 1774 0.3963 0.4893 0.00 0.3704 0.0000 0 1 1 0.4237
## kurtosis se
## age* -1.2608 0.0411
## rum -0.0927 0.0200
## stress 0.3153 0.0221
## selfharm 19.4117 0.0130
## agec -1.8215 0.0116
model1 <- lm(selfharm ~ rum + stress + agec, data = d1)
library(lm.beta)
## Warning: package 'lm.beta' was built under R version 4.5.2
summary(lm.beta(model1))
##
## Call:
## lm(formula = selfharm ~ rum + stress + agec, data = d1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.8393 -0.2231 -0.0855 0.0489 3.6273
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) 0.54026 NA 0.04124 13.100 < 2e-16 ***
## rum 0.12078 0.18861 0.01590 7.598 4.96e-14 ***
## stress 0.13903 0.23771 0.01454 9.564 < 2e-16 ***
## agec 0.09365 0.08548 0.02471 3.789 0.000156 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4994 on 1697 degrees of freedom
## (73 observations deleted due to missingness)
## Multiple R-squared: 0.139, Adjusted R-squared: 0.1375
## F-statistic: 91.35 on 3 and 1697 DF, p-value: < 2.2e-16
with(d1,cor.test(rum,selfharm))
##
## Pearson's product-moment correlation
##
## data: rum and selfharm
## t = 12.559, df = 1735, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.2449768 0.3312195
## sample estimates:
## cor
## 0.2886837
model2 <- lm(selfharm ~ rum + stress + agec +rum*stress + agec*stress, data = d1)
summary(lm.beta(model2))
##
## Call:
## lm(formula = selfharm ~ rum + stress + agec + rum * stress +
## agec * stress, data = d1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.2461 -0.1829 -0.0704 0.0037 3.8133
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) 1.01572 NA 0.09700 10.472 < 2e-16 ***
## rum -0.02434 -0.03801 0.03825 -0.636 0.524674
## stress -0.06262 -0.10706 0.04060 -1.542 0.123188
## agec -0.25594 -0.23361 0.06684 -3.829 0.000133 ***
## rum:stress 0.05716 0.40638 0.01430 3.996 6.72e-05 ***
## stress:agec 0.15074 0.35837 0.02697 5.589 2.66e-08 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4927 on 1695 degrees of freedom
## (73 observations deleted due to missingness)
## Multiple R-squared: 0.1628, Adjusted R-squared: 0.1604
## F-statistic: 65.93 on 5 and 1695 DF, p-value: < 2.2e-16
model3 <- lm(selfharm ~ rum + stress + agec +rum*stress + agec*stress +agec*stress*rum, data = d1)
summary(lm.beta(model3))
##
## Call:
## lm(formula = selfharm ~ rum + stress + agec + rum * stress +
## agec * stress + agec * stress * rum, data = d1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.8189 -0.1763 -0.0797 0.0046 3.7165
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) 0.72583 NA 0.11408 6.362 2.55e-10 ***
## rum 0.07036 0.10987 0.04608 1.527 0.12695
## stress 0.13848 0.23676 0.04891 2.831 0.00469 **
## agec 0.50637 0.46219 0.19344 2.618 0.00893 **
## rum:stress -0.01147 -0.08152 0.01764 -0.650 0.51577
## stress:agec -0.36531 -0.86850 0.08080 -4.521 6.58e-06 ***
## rum:agec -0.23774 -0.56498 0.07961 -2.986 0.00287 **
## rum:stress:agec 0.17088 1.20519 0.02928 5.836 6.39e-09 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4834 on 1693 degrees of freedom
## (73 observations deleted due to missingness)
## Multiple R-squared: 0.1952, Adjusted R-squared: 0.1919
## F-statistic: 58.65 on 7 and 1693 DF, p-value: < 2.2e-16
young <- subset(d1,agec == "0")
young.mod1 <- lm(selfharm ~ stress +rum, data = young)
summary(lm.beta(young.mod1))
##
## Call:
## lm(formula = selfharm ~ stress + rum, data = young)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.5522 -0.1667 -0.0856 -0.0148 3.7137
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) 0.79246 NA 0.04533 17.483 < 2e-16 ***
## stress 0.10878 0.22582 0.01579 6.890 9.79e-12 ***
## rum 0.04318 0.08077 0.01752 2.464 0.0139 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4376 on 1011 degrees of freedom
## (57 observations deleted due to missingness)
## Multiple R-squared: 0.0714, Adjusted R-squared: 0.06956
## F-statistic: 38.87 on 2 and 1011 DF, p-value: < 2.2e-16
young.mod2 <- lm(selfharm ~ stress +rum + rum*stress, data = young)
summary(lm.beta(young.mod2))
##
## Call:
## lm(formula = selfharm ~ stress + rum + rum * stress, data = young)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.4834 -0.1724 -0.0894 -0.0110 3.7165
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) 0.72583 NA 0.10330 7.026 3.9e-12 ***
## stress 0.13848 0.28748 0.04429 3.127 0.00182 **
## rum 0.07036 0.13161 0.04172 1.686 0.09204 .
## stress:rum -0.01147 -0.09617 0.01597 -0.718 0.47301
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.4377 on 1010 degrees of freedom
## (57 observations deleted due to missingness)
## Multiple R-squared: 0.07187, Adjusted R-squared: 0.06912
## F-statistic: 26.07 on 3 and 1010 DF, p-value: 3.018e-16
old <- subset(d1, agec =="1")
old.mod1 <- lm(selfharm ~ stress +rum, data = old)
summary(lm.beta(old.mod1))
##
## Call:
## lm(formula = selfharm ~ stress + rum, data = old)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.2206 -0.2748 -0.0801 0.1300 3.3500
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) 0.25430 NA 0.07164 3.550 0.000412 ***
## stress 0.18049 0.25018 0.02759 6.543 1.18e-10 ***
## rum 0.23983 0.31134 0.02946 8.142 1.84e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.558 on 684 degrees of freedom
## (16 observations deleted due to missingness)
## Multiple R-squared: 0.2351, Adjusted R-squared: 0.2328
## F-statistic: 105.1 on 2 and 684 DF, p-value: < 2.2e-16
old.mod2 <- lm(selfharm ~ stress +rum + rum*stress, data = old)
summary(lm.beta(old.mod1))
##
## Call:
## lm(formula = selfharm ~ stress + rum, data = old)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.2206 -0.2748 -0.0801 0.1300 3.3500
##
## Coefficients:
## Estimate Standardized Std. Error t value Pr(>|t|)
## (Intercept) 0.25430 NA 0.07164 3.550 0.000412 ***
## stress 0.18049 0.25018 0.02759 6.543 1.18e-10 ***
## rum 0.23983 0.31134 0.02946 8.142 1.84e-15 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.558 on 684 degrees of freedom
## (16 observations deleted due to missingness)
## Multiple R-squared: 0.2351, Adjusted R-squared: 0.2328
## F-statistic: 105.1 on 2 and 684 DF, p-value: < 2.2e-16
#GRUP LEBIH MUDA
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.2
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
#low–med–high
stress_vals <- with(young, mean(stress, na.rm=TRUE) + c(-1,0,1)*sd(stress, na.rm=TRUE))
rum_vals <- with(young, mean(rum, na.rm=TRUE) + c(-1,0,1)*sd(rum, na.rm=TRUE))
labs <- c("low","med","high")
#grid kombinasi
df_plot <- expand.grid(
stress = stress_vals,
rum = rum_vals
)
df_plot$stress_level <- factor(rep(labs, times=3), levels = labs)
df_plot$rumination_level <- factor(rep(labs, each = 3), levels = labs)
#prediksi
b <- coef(young.mod2)
df_plot$selfharm_pred <- with(df_plot,
b["(Intercept)"] + b["stress"]*stress + b["rum"]*rum + b["stress:rum"]*stress*rum
)
#plot
ggplot(df_plot, aes(stress_level, selfharm_pred, group = rumination_level,
shape = rumination_level)) +
geom_line() +
geom_point(size = 3) +
labs(title = "Moderation by Rumination",
x = "Stress", y = "Self-harm",
shape = "Rumination") +
theme_minimal(base_size = 14)

#GRUP LEBIH TUA
#low–med–high
stress_vals <- with(old, mean(stress, na.rm=TRUE) + c(-1,0,1)*sd(stress, na.rm=TRUE))
rum_vals <- with(old, mean(rum, na.rm=TRUE) + c(-1,0,1)*sd(rum, na.rm=TRUE))
labs <- c("low","med","high")
#grid kombinasi
df_plot <- expand.grid(
stress = stress_vals,
rum = rum_vals
)
df_plot$stress_level <- factor(rep(labs, times=3), levels = labs)
df_plot$rumination_level <- factor(rep(labs, each = 3), levels = labs)
#prediksi
b <- coef(old.mod2)
df_plot$selfharm_pred <- with(df_plot,
b["(Intercept)"] + b["stress"]*stress + b["rum"]*rum + b["stress:rum"]*stress*rum
)
#plot
ggplot(df_plot, aes(stress_level, selfharm_pred, group = rumination_level,
shape = rumination_level)) +
geom_line() +
geom_point(size = 3) +
labs(title = "Moderation by Rumination",
x = "Stress", y = "Self-harm",
shape = "Rumination") +
theme_minimal(base_size = 14)
