What does the "Unexpected end of input" error mean?

Carol picture Carol · Jul 6, 2017 · Viewed 18.5k times · Source

I am currently building an application in R to calculate the QR matrix decomposition, the QR non negative matrix decomposition and computing ICA. At the moment I am working on the first task. I am getting the following error:

source("trial.R")

Error in source("trial.R") : trial.R:153:0: unexpected end of input
151: 
152: 
    ^

Code:

library(rworldmap)


install.packages("plotrix")
library(plotrix)


install.packages("fastICA")
library(fastICA)
install.packages("Matrix")
source("utils.R")
library("corpcor")

twophase.cx <- function(A, k, fudge=5) {

    SVD=svd(normalised)
    V=SVD$v

    vk<-array(0,dim=c(nrow(V),k))
    cols_v=ncol(V)
    for(cl in 1:k){
     for(r in 1:nrow(V)){
      vk[r,cl]<-V[r,cl+k]
      }}

   probs=rep(0,k)

   prod=A%*%vk%*%t(vk)
    for(j in 1:k){

    subA=A[nrow(A)-j,ncol(A)-j]

    subV=vk[nrow(subV)-j,ncol(vk)-j]

    subprod=prod[nrow(prod)-j,ncol(prod)-j]

    normv<-norm(subV,"2")
    normA<-norm(subA,"2")
    normProd<-norm(subprod,"2")
    add1<-(normv*normv)/(2*k)
    add2<-((normA*normA)-(normProd*normProd))/(2*((normA*normA)-           (normProd*normProd)))                                          
    probs[j]<-add1+add2
  }

   const<-(k*log(k))

   keep<-sample(c(1:k),size=const,prob=probs,replace=FALSE)

   S1<-matrix(0,ncol(A),const)
   for(i in 1:ncol(A)){
    for(j in 1:const){
     if(is.element(j,keep)){
      S1[i,j]<-1}}}

  D1<-matrix(0,const,const)
 for(i in 1:const){
   if(is.element(i,keep)){
    prd<-const*probs[i]
   if(prd<1){D[i,i]<-prd}
   if(prd>=1){D[i,i]<-1}
  }

 toSample<-t(vk)%*%S1%*%D1
 S2<-qr(toSample)$pivot[1:k]
 C<-A %*% S1 %*% S2
cols<-C
X<-pseudoinverse(C)%*%A
err<-norm(A-C %*% X, 'F')
list(cols=C,X=X,  err=err)    

} 


cx <- function(A, k, nreps=20, ...) {
best.res = list(cols=NA, X=NA, err=Inf)
for (rep in 1:nreps) {
    res <- twophase.cx(A, k, ...)
    if (res$err < best.res$err) {
        best.res <- res
    }
}
best.res
}


data <- as.matrix( read.csv("worldclim.csv") )
coord <- read.csv("coordinates.csv")



data.normal <-scale(data,center=TRUE,scale=TRUE)
k<-10


cx.res <- cx(t(data.normal), 5)


xLim <- c(min(coord["lon"]), max(coord["lon"]))
yLim <- c(min(coord["lat"]), max(coord["lat"]))
map <- getMap(resolution="low")
plot(map, xlim=xLim, ylim=yLim, asp=1)



points(coord[cx.res$cols,1], coord[cx.res$cols,2], col=2, pch=19, cex=1.5)
points(coord[cx.res$cols,1], coord[cx.res$cols,2], col=4, pch=65:127,    cex=.6)

x <- cx.res$X[1,]
plot(map, xlim=xLim, ylim=yLim, asp=1)
points(coord[,1], coord[,2], col=color.scale(x, c(0,1), 0.8, 1,     color.spec="hsv"), cex=.6, pch=19)
color.legend(xLim[1]+1, yLim[1]-5, xLim[2]-1, yLim[1]-3, c(round(min(x),    4), round(mean(x), 4), round(max(x), 4)), color.scale(sort(x), c(0,1), 0.8, 1,    color.spec="hsv"), gradient="x")


convex.cone <- function(A, k) {

}


house.price <- read.csv("us_housing_prices.csv", row.names=1)


rownames(house.price)

colnames(house.price)[1:18]
colnames(house.price)[seq(1, by=12, to=ncol(house.price))]


plot.time.series(house.price[1:5,])
plot.time.series(house.price[6:10,])
plot.time.series(house.price[11:15,])
plot.time.series(house.price[16:20,])

hp <- house.price
hp[is.na(house.price)] <- 0


hp <- scale(t(hp))

hp.ica <- fastICA(hp, 20, fun="logcosh", verbose=TRUE)




rownames(hp.ica$S) <- colnames(house.price)
with(hp.ica, plot.time.series(t(S[,1:5])) )

with(hp.ica, plot(S[,1:2], asp=1) )

with(hp.ica, identify(S[,1:2], labels=rownames(S)) )

I am quite new to R. Please suggest which might be the issue.

Answer

Lyngbakr picture Lyngbakr · Jul 6, 2017

You're missing a } for the twophase.cx function, I suspect at line 72.