[R] Missing index in vector assignment

Petr PIKAL petr.pikal at precheza.cz
Wed Mar 17 14:22:59 CET 2010


Hi


r-help-bounces at r-project.org napsal dne 17.03.2010 13:04:05:

> Jim & Petr,
> Thank you for your hint - I am really grateful, because they helped me 
to
> get one step further,
> and although now the problem lies somewhere else, you encouraged that we 
can
> find the
> solution soon!
> 
> 1. To Petr's comments
> 
> Petr, your hint to define y:
>   y <- LVvar[,1, drop=FALSE]
> did solve the problem, so I got a data.frame with the indexes.

Going through help page

y <- as.matrix(LVvar[,1, drop=FALSE])
x <- as.matrix(LVvar[,-1]
svp <- ksvm(x, y, type="nu-svc")

shall work. However as I never used this package and function I am not 
sure if it is used correctly.

Regards
Petr


> 
> Yet, then I turned to the call
>   svp <- ksvm(x, y, type="nu-svc")
>   Error in .local(x, ...) : y must be a vector or a factor.
> 
> So then I followed your second advice, looking up the additional 
information
> from help file:
> ==
> x is defined as:
>  a symbolic description of the model to be fit. When not using a formula 
x
> can be a matrix or vector containing the training data or a kernel 
matrix of
> class kernelMatrix of the training data or a list of character vectors 
(for
> use with the string kernel). Note, that the intercept is always 
excluded,
> whether given in the formula or not.
> 
> y is defined as
> a response vector with one label for each row/component of x. Can be 
either
> a factor (for classification tasks) or a numeric vector (for 
regression).
> ==
> 
> So I tried to convert LVvar into a matrix via as.matrix() but didn't 
make a
> difference.
> ============================================================
> 
> 2. To Jim's comments
> 
> On Wed, Mar 17, 2010 at 1:10 AM, jim holtman <jholtman at gmail.com> wrote:
> 
> > Please provide what LVvar is.
> >
> 
> LVvar is a dataframe
> 
> 
> > At least provide str(LVvar), or preferably a 'dput' of the object.
> >
> 
> str(LVvar) returns:
> 
> 'data.frame':   55 obs. of  7 variables:
>  $ rPerform : num  0.0682 -0.0682 -0.7443 0.7443 0.2619 ...
>  $ rCoordCap: num  4.98 6.08 5.73 5.92 4.96 ...
>  $ rKnowGrow: num  4.5 5.92 5.23 6.08 4.38 ...
>  $ rGoalcom : num  5.81 6.58 6 5.75 5.29 ...
>  $ rSupport : num  6.15 6.92 6.6 4.92 6 ...
>  $ rOpcomm  : num  5.98 6.25 6.33 6.5 5.29 ...
>  $ rT2Cadap : num  5.03 6.12 4.9 6.25 5.12 ...
>  - attr(*, "na.action")=Class 'omit'  Named int 40
>   .. ..- attr(*, "names")= chr "40"
> 
> ==
> 
> dput(LVvar) returns (abbreviated with ...):
> 
> structure(list(rPerform = c(0.0681818181818183, -0.0681818181818183,
> -0.744318181818182, 0.744318181818182, 0.261931818181818,
> -0.900568181818182,
> ...
>  rCoordCap = c(4.97916666666667,
> 6.08333333333333, 5.73333333333333, 5.91666666666667, 4.95833333333333,
> ...
> .Names = c("rPerform", "rCoordCap", "rKnowGrow",
> "rGoalcom", "rSupport", "rOpcomm", "rT2Cadap"), row.names = c(1L,
> 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
> 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L,
> 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 41L, 42L,
> 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L,
> 56L), na.action = structure(40L, .Names = "40", class = "omit"), class =
> "data.frame")
> 
> 
> 
> >
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html<http://www.r-project.org/
> posting-guide.html>
> > and provide commented, minimal, self-contained, reproducible code.
> >
> >
> 
> Here's my try (please have mercy for a complete R beginner):
> 
> library("kernlab")
> library("methods")
> 
> # Data Definitions
> LV <- c("rPerform","rCoordCap", "rKnowGrow", "rGoalcom", "rSupport",
> "rOpcomm", "rT2Cadap" )
> 
> # creates a dataframe
> LVvar <- na.omit(loopLV_IndexScores(LV, u_proj))
> 
> x <- (LVvar[,-1])
> y <- (LVvar[,1])
> svp <- ksvm(x, y, type="nu-svc")
> svp
> 
> ===
> 
> Thanks so much for not giving up.
> 
> Cheers,
> Chaehan
> 
> 
> 
> 
> > On Tue, Mar 16, 2010 at 6:03 PM, Chaehan So <chaehan.so at gmail.com> 
wrote:
> >
> >> Dear r-helpers,
> >>
> >> I am getting a mismatch error between two variables:
> >>
> >>  svp <- ksvm(x, y, type="nu-svc")
> >>  Error in .local(x, ...) : x and y don't match.
> >>
> >> and I suspect that it might be due to missing index in the y variable
> >> which
> >> I defined as:
> >>
> >>  y <- (LVvar[,1])
> >>
> >> I tried various methods to make the y assignment in the same format 
as x,
> >> which is a dataframe
> >>  x <- (LVvar[,-1])
> >>
> >> and looks like
> >>  x
> >>   rCoordCap rKnowGrow rGoalcom rSupport  rOpcomm rT2Cadap
> >> 1   4.979167  4.500000 5.812500 6.145833 5.979167 5.031250
> >> ...
> >>
> >> but I still get y without the indexes as a vector:
> >>  y
> >>  [1] -1.00000000 -6.91193182 -1.00000000  0.74431818 -6.91193182
> >>
> >>
> >> Why are the results different for x and y, even though the assignment 
is
> >> the
> >> same
> >> except I exclude the columns for y?
> >>
> >> Cheers,
> >> Chaehan
> >>
> >>        [[alternative HTML version deleted]]
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html<http://www.r-project.org/
> posting-guide.html>
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> >
> >
> > --
> > Jim Holtman
> > Cincinnati, OH
> > +1 513 646 9390
> >
> > What is the problem that you are trying to solve?
> >
> 
> 
> 
> -- 
> Humboldt University Berlin, Germany
> Institute of Psychology
> Rudower Chaussee 18, Room 1221
> 12489 Berlin
> Germany
> Office: +49 30 2093 - 9337
> Mobile: +49 171- 626 9373
> 
>    [[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list