简体   繁体   中英

No longer self interaction in R?

I tried to run an interaction with the variable itself (eg, wt ~ cyl * cyl) to estimate a quadratic model (eg, wt ~ cyl + cyl^2) in order to use post-estimated tools for interactions. However, the result automatically ignores the interaction term. Is there a way I can specify the quadratic model in R? I thought I did that before but not in R 3.4.1 or 3.4.2. The following is a toy code, including my session info. Appreciate any comments and help!


summary(lm(wt ~ cyl * cyl, data = mtcars))
#> 
#> Call:
#> lm(formula = wt ~ cyl * cyl, data = mtcars)
#> 
#> Residuals:
#>     Min      1Q  Median      3Q     Max 
#> -0.8243 -0.4293 -0.1518  0.3031  1.4297 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  0.56462    0.40062   1.409    0.169    
#> cyl          0.42871    0.06228   6.883 1.22e-07 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Residual standard error: 0.6193 on 30 degrees of freedom
#> Multiple R-squared:  0.6123, Adjusted R-squared:  0.5994 
#> F-statistic: 47.38 on 1 and 30 DF,  p-value: 1.218e-07


devtools::session_info()
#> Session info -------------------------------------------------------------
#>  setting  value                       
#>  version  R version 3.4.2 (2017-09-28)
#>  system   x86_64, mingw32             
#>  ui       RTerm                       
#>  language (EN)                        
#>  collate  English_United States.1252  
#>  tz       America/Chicago             
#>  date     2017-10-04
#> Packages -----------------------------------------------------------------
#>  package   * version    date       source                            
#>  backports   1.1.1      2017-09-25 CRAN (R 3.4.1)                    
#>  base      * 3.4.2      2017-09-28 local                             
#>  compiler    3.4.2      2017-09-28 local                             
#>  datasets  * 3.4.2      2017-09-28 local                             
#>  devtools    1.13.3     2017-08-02 CRAN (R 3.5.0)                    
#>  digest      0.6.12     2017-01-27 CRAN (R 3.3.2)                    
#>  evaluate    0.10.1     2017-06-24 CRAN (R 3.4.0)                    
#>  formatR     1.5        2017-04-25 CRAN (R 3.5.0)                    
#>  graphics  * 3.4.2      2017-09-28 local                             
#>  grDevices * 3.4.2      2017-09-28 local                             
#>  htmltools   0.3.6      2017-04-28 CRAN (R 3.4.0)                    
#>  knitr       1.17       2017-08-10 CRAN (R 3.5.0)                    
#>  magrittr    1.5        2014-11-22 CRAN (R 3.3.2)                    
#>  memoise     1.1.0      2017-04-21 CRAN (R 3.4.0)                    
#>  methods   * 3.4.2      2017-09-28 local                             
#>  Rcpp        0.12.13    2017-09-28 CRAN (R 3.4.2)                    
#>  rmarkdown   1.6.0.9001 2017-07-03 Github (rstudio/rmarkdown@8cfe0cb)
#>  rprojroot   1.2        2017-01-16 CRAN (R 3.3.2)                    
#>  stats     * 3.4.2      2017-09-28 local                             
#>  stringi     1.1.5      2017-04-07 CRAN (R 3.4.1)                    
#>  stringr     1.2.0      2017-02-18 CRAN (R 3.3.2)                    
#>  tools       3.4.2      2017-09-28 local                             
#>  utils     * 3.4.2      2017-09-28 local                             
#>  withr       2.0.0      2017-10-03 Github (r-lib/withr@d1f0957)      
#>  yaml        2.1.14     2016-11-12 CRAN (R 3.3.2)

This should have never done a quadratic term in R. The * in a formula is for variable interaction (and a variable should not interact with itself). If you want quadratic terms use

summary(lm(wt ~ poly(cyl, 2, raw=TRUE), data = mtcars))

or

summary(lm(wt ~ cyl + I(cyl^2), data = mtcars))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM