[英]Perform multiple linear regression analysis including interaction terms, interpret results using summary() and diagnostic plots using lm()
我嘗試使用類似這樣的代碼執行多元線性回歸分析,但沒有成功。我嘗試使用 lm() function 進行分析。我認為“x1*x2”存在問題。
數據 <- data.frame(x1 = rnorm(100), x2 = rnorm(100), y = rnorm(100)) model <- lm(y ~ x1 + x2 + x1*x2) 摘要(模型) 圖(模型)
它向我顯示錯誤。 我應該怎么辦?
由於您的交互項,沒有發生錯誤。 測試時,這對我來說非常有效。 你忘了指定數據。 lm()
function 要求您提供變量應源自的數據。 在下面的代碼中,我還縮短了 function 中的代碼,因為x1*x2
已經足夠了。 R 檢測到您有交互項,因此您不必重復相同的變量名稱。
data <- data.frame(x1 = rnorm(100), x2 = rnorm(100), y = rnorm(100))
model <- lm(y ~ x1*x2,
data= data)
summary(model)
#>
#> Call:
#> lm(formula = y ~ x1 * x2, data = data)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.21772 -0.77564 0.06347 0.56901 2.15324
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -0.05853 0.09914 -0.590 0.5564
#> x1 0.17384 0.09466 1.836 0.0694 .
#> x2 -0.02830 0.08646 -0.327 0.7442
#> x1:x2 -0.00836 0.07846 -0.107 0.9154
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Residual standard error: 0.9792 on 96 degrees of freedom
#> Multiple R-squared: 0.03423, Adjusted R-squared: 0.004055
#> F-statistic: 1.134 on 3 and 96 DF, p-value: 0.3392
創建於 2023-01-14,使用reprex v2.0.2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.