R 在另一个函数中为lm函数提供子集
我想在函数中执行lm回归。像这样的 我甚至试过:R 在另一个函数中为lm函数提供子集,r,function,syntax,lm,R,Function,Syntax,Lm,我想在函数中执行lm回归。像这样的 我甚至试过: somefun<- function (vardep, varindep1, varindep2, DATA, subset=NULL) { summary(lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA, subset=paste(subset, ""))) } somefun("happiness", "educ", "income", DT, sub
somefun<- function (vardep, varindep1, varindep2, DATA, subset=NULL) {
summary(lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA, subset=paste(subset, "")))
}
somefun("happiness", "educ", "income", DT, subset=(year<2005))
somefun("happiness", "educ", "income", DT, subset="(year<2005)")
而summarylmpastehappiness、~、educ、+、income、data=DT、subset=year>2005效果良好
我应该怎么做?您可以将传递给lm的数据帧子集: 尝试?重新制定
panelID = c(1:50)
year= c(2001:2010)
country = c("NLD", "BEL", "GER")
urban = c("A", "B", "C")
indust = c("D", "E", "F")
sizes = c(1,2,3,4,5)
n <- 2
library(data.table)
set.seed(123)
DT <- data.table(panelID = rep(sample(panelID), each = n),
country = rep(sample(country, length(panelID), replace = T), each = n),
year = c(replicate(length(panelID), sample(year, n))),
some_NA = sample(0:5, 6),
Factor = sample(0:5, 6),
industry = rep(sample(indust, length(panelID), replace = T), each = n),
urbanisation = rep(sample(urban, length(panelID), replace = T), each = n),
size = rep(sample(sizes, length(panelID), replace = T), each = n),
income = round(runif(100)/10,2),
sales= round(rnorm(10,10,10),2),
happiness = sample(10,10),
Sex = round(rnorm(10,0.75,0.3),2),
Age = sample(100,100),
educ = round(rnorm(10,0.75,0.3),2))
DT [, uniqueID := .I] # Creates a unique ID
DT <- as.data.frame(DT)
somefun("happiness", "educ", "income", DT)
somefun<- function (vardep, varindep1, varindep2, DATA, subset=NULL) {
summary(lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA, subset=paste(subset)))
}
somefun("happiness", "educ", "income", DT, subset=(year<2005))
somefun("happiness", "educ", "income", DT, subset="(year<2005)")
somefun<- function (vardep, varindep1, varindep2, DATA, subset=NULL) {
summary(lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA, subset=paste(subset, "")))
}
somefun("happiness", "educ", "income", DT, subset=(year<2005))
somefun("happiness", "educ", "income", DT, subset="(year<2005)")
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
0 (non-NA) cases
somefun<- function (vardep, varindep1, varindep2, DATA, subset=NULL) {
summary(lm(paste(vardep, "~", varindep1, "+", varindep2), data = DATA[subset,]))
}
somefun("happiness", "educ", "income", DT, year < 2005)
#>
#> Call:
#> lm(formula = paste(vardep, "~", varindep1, "+", varindep2), data = DATA[subset,
#> ])
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -2.60145 -2.30747 -0.09213 2.30536 2.73841
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 10.553 1.269 8.314 5.44e-10 ***
#> educ -3.780 1.047 -3.609 0.000904 ***
#> income -5.912 12.760 -0.463 0.645880
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 2.386 on 37 degrees of freedom
#> Multiple R-squared: 0.2672, Adjusted R-squared: 0.2276
#> F-statistic: 6.746 on 2 and 37 DF, p-value: 0.003179
somefun("happiness", "educ", "income", DT, year > 2005)
#>
#> Call:
#> lm(formula = paste(vardep, "~", varindep1, "+", varindep2), data = DATA[subset,
#> ])
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -3.7577 -0.9417 -0.2801 1.1769 3.8386
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 3.387 1.246 2.719 0.00914 **
#> educ 1.484 1.323 1.121 0.26790
#> income 8.306 11.702 0.710 0.48136
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 2.353 on 47 degrees of freedom
#> Multiple R-squared: 0.02928, Adjusted R-squared: -0.01203
#> F-statistic: 0.7087 on 2 and 47 DF, p-value: 0.4975