R 新手,需要帮助编码功能和理解错误
New to R and need help coding function and understanding errors
所以我正在为 R 考 class,而且我很难编写基本公式。
基本上,我想要做的是找到 3 个变量,但我不断收到错误。 (我附上了一张图片以便于展示)
注:
d为自由度数,d=1,...,20
这是我的代码:
set.seed(29)
library(ISLR)
library(splines)
#### ETAPE 1
x <- runif(1000,min=0,max=10)
lambda=(2*x)+(0.2*x*sin(x))
y <- rpois(1000,lambda)
J <- data.frame(x=x, y=y)
plot(x,y,cex=0.4)
### ETAPE 2
ajust <- matrix(NA,20,1000)
for(i in (1:20)) {
smoothing=lm(y~ns(x=x,df=i),data=J)
ajust[i,]=predict(smoothing)
}
fd=function(d) {return(smoothing[d])}
for(i in (1:20)) {
lines(x,ajust[i,],col=i)
}
lines(x,lambda,col='black')
for(i in (1:20)) {
d1<- (1/1000)*sum((y-ajust[i,])**2)
}
### Calcul de D2
Mean=lambda
for (d in (1:20)){
W=(Mean-fd(x))**2
d2=sum(W)/1000
}
直到 "calcul de D2" 出现 "Non-numeric argument to binary operator " 错误。而且我不明白如何让它发挥作用。我知道我的问题可能看起来有点含糊,所以如果有什么不清楚的地方,请随时告诉我。
代码中的错误是您的 fd(x)
函数调用了 returns 列表。正如错误所说,这不是数字。
我们没有关于 f(d) 应该是什么的信息(它没有在图片或问题中定义),但似乎解决方案是从 fd(x)
中提取任何组件你的意思从 Mean
中减去。
例如:
for (d in (1:20)){
W=(Mean-fd(x)$fitted.values)**2
d2=sum(W)/1000
}
更新
我从图片中的方程式中看到了你关于 "D3" 的跟进 comment/question。我有点不确定,因为我没有 textbook/context 来确定符号(X 没有正式定义,我也不得不相信图片中的 Y = Mean
在代码中基于你如何使用它)。这是我最好的猜测,基于该上下文:
# The equation for d3 is the expected value of (Y-fd(X))^2.
#
# I don't know the context of this, but I see the definition of d1 and d2.
#
# D1 = for(i in (1:20)) {
# d1<- (1/1000)*sum((y-ajust[i,])**2)
# }
d1 # [1] 10.04203
#
# D2 = for (d in (1:20)){
# W=(Mean-fd(x)$fitted.values)**2
# d2=sum(W)/1000
# }
#
d2 # [1] 0.2024568
#
# Based on that, Y = Mean, y = y, x=x, i=i, N=1000
# W = (Y - fd(xi))^2
# I presume X = vectorized xi
#
# So, D3 =
D3 = (Mean - fd(x)$fitted.values)^2
#Since it's an expected value, I presume we take the mean
D3 = mean(D3)
我可能猜错的地方可能是 X。图中方程中的 X 看起来像所有 x
[i] 的向量。但是 x
的每个元素都是一个 x
[i] 所以 x
已经是它的向量表示。
所以我正在为 R 考 class,而且我很难编写基本公式。
基本上,我想要做的是找到 3 个变量,但我不断收到错误。 (我附上了一张图片以便于展示)
注:
d为自由度数,d=1,...,20
set.seed(29)
library(ISLR)
library(splines)
#### ETAPE 1
x <- runif(1000,min=0,max=10)
lambda=(2*x)+(0.2*x*sin(x))
y <- rpois(1000,lambda)
J <- data.frame(x=x, y=y)
plot(x,y,cex=0.4)
### ETAPE 2
ajust <- matrix(NA,20,1000)
for(i in (1:20)) {
smoothing=lm(y~ns(x=x,df=i),data=J)
ajust[i,]=predict(smoothing)
}
fd=function(d) {return(smoothing[d])}
for(i in (1:20)) {
lines(x,ajust[i,],col=i)
}
lines(x,lambda,col='black')
for(i in (1:20)) {
d1<- (1/1000)*sum((y-ajust[i,])**2)
}
### Calcul de D2
Mean=lambda
for (d in (1:20)){
W=(Mean-fd(x))**2
d2=sum(W)/1000
}
直到 "calcul de D2" 出现 "Non-numeric argument to binary operator " 错误。而且我不明白如何让它发挥作用。我知道我的问题可能看起来有点含糊,所以如果有什么不清楚的地方,请随时告诉我。
代码中的错误是您的 fd(x)
函数调用了 returns 列表。正如错误所说,这不是数字。
我们没有关于 f(d) 应该是什么的信息(它没有在图片或问题中定义),但似乎解决方案是从 fd(x)
中提取任何组件你的意思从 Mean
中减去。
例如:
for (d in (1:20)){
W=(Mean-fd(x)$fitted.values)**2
d2=sum(W)/1000
}
更新
我从图片中的方程式中看到了你关于 "D3" 的跟进 comment/question。我有点不确定,因为我没有 textbook/context 来确定符号(X 没有正式定义,我也不得不相信图片中的 Y = Mean
在代码中基于你如何使用它)。这是我最好的猜测,基于该上下文:
# The equation for d3 is the expected value of (Y-fd(X))^2.
#
# I don't know the context of this, but I see the definition of d1 and d2.
#
# D1 = for(i in (1:20)) {
# d1<- (1/1000)*sum((y-ajust[i,])**2)
# }
d1 # [1] 10.04203
#
# D2 = for (d in (1:20)){
# W=(Mean-fd(x)$fitted.values)**2
# d2=sum(W)/1000
# }
#
d2 # [1] 0.2024568
#
# Based on that, Y = Mean, y = y, x=x, i=i, N=1000
# W = (Y - fd(xi))^2
# I presume X = vectorized xi
#
# So, D3 =
D3 = (Mean - fd(x)$fitted.values)^2
#Since it's an expected value, I presume we take the mean
D3 = mean(D3)
我可能猜错的地方可能是 X。图中方程中的 X 看起来像所有 x
[i] 的向量。但是 x
的每个元素都是一个 x
[i] 所以 x
已经是它的向量表示。