我在数据A中安装了一个混合模型,如下所示:
model <- lme(Y~1+X1+X2+X3, random=~1|Class, method="ML", data=A)
Run Code Online (Sandbox Code Playgroud)
接下来,我想看看模型如何适合数据B并获得估计的残差.R中是否有可以使用的功能?
(我尝试了以下方法,但获得了所有新系数.)
model <- lme(Y~1+X1+X2+X3, random=~1|Class, method="ML", data=B)
Run Code Online (Sandbox Code Playgroud) 我理解"function(x)"是如何工作的,但是"function()"的作用是什么?
z <- function() {
y <- 2
function(x) {
x + y
}
}
Run Code Online (Sandbox Code Playgroud) 我们在课堂上看到了以下R代码:
attach(LifeCycleSavings)
boxplot(sr, main = "Box Plot of Savings Ratio")
detach()
Run Code Online (Sandbox Code Playgroud)
但是,为什么我们需要在这里使用"detach()"?我键入"LifeCycleSavings",仍然得到如下输出:
> LifeCycleSavings
sr pop15 pop75 dpi ddpi
Australia 11.43 29.35 2.87 2329.68 2.87
Austria 12.07 23.32 4.41 1507.99 3.93
Belgium 13.17 23.80 4.43 2108.47 3.82
Run Code Online (Sandbox Code Playgroud)
文件"LifeCycleSavings"没有脱离.
以下R代码只给出正态分布的一半; 为了获得另一半,我应该改变什么代码?
halfnormal <- function(n){
vector <- rep(0,n)
for(i in 1:n){
uni_random <- runif(2)
y <- -log(uni_random)
while(y[2] < (y[1]-1)^2/2){
uni_random <- runif(2)
y <- -log(uni_random)
}
vector[i] <- y[1]
}
vector
}
output <- halfnormal(1000)
hist(output)
Run Code Online (Sandbox Code Playgroud)