Регрессия через начало координат в наборе данных Galton с помощью манипулятора для создания ползунка для бета-программирования R

Я очень новичок в R. Я пытаюсь запустить регрессию через начало в наборе данных Galton, используя манипулятор для создания слайдера для бета-версии.

Ниже приведен код, но я получаю несколько ошибок. Что я делаю не так? Я запускаю его в RStudio 3.2.5 на ПК с Windows 2016.

Код и сообщение об ошибке приведены ниже

Код
библиотека (UsingR)
библиотека (блестящая)
библиотека (ggplot2)
данные (Гальтон)

shinyUI(fluidPage(                # Application title        

  titlePanel("Now we re-create the scatterplot with the linear line that that is forced to go through the mean of the parent’s hight and mean of the child’s height."),  
  sidebarLayout(
    sidebarPanel(                        
      sliderInput("beta",                                   
                  "Beta:",                                    
                  min = 0.6,                                    
                  max = 1.2,                                    
                  step = .02,                                    
                  value = 0.6)                                        ),                                  

    # Show a plot of the generated distribution                

    mainPanel(                        

      plotOutput("myPlot",height = "700px", width = "700")
    )        
  )
))


output$myPlot <- renderPlot({        y <- galton$child - mean(galton$child)
x <- galton$parent - mean(galton$parent)
freqData <- as.data.frame(table(x, y))
names(freqData) <- c("child", "parent", "freq")
freqData$child <- as.numeric(as.character(freqData$child))
freqData$parent <- as.numeric(as.character(freqData$parent))   

# note couldn't get filter to work so changed it to subset below

g <- ggplot(subset(freqData,freq > 0), aes(x = parent, y = child))    
g <- g  + scale_size(range = c(2, 20), guide = "none" )    
g <- g + geom_point(colour="grey50", aes(size = freq+20, show_guide = FALSE))    
g <- g + geom_point(aes(colour=freq, size = freq))    
g <- g + scale_colour_gradient(low = "lightblue", high="white")                         g <- g + geom_abline(intercept = 0, slope = input$beta, size = 3)    
mse <- mean( (y - input$beta * x) ^2 )    
g <- g + ggtitle(paste("beta = ", input$beta, "mse = ", round(mse, 3)))    g       })

«`

Это мое сообщение об ошибке

R version 3.2.5 (2016-04-14) -- "Very, Very Secure Dishes"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> 
> 
> library(UsingR)
Loading required package: MASS
Loading required package: HistData
Loading required package: Hmisc
Loading required package: lattice
Loading required package: survival
Loading required package: Formula
Loading required package: ggplot2

Attaching package: ‘Hmisc’

The following objects are masked from ‘package:base’:

    format.pval, round.POSIXt, trunc.POSIXt, units


Attaching package: ‘UsingR’

The following object is masked from ‘package:survival’:

    cancer

> library(shiny)
> library(ggplot2)
> data(galton)
> 
> 
> shinyUI(fluidPage(                # Application title        
+   
+   titlePanel("Now we re-create the scatterplot with the linear line that that is forced to go through the mean of the parent’s hight and mean of the child’s height."),  
+   sidebarLayout(
+     sidebarPanel(                        
+       sliderInput("beta",                                   
+                   "Beta:",                                    
+                   min = 0.6,                                    
+                   max = 1.2,                                    
+                   step = .02,                                    
+                   value = 0.6)                                        ),                                  
+     
+     # Show a plot of the generated distribution                
+     
+     mainPanel(                        
+       
+       plotOutput("myPlot",height = "700px", width = "700")
+     )        
+   )
+ ))
<div class="container-fluid">
  <h2>Now we re-create the scatterplot with the linear line that that is forced to go through the mean of the parents hight and mean of the childs height.</h2>
  <div class="row">
    <div class="col-sm-4">
      <form class="well">
        <div class="form-group shiny-input-container">
          <label class="control-label" for="beta">Beta:</label>
          <input class="js-range-slider" id="beta" data-min="0.6" data-max="1.2" data-from="0.6" data-step="0.02" data-grid="true" data-grid-num="10" data-grid-snap="false" data-prettify-separator="," data-keyboard="true" data-keyboard-step="3.33333333333333" data-drag-interval="true" data-data-type="number"/>
        </div>
      </form>
    </div>
    <div class="col-sm-8">
      <div id="myPlot" class="shiny-plot-output" style="width: 700px ; height: 700px"></div>
    </div>
  </div>
</div>
> 
> 
> output$myPlot <- renderPlot({        y <- galton$child - mean(galton$child)
+ x <- galton$parent - mean(galton$parent)
+ freqData <- as.data.frame(table(x, y))
+ names(freqData) <- c("child", "parent", "freq")
+ freqData$child <- as.numeric(as.character(freqData$child))
+ freqData$parent <- as.numeric(as.character(freqData$parent))   
+ 
+ # note couldn't get filter to work so changed it to subset below
+ 
+ g <- ggplot(subset(freqData,freq > 0), aes(x = parent, y = child))    
+ g <- g  + scale_size(range = c(2, 20), guide = "none" )    
+ g <- g + geom_point(colour="grey50", aes(size = freq+20, show_guide = FALSE))    
+ g <- g + geom_point(aes(colour=freq, size = freq))    
+ g <- g + scale_colour_gradient(low = "lightblue", high="white")                         g <- g + geom_abline(intercept = 0, slope = input$beta, size = 3)    
Error: unexpected symbol in:
"g <- g + geom_point(aes(colour=freq, size = freq))    
g <- g + scale_colour_gradient(low = "lightblue", high="white")                         g"
> mse <- mean( (y - input$beta * x) ^2 )    
Error in mean((y - input$beta * x)^2) : object 'y' not found
> g <- g + ggtitle(paste("beta = ", input$beta, "mse = ", round(mse, 3)))    g       })
Error: unexpected symbol in "g <- g + ggtitle(paste("beta = ", input$beta, "mse = ", round(mse, 3)))    g"
> 
> ```

Ошибка: попытка использовать имя переменной нулевой длины

Метки

1 ответ

  1. На этой линии:


    g <- g + scale_colour_gradient(low = "lightblue", high="white") g <- g + geom_abline(intercept = 0, slope = input$beta, size = 3)

    есть две команды в одной строке. Каждый должен быть на своей линии (или ;отдельно).