Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 770
Carol Avatar asked Jul 06 '17 15:07

Carol


People also ask

How do you resolve an unexpected end of input?

In conclusion, If you're seeing the unexpected end of input error in your JavaScript code, it's likely that you're missing a closing brace somewhere. To fix this, simply check your code for any open braces and make sure that they all have corresponding closing braces.

How do I fix unexpected end of JSON input?

You can solve the "Unexpected end of JSON input" error in the following 3 ways: wrap your parsing logic in a try/catch block. make sure to return a valid JSON response from your server. remove the parsing logic from your code if you are expecting an empty server response.

What does unexpected end of input mean in JavaScript?

If you received the Unexpected end of input error, odds are you are missing a closing curly brace } so the error is basically saying “you opened the curly brace but the code ended before it was closed”.

How do you resolve an unexpected identifier?

To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.


1 Answers

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

like image 141
Lyngbakr Avatar answered Nov 15 '22 00:11

Lyngbakr