我想計算進入公式右側的變量數量。有沒有這樣的功能?計算公式中的變量
例如:
y<-rnorm(100)
x1<-rnorm(100)
x2<-rnorm(100)
x3<-rnorm(100)
f<-formula(y~x1+x2+x3)
然後,我將稱之爲SomeFunction(f)
,其將返回圖3(因爲有上式的右手側3×變量)。 SomeFunction是否存在?
我想計算進入公式右側的變量數量。有沒有這樣的功能?計算公式中的變量
例如:
y<-rnorm(100)
x1<-rnorm(100)
x2<-rnorm(100)
x3<-rnorm(100)
f<-formula(y~x1+x2+x3)
然後,我將稱之爲SomeFunction(f)
,其將返回圖3(因爲有上式的右手側3×變量)。 SomeFunction是否存在?
如果你要計算估計參數的數量,通過以下G.格羅騰迪克的回答您的評論的建議,你可以嘗試下面的代碼。與AIC一樣,我爲錯誤項添加了一個到n.coefficients
。
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
這裏是一個更直接的替代代碼n.coefficients
:
n.coefficients2 <- length(model1$coefficients) + 1
n.coefficients2
# [1] 5
您可能需要查看formula
的幫助頁面中鏈接的一些相關功能。特別是,terms
:
> 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>
請注意「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
或
length(colnames(l1$qr$qr))-1
這裏qr
是用於擬合模型的QR decomposition of a matrix
。它將包含no。的感興趣的參數。
你也可以找到哪些變量是從model.frame
因素,如:
length(unique(model.frame(l1)[["factor(months)"]]))
或更一般與.getXlevels
,它會給你唯一值的列表上預測側各因素,在:
length(stats::.getXlevels(terms(l1), model.frame(l1))[[1]])
更新
@馬克·米勒被狂吠建立一個更好的樹。如果你的模型有一個AIC
類型的方法可用,你應該能夠使用它來獲得no。的參數。 對於lm
,它在stats
隱藏S3方法,所以這樣稱呼它:
stats:::extractAIC.lm(l1)[[1]] -1
這個問題問一個公式,而不是一個模型。 –