interaction_effects_in_regression_analysis
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| interaction_effects_in_regression_analysis [2017/11/30 12:34] – [Ex. 2] hkimscil | interaction_effects_in_regression_analysis [2025/06/16 13:00] (current) – [E.g.2] hkimscil | ||
|---|---|---|---|
| Line 6: | Line 6: | ||
| ====== E.g. 1 One category and one continuous ====== | ====== E.g. 1 One category and one continuous ====== | ||
| Data 만들기 | Data 만들기 | ||
| + | < | ||
| + | x< | ||
| + | f1< | ||
| + | modmat< | ||
| + | coeff< | ||
| + | y< | ||
| + | dat< | ||
| + | |||
| + | dat | ||
| + | |||
| + | mod <- lm(y~x*f1) | ||
| + | summary(mod) | ||
| + | |||
| + | library(ggplot2) | ||
| + | library(jtools) # in case not loading, install.packages(" | ||
| + | library(interactions) | ||
| + | |||
| + | interact_plot(mod, | ||
| + | |||
| + | </ | ||
| + | |||
| < | < | ||
| > f1< | > f1< | ||
| Line 15: | Line 36: | ||
| 작물의 무게가 온도와 토양의 질소에 영향을 받을까? 라는 연구문제에서 추출된 데이터. 작물무게에 대한 질소함유량의 영향과 온도의 영향, 그리고 그 두 변인의 상호작용효과에 대해서 알고 싶다. | 작물의 무게가 온도와 토양의 질소에 영향을 받을까? 라는 연구문제에서 추출된 데이터. 작물무게에 대한 질소함유량의 영향과 온도의 영향, 그리고 그 두 변인의 상호작용효과에 대해서 알고 싶다. | ||
| - | < | + | < |
| y | y | ||
| 1 21.8693480 | 1 21.8693480 | ||
| Line 23: | Line 44: | ||
| 5 | 5 | ||
| 6 20.9093423 | 6 20.9093423 | ||
| - | 7 25.8828775 | + | </ |
| - | 8 | + | |
| - | 9 | + | |
| - | 10 20.2008030 | + | |
| - | 11 5.6307366 | + | |
| - | 12 24.5176666 | + | |
| - | 13 2.9836344 | + | |
| - | 14 24.9027322 | + | |
| - | 15 7.1347831 | + | |
| - | 16 21.0629404 | + | |
| - | 17 4.9330174 | + | |
| - | 18 23.3587524 | + | |
| - | 19 11.5338189 | + | |
| - | 20 28.2193423 | + | |
| - | 21 5.9288641 | + | |
| - | 22 4.4854811 | + | |
| - | 23 18.5213789 | + | |
| - | 24 3.5410098 | + | |
| - | 25 14.1031612 | + | |
| - | 26 4.2389757 High 1.241753022 | + | |
| - | 27 23.2650544 High 5.428188895 | + | |
| - | 28 24.0330453 High 5.586834035 | + | |
| - | 29 16.6518724 High 3.913568701 | + | |
| - | 30 4.2317570 High 1.012214390 | + | |
| - | 31 26.1115118 High 5.969692939 | + | |
| - | 32 13.1004694 High 3.023571330 | + | |
| - | 33 39.8678989 High 9.080975151 | + | |
| - | 34 16.2227452 High 3.806260177 | + | |
| - | 35 24.8683087 High 5.562874263 | + | |
| - | 36 43.4090915 High 9.818060326 | + | |
| - | 37 -0.2444546 High 0.007114746 | + | |
| - | 38 11.1822149 High 2.640837491 | + | |
| - | 39 43.5794651 High 9.933570819 | + | |
| - | 40 25.3860623 High 5.960662568 | + | |
| - | 41 26.5072704 High 6.139642103 | + | |
| - | 42 30.6778013 High 6.968976315 | + | |
| - | 43 27.4982726 High 6.315991175 | + | |
| - | 44 16.3318687 High 3.883612270 | + | |
| - | 45 37.8328875 High 8.564988615 | + | |
| - | 46 12.6958950 High 2.722899497 | + | |
| - | 47 32.9697332 High 7.371928061 | + | |
| - | 48 39.6831930 High 9.026269785 | + | |
| - | 49 41.7586348 High 9.542581048 | + | |
| - | 50 15.9704872 High 3.644464843</ | + | |
| < | < | ||
| Line 86: | Line 64: | ||
| Residual standard error: 0.4842 on 46 degrees of freedom | Residual standard error: 0.4842 on 46 degrees of freedom | ||
| Multiple R-squared: | Multiple R-squared: | ||
| - | F-statistic: | + | F-statistic: |
| + | </ | ||
| + | < | ||
| + | regression formula: | ||
| + | y hat ~ 1.35817 + 2.95059*x + -2.63301*f1High + 1.59598*x: | ||
| + | x: 질소 | ||
| + | f1: High | Low | ||
| + | </ | ||
| + | * when f1High = 0, | ||
| + | * '' | ||
| + | * x변인(질소)이 0인 상태일 때의 작물의 무게가 1.3g정도 된다는 것을 말한다. | ||
| + | * 다음의 x는 x변인(질소)의 단위가 1씩 증가할 때마다 작물의 무게는 2.9씩 증가한다는 것을 말한다. (일반적인 regression line 해석) | ||
| + | * when f1High = 1, | ||
| + | * '' | ||
| + | * '' | ||
| + | * '' | ||
| + | * x:f1High는 온도가 High일경우에 x의 영향력이 1.59 더 많다는 것을 말한다 (1.59598*x: | ||
| + | * 즉, x의 기울기(slope)는 온도에 따라서 변하는데, | ||
| + | * 또한 온도가 높을 상태일 때의 절편은 -1.27493 가 된다. | ||
| - | coefficient 해석 | ||
| - | - 우선 f1High 라는 변인은 High 상태(온도가 높은 상태)를 의미. 따라서 default 값(제어된 값)은 온도가 낮은 상태 (low). | ||
| - | - 절편 = 온도가 Low인 상태(제어상태)이며 x변인(질소)이 0인 상태일 때의 작물의 무게가 1.3g정도 된다는 것을 말한다. | ||
| - | - 다음의 x는 x변인(질소)의 단위가 1씩 증가할 때마다 작물의 무게는 2.9씩 증가한다는 것을 말한다. (일반적인 regression line 해석) | ||
| - | - 다음 x:f1High는 온도가 High일경우에 x의 영향력이 1.59 더 많다는 것을 말한다. 즉, x의 기울기(slope)는 온도에 따라서 변하는데, | ||
| < | < | ||
| library(jtools) # in case not loading, install.packages(" | library(jtools) # in case not loading, install.packages(" | ||
| Line 105: | Line 96: | ||
| ====== Two category variables ====== | ====== Two category variables ====== | ||
| - | < | + | < |
| + | > set.seed(12) | ||
| > f1< | > f1< | ||
| > f2< | > f2< | ||
| Line 201: | Line 193: | ||
| </ | </ | ||
| - | - 온도: | + | - 온도: |
| - 질소: A, B, C (Low, Medium, High) | - 질소: A, B, C (Low, Medium, High) | ||
| - 각각 Low가 control인 상태 (아웃풋에 High와 Medium, High)에 대한 정보가 출력) | - 각각 Low가 control인 상태 (아웃풋에 High와 Medium, High)에 대한 정보가 출력) | ||
| 이 때 coefficients를 이용해서 모델을 해석해보면 | 이 때 coefficients를 이용해서 모델을 해석해보면 | ||
| + | '' | ||
| + | | | f2A | f2B | f2C | | ||
| + | | f1Low | < | ||
| + | f2B = 0, | ||
| + | f2C = 0 | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | </ | ||
| + | f2A = 0, | ||
| + | f2C = 0 | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | </ | ||
| + | f2A = 0, | ||
| + | f2B = 0 | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | </ | ||
| + | | f1High | ||
| + | f1High = 1, | ||
| + | f2B = 0, | ||
| + | f2C = 0 | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | </ | ||
| + | f1High = 1, | ||
| + | f2A = 0, | ||
| + | f2B = 1, | ||
| + | f2C = 0 | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | </ | ||
| + | f1High = 1, | ||
| + | f2A = 0, | ||
| + | f2B = 0, | ||
| + | f2C = 1 | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | '' | ||
| + | </ | ||
| + | |||
| + | | | f2A | f2B | f2C | | ||
| + | | f1Low | < | ||
| + | '' | ||
| + | </ | ||
| + | '' | ||
| + | </ | ||
| + | '' | ||
| + | </ | ||
| + | | f1High | ||
| + | '' | ||
| + | </ | ||
| + | '' | ||
| + | </ | ||
| + | '' | ||
| + | </ | ||
| + | |||
| + | |||
| - 우선 f1High, f2B, f2C 가 나타나는 것은 f1Low, f2A가 default임을 의미 (즉, 온도가 낮고 질소함유량이 낮은 상태). | - 우선 f1High, f2B, f2C 가 나타나는 것은 f1Low, f2A가 default임을 의미 (즉, 온도가 낮고 질소함유량이 낮은 상태). | ||
| Line 215: | Line 273: | ||
| - f1High:f2C : 질소가 High이고 온도도 High인 상태 -1.16 감소한다. | - f1High:f2C : 질소가 High이고 온도도 High인 상태 -1.16 감소한다. | ||
| - | < | + | <code> |
| - | {{: | + | > interact_plot(mod2, |
| + | </ | ||
| + | {{: | ||
| + | < | ||
| + | > interact_plot(mod2, | ||
| + | </ | ||
| + | {{: | ||
| + | : | ||
| ====== Two continuous variables ====== | ====== Two continuous variables ====== | ||
| < | < | ||
| Line 301: | Line 366: | ||
| Residual standard error: 0.3264 on 46 degrees of freedom | Residual standard error: 0.3264 on 46 degrees of freedom | ||
| Multiple R-squared: | Multiple R-squared: | ||
| - | F-statistic: | + | F-statistic: |
| + | </ | ||
| + | |||
| + | < | ||
| + | y hat ~ 0.965921 + 1.995115*x1 + -0.993288*x2 + 1.499595*x1*x2 | ||
| + | y hat ~ 0.965921 + 1.995115*x1 + (-0.993288 + 1.499595*x1)*x2 | ||
| + | y hat ~ 0.965921 + (1.995115 + 1.499595*x2)*x1 + -0.993288*x2 | ||
| + | |||
| + | </ | ||
| - (Intercept): | - (Intercept): | ||
| - | - x1: nitrogen concentration of 0mg/g인 상태에서 1°C 증가할 때 마다 작물의 바이오매스량이 2 mg/g 증가 | + | - x1: |
| - | - x2: temperature of 0°C 상태에서 nitrogen concentration 이 1 mg/g 증가할 때 마다 작물의 바이오매스량이 ~1 mg/g 정도씩 감소 | + | - nitrogen concentration of 0 mg/g인 상태에서 |
| - | - x1:x2 : 질소량이 1씩 증가할 때 마다, 온도의 영향력은 1.5식 증가한다. 예를 들면 질소량이 0일 경우, 온도와 작물 간의 기울기는 약 2인데, 질소의 양이 1 증가하고 온도가 1 증가하면 기울기는 2 + 1.5 = 3.5가 된다. | + | - 온도 x1이 1°C 증가할 때 마다 |
| + | - 작물의 바이오매스량이 | ||
| + | - x2: | ||
| + | - temperature of 0°C 상태에서 | ||
| + | - x2인 nitrogen concentration 이 1 mg/g 증가할 때 마다 | ||
| + | - 작물의 바이오매스량이 ~1 mg/g 정도씩 감소 | ||
| + | - (위의 마지막 식에서) | ||
| + | - <WRAP box>< | ||
| + | # 0.97 = 1 로 보면 | ||
| + | x2=1: 0.97 + *3.5 x1 + -1 (1=x2) | ||
| + | 0 + 3.5 x1 | ||
| + | x2=2: 0.97 + *5.0 x1 + -1 (2=x2) | ||
| + | -1 + 5.0 x1 | ||
| + | x2=3: 0.97 + *6.5 x1 + -1 (3=x2) | ||
| + | -2 + 6.5 x1 | ||
| + | x2=4: 0.97 + *8.0 x1 + -1 (4=x2) | ||
| + | -3 + 8.0 x1 | ||
| + | x2=5: 0.97 + *9.5 x1 + -1 (5=x2) | ||
| + | -4 + 9.5 x1 | ||
| + | </ | ||
| + | < | ||
| + | *(1.995115 + 1.499595*x2): | ||
| + | x2=1: 3.495 ~ *3.5 | ||
| + | x2=2: 4.994 ~ *5.0 | ||
| + | x2=3: 6.494 ~ *6.5 | ||
| + | x2=4: 7.994 ~ *8.0 | ||
| + | x2=5: 9.493 ~ *9.5 | ||
| + | </ | ||
| + | |||
| + | </ | ||
| {{https:// | {{https:// | ||
| Line 318: | Line 420: | ||
| ====== E.g.2 ====== | ====== E.g.2 ====== | ||
| {{: | {{: | ||
| - | Download the data file to c:/ | + | < |
| - | do | + | # states.data <- readRDS(" |
| - | < | + | states.data <- readRDS(url("http://commres.net/ |
| + | </ | ||
| Or, read the above data file directly | Or, read the above data file directly | ||
| < | < | ||
| z <- gzcon(url(" | z <- gzcon(url(" | ||
| - | data <- readRDS(z) | + | states.data <- readRDS(z) |
| head(states.data, | head(states.data, | ||
| Line 368: | Line 471: | ||
| </ | </ | ||
| < | < | ||
| - | > data.info <- data.frame(attributes(data)[c(" | + | > data.info <- data.frame(attributes(states.data)[c(" |
| > # attributes(data) reveals various attributes of the data file, | > # attributes(data) reveals various attributes of the data file, | ||
| > # which contains variable names and labels. | > # which contains variable names and labels. | ||
| Line 494: | Line 597: | ||
| > | > | ||
| </ | </ | ||
| + | |||
| + | < | ||
| + | y hat = 1048 + -0.004 expense + -3.809 percent + 0.00025 expense: | ||
| + | |||
| + | y hat = 1048 + -0.004 x1 + -3.809 x2 + 0.00025 x1x2 | ||
| + | y hat = 1048 + (-0.004 + 0.00025 x2) x1 + -3.809 x2 | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | |||
| + | > mm1 <- mean(states.data$percent) - sd(states.data$percent) | ||
| + | > mp1 <- mean(states.data$percent) + sd(states.data$percent) | ||
| + | > mp2 <- mean(states.data$percept) + (2*sd(states.data$percent) | ||
| + | > mo0 <- mean(states.data$percent) | ||
| + | > mm1 | ||
| + | [1] 9.571891 | ||
| + | > mp1 | ||
| + | [1] 61.95752 | ||
| + | > mp2 | ||
| + | [1] 88.15033 | ||
| + | > mo0 | ||
| + | [1] 35.76471 | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | > k <- c(mm1, mo0, mp1, mp2) | ||
| + | > k | ||
| + | [1] 9.571891 35.764706 61.957520 88.150335 | ||
| + | > ic <- 1048 + (-3.809 * k) | ||
| + | > ic | ||
| + | [1] 1011.5407 | ||
| + | > x1.slope <- -0.004 + 0.00025 * k | ||
| + | > x1.slope | ||
| + | [1] -0.001607027 | ||
| + | > | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | y hat when mm1 ~ 1011.5407 + -0.001607027 x1 | ||
| + | y hat when mo0 ~ 911.7722 | ||
| + | y hat when mp1 ~ 812.0038 | ||
| + | y hat when mp2 ~ 712.2354 | ||
| + | </ | ||
| + | |||
| + | * 위의 y hat은 (회귀식) 각각 percent 가 mean-1sd, mean, mean+1sd, 그리고 mean+2sd 일 경우의 식이 된다. | ||
| + | * x1 은 expense 이다. | ||
| + | * x2 는 10, 36, 62, 88% 의 응시율이다 | ||
| + | * 10% 일 때는 expense가 한단위 증가할 때 마다 csat점수는 -0.0016의 기울기를 갖는다 | ||
| + | * 36% 일 경우는 0.005 점 증가율을 갖는다 | ||
| + | * 62 일 경우는 0.012 점 증가율을 갖는다 | ||
| + | * 88 일 경우는 0.018 점 증가율을 갖는다 | ||
| + | |||
| < | < | ||
| library(jtools) # in case not loading, install.packages(" | library(jtools) # in case not loading, install.packages(" | ||
| + | library(interactions) # interact_plot requires this package now | ||
| </ | </ | ||
| Line 505: | Line 661: | ||
| {{: | {{: | ||
| + | |||
| + | {{: | ||
| + | {{: | ||
| + | ===== Analysis again ===== | ||
| + | < | ||
| + | attach(states.data) | ||
| + | n.1 <- lm(csat~expense) | ||
| + | n.2 <- lm(csat~percent) | ||
| + | n.12 <- lm(csat~expense+percent) | ||
| + | n.12i <- lm(csat~expense*percent) | ||
| + | n.1i <- lm(csat~expense+expense: | ||
| + | n.2i <- lm(csat~percent+expense: | ||
| + | n.2i.temp <- lm(csat~percent+percent: | ||
| + | n.i <- lm(csat~expense: | ||
| + | |||
| + | s.n.1 <- summary(n.1) | ||
| + | s.n.2 <- summary(n.2) | ||
| + | s.n.12 <- summary(n.12) | ||
| + | s.n.12i <- summary(n.12i) | ||
| + | s.n.1i <- summary(n.1i) | ||
| + | s.n.2i <- summary(n.2i) | ||
| + | s.n.2i.temp <- summary(n.2i.temp) | ||
| + | s.n.i <- summary(n.i) | ||
| + | |||
| + | s.n.12i | ||
| + | |||
| + | # y hat ~ 1048 + -0.003917 x1 + -3.809 x2 + 0.000249 x1 x2 | ||
| + | # y hat ~ 1048 + -0.003917 x1 + (-3.809 + 0.000249 x1) x2 | ||
| + | # x2를 (percent를) 중심으로 보기 | ||
| + | |||
| + | e.mm1 <- mean(expense)-sd(expense) | ||
| + | e.m0 <- mean(expense) | ||
| + | e.mp1 <- mean(expense)+sd(expense) | ||
| + | e.mp2 <- mean(expense)+(2*sd(expense)) | ||
| + | |||
| + | # x1의 case가 4가지 (holding constants) | ||
| + | k <- c(e.mm1, e.m0, e.mp1, e.mp2) | ||
| + | ic <- 1048 + (-0.003917*k) | ||
| + | slp <- -3.809 + (0.000249*k) | ||
| + | ic | ||
| + | slp | ||
| + | |||
| + | # y hat ~ 1032.979 - 2.85 x2 | ||
| + | # y hat ~ 1027.491 - 2.51 x2 | ||
| + | # y hat ~ 1022.002 - 2.16 x2 | ||
| + | # y hat ~ 1016.514 - 1.81 x2 | ||
| + | |||
| + | interact_plot(n.12i, | ||
| + | pred = " | ||
| + | modx = " | ||
| + | | ||
| + | # or | ||
| + | mne <- min(expense) | ||
| + | mxe <- max(expense) | ||
| + | kk <- seq(mne, mxe, by = 2000) | ||
| + | interact_plot(n.12i, | ||
| + | pred = " | ||
| + | modx = " | ||
| + | | ||
| + | |||
| + | </ | ||
| + | |||
| + | < | ||
| + | > n.1 <- lm(csat~expense) | ||
| + | > n.2 <- lm(csat~percent) | ||
| + | > n.12 <- lm(csat~expense+percent) | ||
| + | > n.12i <- lm(csat~expense*percent) | ||
| + | > n.1i <- lm(csat~expense+expense: | ||
| + | > n.2i <- lm(csat~percent+expense: | ||
| + | > n.2i.temp <- lm(csat~percent+percent: | ||
| + | > n.i <- lm(csat~expense: | ||
| + | > | ||
| + | > s.n.1 <- summary(n.1) | ||
| + | > s.n.2 <- summary(n.2) | ||
| + | > s.n.12 <- summary(n.12) | ||
| + | > s.n.12i <- summary(n.12i) | ||
| + | > s.n.1i <- summary(n.1i) | ||
| + | > s.n.2i <- summary(n.2i) | ||
| + | > s.n.2i.temp <- summary(n.2i.temp) | ||
| + | > s.n.i <- summary(n.i) | ||
| + | > | ||
| + | > s.n.12i | ||
| + | |||
| + | Call: | ||
| + | lm(formula = csat ~ expense * percent) | ||
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -65.36 -19.61 | ||
| + | |||
| + | Coefficients: | ||
| + | | ||
| + | (Intercept) | ||
| + | expense | ||
| + | percent | ||
| + | expense: | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 30.8 on 47 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > | ||
| + | > # y hat ~ 1048 + -0.003917 x1 + -3.809 x2 + 0.000249 x1 x2 | ||
| + | > # y hat ~ 1048 + -0.003917 x1 + (-3.809 + 0.000249 x1) x2 | ||
| + | > # x2를 (percent를) 중심으로 보기 | ||
| + | > | ||
| + | > e.mm1 <- mean(expense)-sd(expense) | ||
| + | > e.m0 <- mean(expense) | ||
| + | > e.mp1 <- mean(expense)+sd(expense) | ||
| + | > e.mp2 <- mean(expense)+(2*sd(expense)) | ||
| + | > | ||
| + | > # x1의 case가 4가지 (holding constants) | ||
| + | > k <- c(e.mm1, e.m0, e.mp1, e.mp2) | ||
| + | > ic <- 1048 + (-0.003917*k) | ||
| + | > slp <- -3.809 + (0.000249*k) | ||
| + | > ic | ||
| + | [1] 1033 1027 1022 1017 | ||
| + | > slp | ||
| + | [1] -2.854 -2.505 -2.156 -1.807 | ||
| + | > | ||
| + | > # y hat ~ 1032.979 - 2.85 x2 | ||
| + | > # y hat ~ 1027.491 - 2.51 x2 | ||
| + | > # y hat ~ 1022.002 - 2.16 x2 | ||
| + | > # y hat ~ 1016.514 - 1.81 x2 | ||
| + | > | ||
| + | > interact_plot(n.12i, | ||
| + | + pred = " | ||
| + | + modx = " | ||
| + | + | ||
| + | > # or | ||
| + | > mne <- min(expense) | ||
| + | > mxe <- max(expense) | ||
| + | > kk <- seq(mne, mxe, by = 2000) | ||
| + | > interact_plot(n.12i, | ||
| + | + pred = " | ||
| + | + modx = " | ||
| + | + | ||
| + | > | ||
| + | </ | ||
| + | |||
| + | 아래 그림에서처럼 대학지원 퍼센티지가 높아지면 성적이 떨어지는 경향을 보이는데, | ||
| + | {{: | ||
| + | |||
| + | ===== 언제 interaction effect를 분석에 넣는가? ===== | ||
| + | interaction effects가 significant할 때에 넣는다 | ||
| + | significant하지 않을 때에는 additive model을 (+사인 모델) 사용한다. | ||
| ===== One categorical IV ===== | ===== One categorical IV ===== | ||
| Line 613: | Line 917: | ||
| [5] " | [5] " | ||
| </ | </ | ||
| - | + | 아래의 경우 interaction effect는 중요한 의미를 갖는다. additive model에서는 murder가 중요한 역할을 하지 않지만, interactive model에서는 Illiteracy와 결합하여 중요한 역할을 하는 것으로 해석될 수 있다. | |
| - | < | + | < |
| + | fit <- lm(Income ~ Illiteracy + Murder, data = as.data.frame(state.x77)) | ||
| + | fiti <- lm(Income ~ Illiteracy * Murder, data = as.data.frame(state.x77)) | ||
| + | summary(fit) | ||
| summary(fiti) | summary(fiti) | ||
| + | </ | ||
| + | < | ||
| + | > fit <- lm(Income ~ Illiteracy + Murder, data = as.data.frame(state.x77)) | ||
| + | > fiti <- lm(Income ~ Illiteracy * Murder, data = as.data.frame(state.x77)) | ||
| + | > summary(fit) | ||
| + | |||
| + | Call: | ||
| + | lm(formula = Income ~ Illiteracy + Murder, data = as.data.frame(state.x77)) | ||
| + | |||
| + | Residuals: | ||
| + | | ||
| + | -880.9 -397.3 | ||
| + | |||
| + | Coefficients: | ||
| + | Estimate Std. Error t value Pr(> | ||
| + | (Intercept) | ||
| + | Illiteracy | ||
| + | Murder | ||
| + | --- | ||
| + | Signif. codes: | ||
| + | |||
| + | Residual standard error: 560 on 47 degrees of freedom | ||
| + | Multiple R-squared: | ||
| + | F-statistic: | ||
| + | |||
| + | > summary(fiti) | ||
| + | |||
| Call: | Call: | ||
| lm(formula = Income ~ Illiteracy * Murder, data = as.data.frame(state.x77)) | lm(formula = Income ~ Illiteracy * Murder, data = as.data.frame(state.x77)) | ||
| Residuals: | Residuals: | ||
| - | | + | |
| - | -955.20 -325.99 10.66 299.96 1892.12 | + | -955.2 -326.0 10.7 300.0 1892.1 |
| Coefficients: | Coefficients: | ||
| Estimate Std. Error t value Pr(> | Estimate Std. Error t value Pr(> | ||
| - | (Intercept) | + | (Intercept) |
| - | Illiteracy | + | Illiteracy |
| - | Murder | + | Murder |
| - | Illiteracy: | + | Illiteracy: |
| --- | --- | ||
| Signif. codes: | Signif. codes: | ||
| - | Residual standard error: 520.1 on 46 degrees of freedom | + | Residual standard error: 520 on 46 degrees of freedom |
| - | Multiple R-squared: | + | Multiple R-squared: |
| - | F-statistic: | + | F-statistic: |
| > | > | ||
| </ | </ | ||
| - | |||
| < | < | ||
| Line 645: | Line 978: | ||
| {{: | {{: | ||
| - | < | ||
| - | interact_plot(fitiris, | ||
| - | {{: | + | ====== Eg. 4 ====== |
| + | < | ||
| + | # Load the data | ||
| + | data(" | ||
| + | set.seed(123) | ||
| + | training.samples <- marketing$sales %>% | ||
| + | createDataPartition(p = 0.8, list = FALSE) | ||
| + | train.data | ||
| + | test.data <- marketing[-training.samples, | ||
| + | |||
| + | model2 <- lm(sales ~ youtube + facebook + youtube: | ||
| + | data = marketing) | ||
| + | # Or simply, use this: | ||
| + | model2 <- lm(sales ~ youtube*facebook, | ||
| + | # Summarize the model | ||
| + | summary(model2) | ||
| + | |||
| + | interact_plot(model2, | ||
| + | |||
| + | </ | ||
| ====== Ex. ====== | ====== Ex. ====== | ||
| Use Cars93 dataset. | Use Cars93 dataset. | ||
| Line 662: | Line 1012: | ||
| Interprete what has been found. | Interprete what has been found. | ||
| - | ====== Ex. 2 ====== | + | |
| + | ====== Ex. 3 ====== | ||
| < | < | ||
| library(msm) | library(msm) | ||
interaction_effects_in_regression_analysis.1512014657.txt.gz · Last modified: by hkimscil
