User Tools

Site Tools


interaction_effects_in_regression_analysis

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
interaction_effects_in_regression_analysis [2023/06/12 08:50] – [E.g. 3] hkimscilinteraction_effects_in_regression_analysis [2025/06/16 13:00] (current) – [E.g.2] hkimscil
Line 96: Line 96:
  
 ====== Two category variables ====== ====== Two category variables ======
-<code>> set.seed(12)+<code> 
 +> set.seed(12)
 > f1<-gl(n=2,k=30,labels=c("Low","High")) > f1<-gl(n=2,k=30,labels=c("Low","High"))
 > f2<-as.factor(rep(c("A","B","C"),times=20)) > f2<-as.factor(rep(c("A","B","C"),times=20))
Line 272: Line 273:
   - f1High:f2C : 질소가 High이고 온도도 High인 상태 -1.16 감소한다.    - f1High:f2C : 질소가 High이고 온도도 High인 상태 -1.16 감소한다. 
  
-<code>interact_plot(mod2, pred = "f1", modx = "f2")</code> +<code
-{{:r:interaction.effects.2.jpeg}}+> interact_plot(mod2, pred = "f1", modx = "f2") 
 +</code> 
 +{{:pasted:20250616-072703.png?400}} 
 +<code> 
 +> interact_plot(mod2, pred = "f2", modx = "f1"
 +</code> 
 +{{:pasted:20250616-072946.png?400}} 
 +:r:interaction.effects.2.jpeg
 ====== Two continuous variables ====== ====== Two continuous variables ======
 <code># third case interaction between two continuous variables <code># third case interaction between two continuous variables
Line 379: Line 387:
   - (위의 마지막 식에서) x1:x2 = x1*x2 : 질소량이 1씩 증가할 때 마다, 온도의 영향력은 1.5식 증가한다. 예를 들면 질소량이 0일 경우, 온도와 작물 간의 기울기는 약 2인데, 질소의 양이 1 증가하고 온도가 1 증가하면 기울기는 2 + 1.5 = 3.5가 된다.    - (위의 마지막 식에서) x1:x2 = x1*x2 : 질소량이 1씩 증가할 때 마다, 온도의 영향력은 1.5식 증가한다. 예를 들면 질소량이 0일 경우, 온도와 작물 간의 기울기는 약 2인데, 질소의 양이 1 증가하고 온도가 1 증가하면 기울기는 2 + 1.5 = 3.5가 된다. 
   - <WRAP box><code>   - <WRAP box><code>
-x1=1,x2=1: 0.97 + *3.5 x1 + -1 x2 +# 0.97 = 1 로 보면 
-x1=2,x2=2: 0.97 + *5.0 x1 + -1 x2 +x2=1: 0.97 + *3.5 x1 + -1 (1=x2) 
-x1=3,x2=3: 0.97 + *6.5 x1 + -1 x2 +      0 + 3.5 x1 
-x1=4,x2=4: 0.97 + *8.0 x1 + -1 x2 +x2=2: 0.97 + *5.0 x1 + -1 (2=x2) 
-x1=5,x2=5: 0.97 + *9.5 x1 + -1 x2</code>+      -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 
 +</code>
 <code> <code>
 *(1.995115 + 1.499595*x2):  *(1.995115 + 1.499595*x2): 
Line 405: Line 420:
 ====== E.g.2  ====== ====== E.g.2  ======
 {{:r:states.rds}} {{:r:states.rds}}
-Download the data file to c:/Rstatistics firstThen +<code> 
-do  +# states.data <- readRDS("c:/Rstatistics/dataSets/states.rds" 
-<code>states.data <- readRDS("c:/Rstatistics/dataSets/states.rds") </code> +states.data <- readRDS(url("http://commres.net/wiki/_media/r/states.rds")
 +</code> 
  
 Or, read the above data file directly Or, read the above data file directly
Line 455: Line 471:
 </code> </code>
 <code> <code>
-> data.info <- data.frame(attributes(data)[c("names", "var.labels")])+> data.info <- data.frame(attributes(states.data)[c("names", "var.labels")])
 > # 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 909: Line 925:
 </code> </code>
 <code> <code>
 +> 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:
 +   Min     1Q Median     3Q    Max 
 +-880.9 -397.3  -51.3  333.1 1960.7 
 +
 +Coefficients:
 +            Estimate Std. Error t value Pr(>|t|)    
 +(Intercept)   4890.5      187.6   26.06   <2e-16 ***
 +Illiteracy    -548.7      184.6   -2.97   0.0046 ** 
 +Murder          25.4       30.5    0.83   0.4089    
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 560 on 47 degrees of freedom
 +Multiple R-squared:  0.203, Adjusted R-squared:  0.169 
 +F-statistic: 5.98 on 2 and 47 DF,  p-value: 0.00486
 +
 +> summary(fiti)
 +
 +Call:
 +lm(formula = Income ~ Illiteracy * Murder, data = as.data.frame(state.x77))
 +
 +Residuals:
 +   Min     1Q Median     3Q    Max 
 +-955.2 -326.0   10.7  300.0 1892.1 
 +
 +Coefficients:
 +                  Estimate Std. Error t value Pr(>|t|)    
 +(Intercept)         3822.6      405.3    9.43  2.5e-12 ***
 +Illiteracy           617.3      434.9    1.42   0.1624    
 +Murder               146.8       50.3    2.92   0.0054 ** 
 +Illiteracy:Murder   -117.1       40.1   -2.92   0.0054 ** 
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 520 on 46 degrees of freedom
 +Multiple R-squared:  0.327, Adjusted R-squared:  0.283 
 +F-statistic: 7.46 on 3 and 46 DF,  p-value: 0.000359
 +
 +
 </code> </code>
 <code>> interact_plot(fiti, pred = "Illiteracy", modx = "Murder")</code> <code>> interact_plot(fiti, pred = "Illiteracy", modx = "Murder")</code>
interaction_effects_in_regression_analysis.1686527418.txt.gz · Last modified: 2023/06/12 08:50 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki