solve.default(vamos)中的错误:系统在计算上是单数的:倒数条件数

时间:2015-09-24 03:10:09

标签: r syntax-error

y <- sample(1:10,5)

sam<-sample(100:130,20)

x <- matrix(sam,5,4)

# 1a

betaestimator <- function(x,y) {
    b<-  solve(t(x)%*%x)%*%t(x)%*%y
b

}


#1b

covestimator <- function(x,y) {

    b<-  solve(t(x)%*%x)%*%t(x)%*%y
  t<-nrow(x)
    k<-ncol(x)
  s <- ((t(y-x%*%b))%*%(y-x%*%b))/(t-k)
  s2 <- matrix (s:s,k,k)
  e <- s2*(solve(t(x)%*%x))
  e

}


#1c

ttest <- function (x,y) {
    b<-  solve(t(x)%*%x)%*%t(x)%*%y
  t<-nrow(x)
    k<-ncol(x)
  s <- ((t(y-x%*%b))%*%(y-x%*%b))/(t-k)
  s2 <- matrix (s:s,k,k)
  e <- s2*(solve(t(x)%*%x))

estatt<-matrix(0,k,1)
   for (i in 1:k ) {
estatt[i,1] = b[i,1]*e[i,i]

}
estatt
}


#ESTATISTICA t TA DANDO MT BAIXO


#2.1


b1<-0.121
b2<-0.190
t<-23
c2<-2
theta<-0.6
x0<-rnorm(1,c2/(1-theta),1/(1-theta^2))




set.seed(1210686)
normais<-c(rnorm(23,0,1))


c<-0:22
c[1]<-x0

for (i in 2:23) {
c[i] = 2 + theta*c[i-1] + normais[i]
}

#2.2

epsilon<-c(rnorm(23,0,1))


#2.3
y<-0:22
for (i in 1:23) {
y[i] = b1 + b2*c[1] + epsilon[i]
}

ytransposto <- t(y)
tudoum<-matrix(1,1,23)

novax <- matrix(rbind(tudoum,c),2,23)
novax
vamos <- (t(novax)%*%novax)
vamos
solve(vamos)

当我尝试解决这个矩阵时,它给出了错误:

Error in solve.default(vamos): system is computationally singular: reciprocal condition number = 1.71139e-19
Error in solve.default(vamos): system is computationally singular: reciprocal condition number = 1.71139e-19

我无法更改数字,因为这是我老师给我的课程项目的一部分。谢谢!

1 个答案:

答案 0 :(得分:1)

solve()中,使用较小的容差,例如solve(..., tol = 1e-20)。 这应该没问题,因为你得到reciprocal condition number = 1.71139e-19help filethis related question中的更多信息。