hup2 <- beta2hat+sigma*solve(t(x2)%*%x2)*qnorm(0.025) Error in sigma * solve(t(x2) %*% x2) : non-numeric argument to binary operator
时间: 2024-02-13 22:06:54 浏览: 62
这个错误是因为在计算hup2时,sigma和solve(t(x2)%*%x2)*qnorm(0.025)之间缺少运算符,导致无法进行乘法运算。可以在两者之间添加一个乘号(*)进行修正,代码如下:
```
rm(list=ls())
k <- 1000
n <- 100
alp <- 0.05
test.res <- numeric(k)
sigma <- 1 # 定义模型误差的标准差
for(i in 1:k){
x1<- rnorm(n,0,0.5)
x2<- rbinom(n,1,prob=0.5)
y <- 2*x1+3*x2
eb <- rnorm(n,0,1)
beta1hat <- solve(t(x1)%*%x1)%*%t(x1)%*%y
beta2hat <- solve(t(x2)%*%x2)%*%t(x2)%*%y
}
hlo1 <- beta1hat-sigma*solve(t(x1)%*%x1)*qnorm(0.025)
hup1 <- beta1hat+sigma*solve(t(x1)%*%x1)*qnorm(0.025)
hlo2 <- beta2hat-sigma*solve(t(x2)%*%x2)*qnorm(0.025)
hup2 <- beta2hat+sigma*solve(t(x2)%*%x2)*qnorm(0.025)
```
这个错误修正后,代码应该能够正常运行了。
相关问题
rm(list=ls()) k <- 1000 n <- 100 alp <- 0.05 test.res <- numeric(k) for(i in 1:k){ x1<- rnorm(n,0,0.5) x2<- rbinom(n,1,prob=0.5) y <- 2*x1+3*x2 eb <- rnorm(n,0,1) beta1hat <- solve(t(x1)%*%x1)%*%t(x1)%*%y beta2hat <- solve(t(x1)%*%x1)%*%t(x1)%*%y } hlo1 <- beta1hat-sigma*solve(t(x1)%*%x1)*qnorm(0.025) hup1 <- beta1hat+sigma*solve(t(x1)%*%x1)*qnorm(0.025) hlo2 <- beta2hat-sigma*solve(t(x2)%*%x2)*qnorm(0.025) hup2 <- beta2hat+sigma*solve(t(x2)%*%x2)*qnorm(0.025) 报错
这段代码存在一些问题,导致运行时出现报错。其中的sigma没有定义,应该是指模型误差的标准差,需要在代码中进行定义。同时,最小二乘法估计beta2hat的时候,应该是用t(x2)而不是t(x1)进行计算。修改后的代码如下:
```
rm(list=ls())
k <- 1000
n <- 100
alp <- 0.05
test.res <- numeric(k)
sigma <- 1 # 定义模型误差的标准差
for(i in 1:k){
x1<- rnorm(n,0,0.5)
x2<- rbinom(n,1,prob=0.5)
y <- 2*x1+3*x2
eb <- rnorm(n,0,1)
beta1hat <- solve(t(x1)%*%x1)%*%t(x1)%*%y
beta2hat <- solve(t(x2)%*%x2)%*%t(x2)%*%y
}
hlo1 <- beta1hat-sigma*solve(t(x1)%*%x1)*qnorm(0.025)
hup1 <- beta1hat+sigma*solve(t(x1)%*%x1)*qnorm(0.025)
hlo2 <- beta2hat-sigma*solve(t(x2)%*%x2)*qnorm(0.025)
hup2 <- beta2hat+sigma*solve(t(x2)%*%x2)*qnorm(0.025)
```
这样修改后,代码应该能够正常运行了。
R语言rm(list=ls()) k <- 1000 n <- 100 alp <- 0.05 beta1hat <- numeric(k) beta2hat <- numeric(k) for(i in 1:k){ x1<- rnorm(n,0,0.5) x2<- rbinom(n,1,prob=0.5) eb <- rnorm(n,0,1) y <- 2*x1+3*x2 beta1hat[i]<- solve(t(x1)%*%x1)%*%t(x1)%*%y beta2hat[i]<- solve(t(x2)%*%x2)%*%t(x2)%*%y } sigma1 <- sqrt(var(beta1hat)) sigma2 <- sqrt(var(beta2hat)) hlo1 <- vector(beta1hat-sigma1*solve(t(x1)%*%x1)*qnorm(0.025)) hup1 <- vector(beta1hat+sigma1*solve(t(x1)%*%x1)*qnorm(0.025)) hlo2 <- vector(beta2hat-sigma2*solve(t(x2)%*%x2)*qnorm(0.025)) hup2 <- vector(beta2hat+sigma2*solve(t(x2)%*%x2)*qnorm(0.025))报错
根据您提供的代码,我发现以下问题:
1. 在for循环中,您计算了beta1hat和beta2hat,但是没有指定它们的初始化值。您需要在循环之前使用`beta1hat <- numeric(k)`和`beta2hat <- numeric(k)`进行初始化。
2. 在计算置信区间时,您使用了vector函数,但是vector函数只能用于创建向量,不能用于更新向量。您需要使用c函数(concatenate函数)来更新向量,例如:
```
hlo1 <- beta1hat-sigma1*solve(t(x1)%*%x1)*qnorm(0.025)
hlo1 <- c(hlo1, rep(0, k-length(hlo1)))
hup1 <- beta1hat+sigma1*solve(t(x1)%*%x1)*qnorm(0.025)
hup1 <- c(hup1, rep(0, k-length(hup1)))
hlo2 <- beta2hat-sigma2*solve(t(x2)%*%x2)*qnorm(0.025)
hlo2 <- c(hlo2, rep(0, k-length(hlo2)))
hup2 <- beta2hat+sigma2*solve(t(x2)%*%x2)*qnorm(0.025)
hup2 <- c(hup2, rep(0, k-length(hup2)))
```
3. 在计算beta1hat和beta2hat时,您使用了矩阵乘法运算符`%*%`,但是您需要将x1、x2和y转换为矩阵或列向量。您可以使用`as.matrix()`或`as.vector()`函数进行转换,例如:
```
x1<- as.matrix(rnorm(n,0,0.5))
x2<- as.matrix(rbinom(n,1,prob=0.5))
y <- as.matrix(2*x1+3*x2)
beta1hat[i]<- solve(t(x1)%*%x1)%*%t(x1)%*%y
beta2hat[i]<- solve(t(x2)%*%x2)%*%t(x2)%*%y
```
综上所述,以下是修改后的代码:
```
rm(list=ls())
k <- 1000
n <- 100
alp <- 0.05
beta1hat <- numeric(k)
beta2hat <- numeric(k)
for(i in 1:k){
x1<- as.matrix(rnorm(n,0,0.5))
x2<- as.matrix(rbinom(n,1,prob=0.5))
eb <- rnorm(n,0,1)
y <- as.matrix(2*x1+3*x2)
beta1hat[i]<- solve(t(x1)%*%x1)%*%t(x1)%*%y
beta2hat[i]<- solve(t(x2)%*%x2)%*%t(x2)%*%y
}
sigma1 <- sqrt(var(beta1hat))
sigma2 <- sqrt(var(beta2hat))
hlo1 <- beta1hat-sigma1*solve(t(x1)%*%x1)*qnorm(0.025)
hlo1 <- c(hlo1, rep(0, k-length(hlo1)))
hup1 <- beta1hat+sigma1*solve(t(x1)%*%x1)*qnorm(0.025)
hup1 <- c(hup1, rep(0, k-length(hup1)))
hlo2 <- beta2hat-sigma2*solve(t(x2)%*%x2)*qnorm(0.025)
hlo2 <- c(hlo2, rep(0, k-length(hlo2)))
hup2 <- beta2hat+sigma2*solve(t(x2)%*%x2)*qnorm(0.025)
hup2 <- c(hup2, rep(0, k-length(hup2)))
```
阅读全文