[英]How to make a plot using plotly without typing manually the name of the variables?
[英]How to do a regression of a series of variables without typing each variable name
我想对数据集中的一堆自变量进行回归分析。 预测变量很多,所以我不想全部写出来。 是否存在跨多列的表示法,所以我不必每次都键入?
我的尝试是这样做的(我的预测指标在第20至43列中):
modelAllHexSubscales = lm(HHdata$garisktot~HHdata[,20:43])
显然,这是行不通的,因为HHdata[,20:43]
是数据矩阵,而我真的需要它来将数据视为HHdata[,20]+HHdata[,21]
等。
这是另一种选择:
# if garisktot is in columns 20:43
modelAllHexSubscales <- lm(garisktot ~ ., data=HHdata[,20:43])
# if it isn't
modelData <- data.frame(HHdata["garisktot"],HHdata[,20:43])
modelAllHexSubscales <- lm(garisktot ~ ., data=modelData)
首先粘贴列名称来生成公式。
f <- as.formula(paste('garisktot ~', paste(colnames(HHdata)[20:43], collapse='+')))
modelAllHexSubscales <- lm(f, HHdata)
您是否尝试过直接这样做,例如
> y
[1] 10 19 30 42 51 59 72 78
> X
[,1] [,2]
[1,] 1 1.0
[2,] 2 3.0
[3,] 3 5.5
[4,] 4 7.0
[5,] 5 9.0
[6,] 6 11.0
[7,] 7 13.0
[8,] 8 16.0
> summary(lm(y ~ X))
Call:
lm(formula = y ~ X)
Residuals:
1 2 3 4 5 6 7 8
-0.1396 -1.2774 0.9094 1.4472 0.3094 -1.8283 1.0340 -0.4547
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -2.647 2.004 -1.321 0.24366
X1 15.436 3.177 4.859 0.00464 **
X2 -2.649 1.535 -1.726 0.14490
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 1.363 on 5 degrees of freedom
Multiple R-squared: 0.9978, Adjusted R-squared: 0.9969
F-statistic: 1124 on 2 and 5 DF, p-value: 2.32e-07
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.