R 计算公式中的变量
我想计算进入公式右侧的变量数量。有没有这样的函数 例如:R 计算公式中的变量,r,formula,R,Formula,我想计算进入公式右侧的变量数量。有没有这样的函数 例如: y<-rnorm(100) x1<-rnorm(100) x2<-rnorm(100) x3<-rnorm(100) f<-formula(y~x1+x2+x3) y您可能需要查看公式的帮助页面中链接的一些相关函数。尤其是,术语: > terms(f) y ~ x1 + x2 + x3 + x4 attr(,"variables") list(y, x1, x2, x3, x4) attr(,"fac
y<-rnorm(100)
x1<-rnorm(100)
x2<-rnorm(100)
x3<-rnorm(100)
f<-formula(y~x1+x2+x3)
y您可能需要查看公式的帮助页面中链接的一些相关函数。尤其是,术语
:
> terms(f)
y ~ x1 + x2 + x3 + x4
attr(,"variables")
list(y, x1, x2, x3, x4)
attr(,"factors")
x1 x2 x3 x4
y 0 0 0 0
x1 1 0 0 0
x2 0 1 0 0
x3 0 0 1 0
x4 0 0 0 1
attr(,"term.labels")
[1] "x1" "x2" "x3" "x4"
attr(,"order")
[1] 1 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: R_GlobalEnv>
术语(f)
y~x1+x2+x3+x4
属性(,“变量”)
列表(y、x1、x2、x3、x4)
属性(,“因子”)
x1x2x3x4
y 0 0 0 0 0
x1 100 0 0
x20100
x3 0 1 0
x401
属性(,“术语标签”)
[1] “x1”“x2”“x3”“x4”
属性(,“订单”)
[1] 1 1 1 1
属性(,“截取”)
[1] 1
属性(,“响应”)
[1] 1
属性(,“.Environment”)
请注意“term.labels”属性。这里有两种可能性:
length(attr(terms(f), "term.labels"))
length(all.vars(update(f, z ~.))) - 1
根据您的评论,这可能取决于您如何拟合模型
对于线性模型,这些答案都给出了12
:
set.seed(1)
df1 <- data.frame (y=rnorm(100),
x=rnorm(100),
months=sample(letters[1:12], replace=TRUE, size=100))
f1 <-formula(y~x+factor(months))
l1 <- lm(f1, data=df1)
ncol(l1$qr$qr)-1
此处qr
是用于拟合模型的矩阵qr分解。它将包含感兴趣的参数数量
您还可以从model.frame
中找到哪些变量是因子,例如:
length(unique(model.frame(l1)[["factor(months)"]]))
或者更一般地使用.getXlevels
,它将为您提供预测端每个因子的唯一值列表,如中所示:
length( stats::.getXlevels(terms(l1), model.frame(l1))[[1]] )
更新
@马克·米勒正在爬上一棵更好的树。如果您的模型具有可用的AIC
-类型方法,您应该能够使用该方法获取参数的数量。
对于lm
,它是stats
中的一个隐藏S3方法,因此如下所示:
stats:::extractAIC.lm(l1)[[1]] -1
如果您想计算估计参数的数量,正如您在G.Grothendieck回答下面的评论所建议的,您可以尝试下面的代码。我在误差项的系数中加了一个,就像AIC一样
n <- 20 # number of observations
B0 <- 2 # intercept
B1 <- -1.5 # slope 1
B2 <- 0.5 # slope 2
B3 <- -2.5 # slope 3
sigma2 <- 5 # residual variance
x1 <- sample(1:3, n, replace=TRUE) # categorical covariate
x12 <- ifelse(x1==2, 1, 0)
x13 <- ifelse(x1==3, 1, 0)
x3 <- round(runif(n, -5 , 5), digits = 3) # continuous covariate
eps <- rnorm(n, mean = 0, sd = sqrt(sigma2)) # error
y <- B0 + B1*x12 + B2*x13 + B3*x3 + eps # dependent variable
x1 <- as.factor(x1)
model1 <- lm(y ~ x1 + x3) # linear regression
model1
summary(model1)
n.coefficients <- as.numeric(sapply(model1, length)[1]) + 1
n.coefficients
# [1] 5
非常感谢。然而,如果我包括一个因子变量,它只算作一个。这有什么办法吗?例如:对于F,这是一个与您在帖子中提出的问题不同的问题。@BUML1290您需要更新您的问题,以获得有关公式中因子的答案。请注意,变量和术语之间存在差异。该问题涉及的是公式,而不是模型。
n <- 20 # number of observations
B0 <- 2 # intercept
B1 <- -1.5 # slope 1
B2 <- 0.5 # slope 2
B3 <- -2.5 # slope 3
sigma2 <- 5 # residual variance
x1 <- sample(1:3, n, replace=TRUE) # categorical covariate
x12 <- ifelse(x1==2, 1, 0)
x13 <- ifelse(x1==3, 1, 0)
x3 <- round(runif(n, -5 , 5), digits = 3) # continuous covariate
eps <- rnorm(n, mean = 0, sd = sqrt(sigma2)) # error
y <- B0 + B1*x12 + B2*x13 + B3*x3 + eps # dependent variable
x1 <- as.factor(x1)
model1 <- lm(y ~ x1 + x3) # linear regression
model1
summary(model1)
n.coefficients <- as.numeric(sapply(model1, length)[1]) + 1
n.coefficients
# [1] 5
# For each variable in a linear regression model, one coefficient exists
# An intercept coefficient exists as well
# Subtract -1 to account for the intercept
n.coefficients2 <- length(model1$coefficients) - 1
n.coefficients2
# [1] 5