所以我试图比较不同的线性模型,以确定一个是否优于另一个.但是我有几个模型,所以我想创建一个模型列表然后调用它们.那可能吗?
Models <- list(lm(y~a),lm(y~b),lm(y~c)
Models2 <- list(lm(y~a+b),lm(y~a+c),lm(y~b+c))
anova(Models2[1],Models[1])
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!
我有这些循环:
xall = data.frame()
for (k in 1:nrow(VectClasses))
{
for (i in 1:nrow(VectIndVar))
{
xall[i,k] = sum(VectClasses[k,] == VectIndVar[i,])
}
}
Run Code Online (Sandbox Code Playgroud)
数据:
VectClasses =包含每个类别特征的数据框
VectIndVar =包含数据库的每个记录的数据框
这两个for循环可以工作并提供我可以使用的输出,但是,这花费的时间太长,因此我需要Apply系列
我正在寻找的输出是这样的:
V1 V2 V3 V4
1 3 3 2 2
2 2 2 1 1
3 3 4 3 3
4 3 4 3 3
5 4 4 3 3
6 3 2 3 3
Run Code Online (Sandbox Code Playgroud)
我尝试使用:
xball = data.frame()
xball = sapply(xball, function (i,k){
sum(VectClasses[k,] == VectIndVar[i,])})
xcall = data.frame()
xcall = lapply(xcall, …Run Code Online (Sandbox Code Playgroud) x <- 1:9
names(x) <- paste0("x",x)
y <- 2:5
names(y) <- paste0("y",y)
fun1 <-function(a, b) {paste(class(a),b, sep = "**")} #works
funError <-function(a, b) {paste(class(a),class(b), sep = "**")} #does not work with outer
funNoError<-function(a, b) {paste(a,class(a),class(b),b, sep = "**")} #works with outer
funError(1,2) #is a valid function
outer(x, y, "funError") # fails
outer(x, y, "funNoError") # works
Run Code Online (Sandbox Code Playgroud)
Q1:为什么不起作用outer(x, y, "funError")?
dim(robj) <- c(dX, dY) 中的错误:dims [产品 36] 与对象 [1] 的长度不匹配
Q2:为什么outer(x, y, "funNoError")有效?它非常相似。
我能看到的唯一区别是,的每个“结果”funError …
我想提请只有R中,使用一个汉克尔矩阵matrix(),seq()和rep()R的功能到现在为止,我以某种方式作出这样的:
#Do this exercise with other packages, need to rework
install.packages("matrixcalc")
library(matrixcalc)
E1 <- hankel.matrix( 5, seq( 1, 9 ) )
print(E1)
#Use matrix() only, not efficient
E2 <- matrix(c(1,2,3,4,5,2,3,4,5,6,3,4,5,6,7,4,5,6,7,8,5,6,7,8,9), ncol=5)
print(E2)
#Use seq() but not worked
E3 <- matrix(c(seq(1:5),seq(2:6),seq(3:7),seq(4:8),seq(5:9)), ncol=5)
print(E3)
Run Code Online (Sandbox Code Playgroud)
E1 使用了一个库来绘制一个 Hankel 矩阵,在 E2 中,我尝试手动输入数字来绘制一个,但是如果我想要一个新的大矩阵会花费很多时间。我尝试使用seq()但它不起作用。它会画成这样:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] …Run Code Online (Sandbox Code Playgroud) 这是我的代码:步骤1:定义一个我将在稍后使用的反函数
inverse = function (f, lower = -100, upper = 100) {
function (y) uniroot((function (x) f(x) - y), lower = lower, upper = upper)[1]
}
Run Code Online (Sandbox Code Playgroud)
第2步:这是我的功能和他们的反向:
F1<-function(x,m1,l,s1,s2){l*pnorm((x-m1)/s1)+(1-l)*pnorm((x+m1)/s2)}
F1_inverse = inverse(function(x) F1(x,1,0.1,2,1) , -100, 100)
F2<-function(x,m2,l,s1,s2){l*pnorm((x-m2)/s1)+(1-l)*pnorm((x+m2)/s2)}
F2_inverse = inverse(function(x) F1(x,1,0.1,2,1) , -100, 100)
Run Code Online (Sandbox Code Playgroud)
Step3:这是我的最终功能,它结合了上述功能(我确信功能是正确的):
copwnorm<-function(x,y,l,mu1,mu2,sd1,sd2) {
(l*dnorm(((F1_inverse(pnorm(x))$root-mu1)/sd1))*
dnorm(((F2_inverse(pnorm(y))$root-mu2)/sd1)))
}
Run Code Online (Sandbox Code Playgroud)
Step4:我想在步骤enter code here3中为该函数创建等高线图:
x<-seq(-2,2,0.1)
y<-seq(-2,2,0.1)
z<-outer(x,y,copwnorm)
contour(x,y,z,xlab="x",ylab="y",nlevels=15)
Run Code Online (Sandbox Code Playgroud)
这是问题所在,当我尝试应用函数outer(x,y,copwnorm)时,它给出了一个错误:'zeroin'中的函数值无效.请问如何解决这个问题?