我正在尝试将ax*log(x)模型拟合到数据中.拟合成功完成但我在解释结果系数时遇到困难.这是我的代码的快照.
x <- c(6, 11, 16, 21, 26, 31, 36, 41, 46, 51)
y <- c(5.485, 6.992, 7.447, 8.134, 8.524, 8.985, 9.271, 9.647, 10.561, 9.971)
fit <- lm(y ~ x*log(x))
coef(fit)
> (Intercept) x log(x) x:log(x)
3.15224227 0.10020022 1.12588040 -0.01322249
Run Code Online (Sandbox Code Playgroud)
我应该如何解释这些系数?我们称他们为a,b,c,d.我应该把它们放在公式"x*log(x)"中?
我在这个链接中展示了如何使用lm()数据框
然而(对R来说是全新的)我对systax还有点不清楚?
是否有更多的这种添加的.到y ~呢,还是仅仅代表着你已经从向量输入到数据帧输入感动?
对于以下数据:
require(dplyr)
require(ggplot2)
ds <- read.table(header = TRUE, text ="
obs id year attend
1 47 2000 1
2 47 2001 3
3 47 2002 5
4 47 2003 8
5 47 2004 6
6 47 2005 4
7 47 2006 2
8 47 2007 1
9 47 2008 2
10 47 2009 3
11 47 2010 4
12 47 2011 5
")
print(ds)
Run Code Online (Sandbox Code Playgroud)
我想计算线性模型的预测值
linear<- predict(lm(attend ~ year, ds))
quadratic<- predict(lm(attend ~ year + I(year^2),ds))
cubic<- predict(lm(attend ~ year …Run Code Online (Sandbox Code Playgroud) 假设我有三个数据框的列表
set.seed(55)
df1 <- data.frame(a=rnorm(6), b=rnorm(6), c=rnorm(6))
df2 <- data.frame(a=rnorm(6), b=rnorm(6), c=rnorm(6))
df3 <- data.frame(a=rnorm(6), b=rnorm(6), c=rnorm(6))
l <- list(df1, df2, df3)
Run Code Online (Sandbox Code Playgroud)
我现在想用每个数据帧的a和c做一个线性模型.我尝试了以下内容
for(i in l) {
x <- (lm(l[[i]]$a~l[[i]]$c))
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误
Error in l[[i]] : invalid subscript type 'list'
Run Code Online (Sandbox Code Playgroud)
我想有一个列表,每个元素都是一个l和一个c.任何帮助,将不胜感激.非常感谢你!
由于我必须使用的数据集的大小Speedlm,fastLm或biglm.不幸的是我坚持使用speedlm作为fastlm不具有update的功能,并且biglm只支持单核心.
使用speedlm我想显示所有残差.我知道,lm或者fastlm我可以简单地使用该residuals()功能.然而事实证明speedlm不支持这一点.
lmfit <- speedglm(formula , res)
print(names(lmfit))
[1] "coefficients" "coef" "df.residual" "XTX" "Xy" "nobs" "nvar" "ok" "A" "RSS" "rank" "pivot" "sparse" "yy" "X1X" "intercept" "method" "terms" "call"
lmfit <- fastLm(formula, res)
print(names(lmfit))
[1] "coefficients" "stderr" "df.residual" "fitted.values" "residuals" "call" "intercept" "formula"
Run Code Online (Sandbox Code Playgroud)
有没有办法显示所有残差speedlm?
尝试print(residuals(lmfit))时只打印一个NULL
编辑:
当使用@Roland提到的方法时,它返回纯粹NA的
lmfit <- speedlm(formula , res, …Run Code Online (Sandbox Code Playgroud) 似乎lm()从一个函数内部调用或通过lapply拧紧$call与一个拟合相关联.最小的工作示例:
> library(MASS)
> dat <- data.frame(x = 1:100, y=1:100)
> dat <- within(dat, z <- x + log(y) + rnorm(100))
> fits <- lapply(list(z ~ x + y, z ~ x + log(y)), lm, dat)
> stepAIC(fits[[1]]) # <-- error when I try to use the fit in other functions
Error in eval(expr, envir, enclos) : could not find function "FUN"
> fits[[1]]$call
FUN(formula = X[[i]], data = ..1) # Aha -- this …Run Code Online (Sandbox Code Playgroud) 我似乎splines::ns()在R中的函数有问题.
我创建了一个简单的虚拟问题
dat <- data.frame(t <- seq(0, 6, .01),
x <- rnorm(length(t), sd = 1),
y <- 5 + t - x^2 + rnorm(length(t), sd = .33))
lm(y ~ t + I(x^2), data = dat)
library(splines)
lm(y ~ t + ns(x, knots = c(0), Boundary.knots = c(-3, 3)), data = dat)
Run Code Online (Sandbox Code Playgroud)
虽然第一个模型工作正常,但第二个模型无法正确识别拦截.我在这里错过了什么?
该问题涉及机器学习特征选择过程.
我有一个很大的特征矩阵 - 列是主题(行)的特征:
set.seed(1)
features.mat <- matrix(rnorm(10*100),ncol=100)
colnames(features.mat) <- paste("F",1:100,sep="")
rownames(features.mat) <- paste("S",1:10,sep="")
Run Code Online (Sandbox Code Playgroud)
S在不同条件(C)下测量每个受试者()的响应,因此看起来像这样:
response.df <-
data.frame(S = c(sapply(1:10, function(x) rep(paste("S", x, sep = ""),100))),
C = rep(paste("C", 1:100, sep = ""), 10),
response = rnorm(1000), stringsAsFactors = F)
Run Code Online (Sandbox Code Playgroud)
所以我匹配的主题是response.df:
match.idx <- match(response.df$S, rownames(features.mat))
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种快速计算每个特征和响应的单变量回归的方法.
比这更快的东西?:
fun <- function(f){
fit <- lm(response.df$response ~ features.mat[match.idx,f])
beta <- coef(summary(fit))
data.frame(feature = colnames(features.mat)[f], effect = beta[2,1],
p.val = beta[2,4], stringsAsFactors = F))
}
res <- do.call(rbind, lapply(1:ncol(features.mat), …Run Code Online (Sandbox Code Playgroud) 假设我有这个数据框:
df <- data.frame(GN1 = sample(1:10, 10 ,replace=TRUE),
GN2 = sample(1:10, 10 ,replace=TRUE),
GN3 = sample(1:10, 10 ,replace=TRUE),
E10 = sample(1:10, 10 ,replace=TRUE),
PSV7 = sample(1:10, 10 ,replace=TRUE),
PEC3 = sample(1:10, 10 ,replace=TRUE),
PEC4 = sample(1:10, 10 ,replace=TRUE),
AC6 = sample(1:10, 10 ,replace=TRUE),
AC7 = sample(1:10, 10 ,replace=TRUE),
stringsAsFactors = FALSE)
GN1 GN2 GN3 E10 PSV7 PEC3 PEC4 AC6 AC7
1 7 3 10 6 4 4 3 9 3
2 2 5 6 6 6 6 5 7 1 …Run Code Online (Sandbox Code Playgroud) 我想对数据框进行分组Participant并迭代地应用一个简单的线性模型公式lm(Outcome ~ A, data = mydata),以便最终得到一个新的单独的数据框,每个数据框具有一个系数Participant。
这是以下示例mydata:
structure(list(Participant = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6,
6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, …Run Code Online (Sandbox Code Playgroud) lm ×10
r ×10
regression ×4
dplyr ×2
cubic-spline ×1
formula ×1
list ×1
modeling ×1
predict ×1
spline ×1