1

I would like to analyze a survey which was analyzed in the past with the SPSS software. The survey has a weight variable.

In SPSS it's easy to weight by variable (with the 'weight by' function) But I have problems while tring to do so with R.

I used the Survay package to include the weight variable. I will show here a more simple example:

    Data <- data.frame(
    X =c(1,4,6,4,1,7,3,2,2),
    Y = c(6,5,9,9,43,65,45,67,90),
    weight=c(0.1,1.2,4,0,0,5,0.65,1,0)
    )
    summary(Data )
      X               Y             weight     
 Min.   :1.000   Min.   : 5.00   Min.   :0.000
 1st Qu.:2.000   1st Qu.: 9.00   1st Qu.:0.000
 Median :3.000   Median :43.00   Median :0.650
 Mean   :3.333   Mean   :37.67   Mean   :1.328
 3rd Qu.:4.000   3rd Qu.:65.00   3rd Qu.:1.200
 Max.   :7.000   Max.   :90.00   Max.   :5.000
    library(survey)
    dat_weight=svydesign(ids = ~1, data = Data , weights = Data $weight)

Now I would like to save this object (dat_weight) as a simple data frame and use it for other analyses (such as PCA, CA, and so on).

Can it be done?

double-beep
  • 5,031
  • 17
  • 33
  • 41
tzipy
  • 147
  • 7
  • What are `dat1` and `dat_weight` like? Maybe you should use `str` function to have a look. – Feng Mar 14 '17 at 13:46
  • Can you make this example reproducible? You are more likely to get help that way. See: http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Sun Bee Mar 14 '17 at 13:52
  • Is it better now? – tzipy Mar 14 '17 at 20:21
  • Have you tried the `weighted.mean` base function? Also, I'm not really sure what you're actually trying to do. Can you just `temp <- summary(dat_weight)` – Patrick Williams Mar 14 '17 at 20:21
  • I want to perform PCA analysis on this data frame, but I don't know how to weight the observations by the variable "weight" and than save this as data frame. the function svydesign creates a "survey.design" class and can't be tranform to a data frame – tzipy Mar 14 '17 at 20:30
  • The weights were already calculated, why use the `survey` package? – Michelle Mar 13 '18 at 22:46
  • The use of weights in PCA does not appear to be exactly straightforward: https://stats.stackexchange.com/questions/113485/weighted-principal-components-analysis – Michelle Mar 13 '18 at 23:02

1 Answers1

0

I had the same problem until I found a surprisingly easy solution. You can use save() function to save a R object to a specific file with extension .RData. Once you have done that, you can read back the object with load() function without assigning it to anything because it will assign to its original name when you saved.

See the following example:

Data <- data.frame(
    X =c(1,4,6,4,1,7,3,2,2),
    Y = c(6,5,9,9,43,65,45,67,90),
    weight=c(0.1,1.2,4,0,0,5,0.65,1,0)
)
dat_weight <- survey::svydesign(ids = ~1, data = Data, weights = Data$weight)
save(dat_weight, file = "~/weighted_data.RData")

# clean environment
rm(list = ls())
load("~/weighted_data.RData")

Next time you don't have to apply weights on your dataset and instead directly dive into analysis.

Masood Sadat
  • 1,247
  • 11
  • 18